加入收藏 | 设为首页 | 会员中心 | 我要投稿 东莞站长网 (https://www.0769zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

mysql-是否可以在不使用关系的情况下联接原则ORM中的表?

发布时间:2021-01-24 23:31:33 所属栏目:MySql教程 来源:网络整理
导读:假设有两个表. Table X--Columns: id x_valueTable Y--Columns: id x_id y_value 现在,我不想在学说类中定义关系,并且我想使用像这样的查询使用这两个表来检索一些记录: Select x_value from x,y where y.id="variable_z" and x.id=y.x_id; 我无法弄清楚如

假设有两个表.

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:xampphtdocsfanyerdoctrinelibDoctrineTable.php:290 Stack trace: #0 
 C:xampphtdocsfanyerdoctrinelibDoctrineTable.php(240): Doctrine_Table-   >initDefinition() #1 C:xampphtdocsfanyerdoctrinelibDoctrineConnection.php(1127): 
Doctrine_Table->__construct('u',Object(Doctrine_Connection_Mysql),true) #2 
C:xampphtdocsfanyerdoctrinelibDoctrineRawSql.php(425): Doctrine_Connection-
>getTable('u') #3 C:xampphtdocsfanyerdoctrinemodelsImage.php(33):    Doctrine_RawSql-
>addComponent('img','u.Image imga') #4   C:xampphtdocsfanyercommunity_images.php(31): 
  Image->get_community_images_gallery_filter(4,'AL','ALBERTVILLE') #5 {main} thrown      in 
    C:xampphtdocsfanyerdoctrinelibDoctrineTable.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();

(编辑:东莞站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读