2017-09-19 10 views
1

IDでリンクされた別のテーブルの情報に基づいて、あるテーブルから情報を取得しようとしています。doctrine query builderでの結合の実装が正しく動作しない

2つのテーブルは、propertyunitです。

プロパティ内のすべてのユニットを収集する必要がありますが、プロパティのステータスが「1」で隠しフラグが「0」の場合のみです。通常のMySQLでは、私が書いた:私は教義クエリビルダの文書から収集された情報を使用して

$qb = $this->getEntityManager()->createQueryBuilder(); 
$qb->select('u') 
    ->from('AppBundle:Unit', 'u') 
    ->join('u', 'AppBundle:Property', 'p', 'u.property = p.id') 
    ->where('p.status = :status') 
    ->andWhere('p.hidden = :hidden') 
    ->setParameter('status', 1) 
    ->setParameter('hidden', 0); 

return $qb->getQuery()->getResult(); 

:私はそれを用いquerybuilderをしようとするものの、

SELECT u.* FROM unit u INNER JOIN property p ON p.id = u.property WHERE p.status = 1 AND p.hidden = 0 

、正しい結果を生成します。しかし、私は次のエラーを取得するページをロードするとき:

[Semantical Error] line 0, col 42 near 'u AppBundle:Property': Error: Class 'u' is not defined.

クエリが実行されている:

SELECT u FROM AppBundle:Unit u INNER JOIN u AppBundle:Property P u.property = p.id WHERE p.status = :status AND p.hidden = :hidden 

を誰もが、私は私のクエリで間違ってやっているかを把握助けることができますか?これに

->join('u', 'AppBundle:Property', 'p', 'u.property = p.id') 

->join('AppBundle:Property', 'p', 'WITH', 'u.property = p.id') 

答えて

1

試してみますこれは、Doctrine QueryBuilderクラスのドキュメントです。

0

join()メソッドがあるので、あなたは、あなたの第一及び第二引数の場所を交換する必要があります

/** 
* Creates and adds a join over an entity association to the query. 
* 
* The entities in the joined association will be fetched as part of the query 
* result if the alias used for the joined association is placed in the select 
* expressions. 
* 
* <code> 
*  $qb = $em->createQueryBuilder() 
*   ->select('u') 
*   ->from('User', 'u') 
*   ->join('u.Phonenumbers', 'p', Expr\Join::WITH, 'p.is_primary = 1'); 
* </code> 
* 
* @param string  $join   The relationship to join. 
* @param string  $alias   The alias of the join. 
* @param string|null $conditionType The condition type constant. Either ON or WITH. 
* @param string|null $condition  The condition for the join. 
* @param string|null $indexBy  The index for the join. 
* 
* @return QueryBuilder This QueryBuilder instance. 
*/ 
public function join($join, $alias, $conditionType = null, $condition = null, $indexBy = null) 

これを変更する

関連する問題