2017-05-04 2 views
0

私は印刷した3Dオブジェクトを格納するエンティティを持っています。symfonyのdoctrineリポジトリはエンティティのインスタンスを返します

private $id; 
/** 
* @ORM\Column(type="array", nullable=true) 
*/ 
private $images; 
/** 
* @ORM\Column(type="datetime") 
*/ 
private $date_created; 
/** 
* @ORM\Column(type="datetime") 
*/ 
private $date_modified; 
/** 
* @ORM\ManyToOne(targetEntity="App\UserBundle\Entity\User") 
*/ 
private $user; 
/** 
* @ORM\ManyToOne(targetEntity="App\ThreedBundle\Entity\Threedobject", cascade={"all"}) 
*/ 
private $threedobject; 
/** 
* @ORM\Column(type="text", nullable=true) 
*/ 
private $description; 

あり、このようになりますSQLクエリ:表す

select threedobject_id from threed_print where user_id = {RANDOM_NUMBER} group by threedobject_id; 

私は(私はアプリケーション\ ThreedBundle \エンティティ\ Threedobjectインスタンスを意味する)は、すべてのインスタンスthreedobject $を取得する必要がありますDoctrineを介したSQLクエリに続きます。 次のquerybuilderを試しましたが、値の配列表現が返されましたが、ほとんどの場合、要素のメソッドを使用しなければならないため、インスタンスを取得します。

$query = $this->em->createQueryBuilder(); 
$result = $query 
    ->select('tp') 
    ->addSelect('to') 
    ->from('ThreedBundle:ThreedPrint', 'tp') 
    ->join('tp.threedobject', 'to') 
    ->join('tp.user', 'u') 
    ->where('u.id = :userID') 
    ->groupby('to.id') 
    ->setParameter('userID', $userID) 
    ->getQuery(); 
return $result->getResult(); 

私は、リポジトリfindメソッドについて読むが、これでは私が欲しいものではない、または私は完全にそれが機能しているか理解していています。

UPDATE:

基本的に私は必要なもの:

$query = $this->em->createQueryBuilder(); 
$result = $query 
    ->select('to') 
    ->from('ThreedBundle:ThreedPrint', 'tp') 
    ->join('tp.threedobject', 'to') 
    ->join('tp.user', 'u') 
    ->where('u.id = :userID') 
    ->groupby('to.id') 
    ->setParameter('userID', $userID) 
    ->getQuery(); 
return $result->getResult(); 

しかし、私はそれのために、次のエラーだ:リポジトリを使用する場合は、あなたがすべき

'SELECT to FROM': Error: Cannot select entity through identification variables without choosing at least one root entity alias. 

答えて

0

ThreedObjectからThreedObjectへの逆関係oneToManyを実装し、Threedobjectに$ threedprintsフィールドを追加する必要があります。

その後、私は質問を更新し、この

$threedobjects=$this->em->createQueryBuilder() 
->select('to') 
->from('ThreedBundle:Threedobject') 
->join('to.threedprints', 'tp') 
->join('tp.user', 'u') 
->where('u.id = :userID') 
->setParameter('userID', $userID) 
->getQuery()->getResult(); 
0

をsrc(通常はRepositoryと呼ぶ)の下にプロジェクト内のフォルダを作成し、名前を付けて新しいクラスを作成します(例:ThreedobjectReposito ry)。

、あなたはそのクラスに次のように置く:

namespace NameProject\NameBundle\Repository; 

use Doctrine\ORM\EntityRepository; 


class ThreedobjectRepository extends EntityRepository 
{ 

function findById3D($a) 
{ 
$query = $this->getEntityManager() 
    ->createQuery("Select th.threedobject_id AS id 
           FROM NameprojectNameBundle:threed_print th 
           Where th.user_id=:user_id 
           GROUP BY threedobject_id") 
       set parameter (user_id, $a); 

return $query->getResult(); 


} 

} 

はあなたthreed_print.phpにこのラインを置くことを忘れないでください:

/** 
* @ORM\Entity(repositoryClass="Nameproject\NameBundle\Repository\ThreedobjectRepository") 
* @ORM\Table 
*/ 

、あなたはあなたがしているコントローラに行くことができますfindall()やfindoneby()を使う代わりに、findById3D($ a)を既に作成した関数を使い、使いたいインスタンスを簡単に使うだけです。

私はあなたを助けたと思います。

+0

を書くことができますが、問題は、私はIDを取得することができますが、私は、オブジェクトに関するエンティティのインスタンスが必要です。ありがとうございました – PumpkinSeed

+0

私はそれを見ましたが、同じ問題について話しています。あなたはそれを見ましたか?:http://stackoverflow.com/questions/17878237/doctrine-cannot-select-entity-through-identification-変数を選択せず​​に –

+0

私はこのエラーメッセージについてのすべての投稿を読んで、解決策を不幸にしませんでした。 – PumpkinSeed

関連する問題