Query Builder / DQL不能使用INNER JOIN - 语法问题 [英] Query Builder / DQL not working with INNER JOIN - Syntax Issue

查看:119
本文介绍了Query Builder / DQL不能使用INNER JOIN - 语法问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我有一个语法是在这里,但我不能弄清楚。我试图做一个5表的SELECT和INNER JOIN,但Symfony正在抱怨JOIN中的实体被定义之前使用。



实际错误如下: [语义错误]行0,col 121靠近'I ON C.id =':错误:标识变量MySiteBundle:连接路径表达式中使用的项目,但未定义。



这是PHP代码。



注意:我缩短了查询两列,两个表和一个连接来保持问题简单,并显示我的观点。实际的查询时间要长得多,并产生相同的错误。

  $ em = $ this-> getDoctrine() - > ; getEntityManager(); 
$ query = $ em-> createQuery(
'select C.name as CName,I.id as IId
FROM MySiteBundle:Categories C
INNER JOIN MySiteBundle:Items I ON C.id = I.category_id');
$ result = $ query-> getResult();






更新 p>

如我所说,我已经取消了DQL代码,并且使用了Query Builder代码。我正在收到一个非常类似的错误,它说'Categories c':错误:类'Categories'没有定义。我的QB代码在下面。

  $ em = $ this-> getDoctrine() - > getEntityManager(); 
$ qb = $ em-> createQueryBuilder()
- > select('c.name,i.id,i.image,i.name,i.description,m.id,m .quantity,m.value,m.qty_received,m.custom_image,m.custom_name,m.custom_description,u.user1fname,u.user1lname,u.user2fname,u.user2lname')
- > from('类别','c')
- > innerJoin('Items','i','ON','c.id = i.category_id')
- > innerJoin('MemberItems' ,'m','ON','i.id = m.item_id')
- > innerJoin('User','u','ON','m.memberinfo_id = u.id')
- > where('u.id =?',$ slug)
- > orderBy('c.id','ASC')
- > getQuery();

$ memberItems = $ qb-> getResult();

任何建议?

解决方案当我打字时,路易斯发贴。哦,好吧。



DQL根据你的关联来照顾你的联盟细节。一般来说,您只需要拼出FROM类的名称。如下所示:

 '选择C.name作为CName,I.id as IId 
FROM MySiteBundle:Categories C
INNER JOIN C.items');

并明确使用查询构建器。



================================================== =========================



以下是使用查询构建器的示例在Symfony 2中。

  public function getAccounts($ params = array())
{
//构建查询
$ em = $ this-> getEntityManager();
$ qb = $ em-> createQueryBuilder();

$ qb-> addSelect('account');
$ qb-> addSelect('accountPerson');
$ qb-> addSelect('person');
$ qb-> addSelect('registeredPerson');
$ qb-> addSelect('projectPerson');

$ qb-> from('ZaysoCoreBundle:Account','account');

$ qb-> leftJoin('account.accountPersons','accountPerson');
$ qb-> leftJoin('accountPerson.person','person');
$ qb-> leftJoin('person.registeredPersons','registeredPerson');
$ qb-> leftJoin('person.projects','projectPerson');
$ qb-> leftJoin('projectPerson.project','project');

if(isset($ params ['accountId']))
{
$ qb-> andWhere($ qb-> expr() - & 'account.id',$ PARAMS [ '的accountId']));
}
if(isset($ params ['projectId']))
{
$ qb-> andWhere($ qb-> expr() - > ( 'project.id',$ PARAMS [ '专案编号']));
}
if(isset($ params ['aysoid']))
{
$ qb-> andWhere($ qb-> expr() - > eq ( 'registeredPerson.regKey',$ QB-> EXPR() - >文字($ PARAMS [ 'aysoid'])));
}
$ query = $ qb-> getQuery();

// die('DQL'。$ query-> getSQL());
return $ query-> getResult();
}


I know I have a syntax isse here however I cant figure it out. I'm trying to do a SELECT and INNER JOIN of 5 tables but Symfony is complaining about the Entities in the JOIN are used before being defined.

Actual error is as follows: [Semantical Error] line 0, col 121 near 'I ON C.id = ': Error: Identification Variable MySiteBundle:Items used in join path expression but was not defined before.

Here is the PHP code.

Note: I have shortened this query to two columns, two tables, and one join to keep the question simple and show my point. The actual query is much longer and is producing the same error.

$em = $this->getDoctrine()->getEntityManager();
$query = $em->createQuery(
    'select C.name as CName, I.id as IId
    FROM MySiteBundle:Categories C
    INNER JOIN MySiteBundle:Items I ON C.id = I.category_id');
$result = $query->getResult();


Update

As suggested I've done away with the DQL code and am using Query Builder code. I'm getting a very similiar error which says 'Categories c': Error: Class 'Categories' is not defined. My QB code is below.

$em = $this->getDoctrine()->getEntityManager();
$qb = $em->createQueryBuilder()
        ->select('c.name, i.id, i.image, i.name, i.description, m.id, m.quantity, m.value, m.qty_received, m.custom_image, m.custom_name, m.custom_description, u.user1fname, u.user1lname, u.user2fname, u.user2lname')
        ->from('Categories', 'c')
        ->innerJoin('Items', 'i', 'ON', 'c.id = i.category_id')
        ->innerJoin('MemberItems', 'm', 'ON', 'i.id = m.item_id')
        ->innerJoin('User', 'u', 'ON', 'm.memberinfo_id = u.id')
        ->where('u.id = ?', $slug)
        ->orderBy('c.id', 'ASC')
        ->getQuery();

$memberItems = $qb->getResult();

Any suggestions?

解决方案

Louis posted while I was typing. Oh well.

DQL takes care of the join details for you based on your associations. In general, you only need to spell out the FROM class name. Something like:

'select C.name as CName, I.id as IId
FROM MySiteBundle:Categories C
INNER JOIN C.items');

And definitively use query builder.

=============================================================================

Here is an example of using query builder in Symfony 2.

public function getAccounts($params = array())
{
    // Build query
    $em = $this->getEntityManager();
    $qb = $em->createQueryBuilder();

    $qb->addSelect('account');
    $qb->addSelect('accountPerson');
    $qb->addSelect('person');
    $qb->addSelect('registeredPerson');
    $qb->addSelect('projectPerson');

    $qb->from('ZaysoCoreBundle:Account','account');

    $qb->leftJoin('account.accountPersons',  'accountPerson');
    $qb->leftJoin('accountPerson.person',    'person');
    $qb->leftJoin('person.registeredPersons','registeredPerson');
    $qb->leftJoin('person.projects',         'projectPerson');
    $qb->leftJoin('projectPerson.project',   'project');

    if (isset($params['accountId']))
    {
        $qb->andWhere($qb->expr()->in('account.id',$params['accountId']));
    }
    if (isset($params['projectId']))
    {
        $qb->andWhere($qb->expr()->in('project.id',$params['projectId']));
    }
    if (isset($params['aysoid']))
    {
        $qb->andWhere($qb->expr()->eq('registeredPerson.regKey',$qb->expr()->literal($params['aysoid'])));
    }
    $query = $qb->getQuery();

  //die('DQL ' . $query->getSQL());
    return $query->getResult();
}

这篇关于Query Builder / DQL不能使用INNER JOIN - 语法问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆