【问题标题】:Is this possible to join tables in doctrine ORM without using relations?这是否可以在不使用关系的情况下连接学说 ORM 中的表?
【发布时间】:2010-02-04 20:39:59
【问题描述】:

假设有两个表。

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;

我无法弄清楚如何在学说 orm 中编写这样的查询

编辑:

表结构:

表 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 ;

表 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

【问题讨论】:

    标签: mysql doctrine dql


    【解决方案1】:

    试试这样的:---

    $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();
    

    【讨论】:

      【解决方案2】:

      您可以将 sql 直接写入 db 驱动程序。但是,您不会取回任何水合数组或对象。但有时它确实会派上用场:

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

      【讨论】:

        【解决方案3】:

        您可以在 Symfony 1.4 中手动更改您的表类,但这应该小心完成,因为您没有在数据库级别链接表,这通常应该发生。

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

        可能重复:doctrine join without relation

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-04-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-16
          • 2016-03-31
          • 1970-01-01
          相关资源
          最近更新 更多