这是否可以在不使用关系的情况下连接学说 ORM 中的表?

Is this possible to join tables in doctrine ORM without using relations?(这是否可以在不使用关系的情况下连接学说 ORM 中的表?)
本文介绍了这是否可以在不使用关系的情况下连接学说 ORM 中的表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设有两个表.

Table X--
Columns:
     id         x_value

Table Y--
Columns:
     id        x_id      y_value

现在我不想在学说类中定义关系,我想使用这两个表使用这样的查询来检索一些记录:

Now I dont want to define relationship in doctrine classes and i want to retrieve some records using these two tables using a query like this:

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

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

I m not able to figure out how to write query like this in doctrine 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, 0, '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();

这篇关于这是否可以在不使用关系的情况下连接学说 ORM 中的表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)