我知道我在这里有语法但是我无法弄明白。我正在尝试对5个表进行SELECT和INNER JOIN,但Symfony抱怨JOIN中的实体在被定义之前使用。
实际错误如下:[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.
这是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();
更新
正如所建议的那样,我已经废弃了DQL代码并使用了Query Builder代码。我得到一个非常类似的错误,上面写着'Categories c': Error: Class 'Categories' is not defined
。我的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('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();
有什么建议吗?
答案 0 :(得分:9)
DQL根据您的关联为您处理联接详细信息。通常,您只需要拼出FROM类名称。类似的东西:
'select C.name as CName, I.id as IId
FROM MySiteBundle:Categories C
INNER JOIN C.items');
最终使用查询构建器。
=============================================== ==============================
以下是在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();
}
答案 1 :(得分:3)
DQL不使用这样的连接。它们有点简化。但是我也发现它们没有被记录。
$em = $this->getDoctrine()->getEntityManager();
$query = $em->createQuery(
'select C.name as CName, I.id as IId
FROM MySiteBundle:Categories C
INNER JOIN C.items I');
$result = $query->getResult();
使用的实际关系取决于您的模型。
我通常使用查询构建器。
$em = $this->getEntityManager();
$request = $em->getRepository('MySiteBundle:Categories');
$qb = $request->createQueryBuilder('C');
$query = $qb
->select('C.name, I.id')
->innerJoin('C.items', 'I')
->getQuery();
答案 2 :(得分:0)
您可能需要检查注释或yml文件,以确保为OneToMany,ManyToMany和ManyToOne正确设置映射。
在您的MemberItems Controller或MemberItems Repository中输入:
$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('m')
->innerJoin('m.memberinfo_id', 'u')
->innerJoin('m.item_id', 'i')
->innerJoin('i.category_id', 'c')
->where(
->where('u.id = ?', $slug)
->orderBy('c.id', 'ASC')
->getQuery();
$memberItems = $qb->getResult();