관계를 사용하지 않고 교리 ORM에서 테이블에 가입 할 수 있습니까?

StackOverflow https://stackoverflow.com/questions/2203027

  •  18-09-2019
  •  | 
  •  

문제

두 개의 테이블이 있다고 가정합니다.

Table X--
Columns:
     id         x_value

Table Y--
Columns:
     id        x_id      y_value

이제 나는 교리 수업에서 관계를 정의하고 싶지 않으며 다음과 같은 쿼리를 사용 하여이 두 테이블을 사용하여 일부 레코드를 검색하고 싶습니다.

Select x_value from x, y where y.id="variable_z" and x.id=y.x_id;

나는 교리에서 이와 같은 쿼리를 작성하는 방법을 알 수 없습니다.

편집하다:

테이블 구조 :

1 번 테이블:

CREATE TABLE IF NOT EXISTS `image` (
  `id` int(11) NOT NULL AUTO_INCREMENT, 
`random_name` varchar(255) NOT NULL,
 `user_id` int(11) NOT NULL,
  `community_id` int(11) NOT NULL,
  `published` varchar(1) NOT NULL,
  PRIMARY KEY (`id`)
 ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=259 ;

Table 2:

  CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
   `city` varchar(20) DEFAULT NULL,
   `state` varchar(20) DEFAULT NULL,
   `school` varchar(50) DEFAULT NULL,
   PRIMARY KEY (`id`)
 ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=20 ;

사용 중입니다.

        $q = new Doctrine_RawSql();

        $q  ->select('{u.*}, {img.*}')
        ->from('users u LEFT JOIN image img ON u.id = img.user_id')
        ->addComponent('u', 'Users u')
        ->addComponent('img', 'u.Image img')
        ->where("img.community_id='$community_id' AND img.published='y' AND u.state='$state' AND u.city='$city
        ->orderBy('img.id DESC')
        ->limit($count+12)  
        ->execute();        

오류가 발생합니다.

 Fatal error: Uncaught exception 'Doctrine_Exception' with message 'Couldn't find class
 u' in C:\xampp\htdocs\fanyer\doctrine\lib\Doctrine\Table.php:290 Stack trace: #0 
 C:\xampp\htdocs\fanyer\doctrine\lib\Doctrine\Table.php(240): Doctrine_Table-   >initDefinition() #1 C:\xampp\htdocs\fanyer\doctrine\lib\Doctrine\Connection.php(1127): 
Doctrine_Table->__construct('u', Object(Doctrine_Connection_Mysql), true) #2 
C:\xampp\htdocs\fanyer\doctrine\lib\Doctrine\RawSql.php(425): Doctrine_Connection-
>getTable('u') #3 C:\xampp\htdocs\fanyer\doctrine\models\Image.php(33):    Doctrine_RawSql-
>addComponent('img', 'u.Image imga') #4   C:\xampp\htdocs\fanyer\community_images.php(31): 
  Image->get_community_images_gallery_filter(4, 0, 'AL', 'ALBERTVILLE') #5 {main} thrown      in 
    C:\xampp\htdocs\fanyer\doctrine\lib\Doctrine\Table.php  on line 290
도움이 되었습니까?

해결책

다음과 같은 것을 시도하십시오 : ---

$q = new Doctrine_RawSql();
$this->related_objects = $q->
        select('{o.name}')->
        from('tagset t1 JOIN tagset t2 ON t1.tag_id = t2.tag_id AND t1.object_id != t2.object_id JOIN object o ON t2.object_id = o.id')->
        addComponent('o','Object o')->
        where('t1.object_id = ?', $this->object->id)->
        groupBy('t2.object_id')->
        orderBy('COUNT(*) DESC')->
        execute();

다른 팁

SQL을 DB 드라이버에 직접 쓸 수 있습니다. 그러나 수화 된 배열이나 객체를 되 찾을 수는 없습니다. 그러나 때로는 편리합니다.

 $sql = "SELECT * FROM... ";        
 $pdo = Doctrine_Manager::connection()->getDbh();       
 $data = $pdo->query($sql)->fetchAll();

Symfony 1.4에서 테이블 클래스를 수동으로 변경할 수 있지만 일반적으로 발생 해야하는 데이터베이스 수준에서 테이블을 연결하지 않기 때문에 신중하게 수행해야합니다.

class TableName extends BaseTableName
{
    public function setUp()
    {
        $this->hasOne('LinkedTableName', array(
             'local' => 'link_id',
             'foreign' => 'link_id'));
        parent::setUp();
    }
}

가능한 복제 : 교리는 관계없이 가입합니다

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top