2017-05-04 58 views
0

我有一個存儲我打印的3D對象的實體。Symfony主義存儲庫返回實體實例

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; 

我必須得到$ threedobject這(我指的是應用程序\ ThreedBundle \實體\ Threedobject實例)的所有實例,它代表通過Doctrine進行sql查詢。 我試過下面的查詢構建器,但它已經返回值的數組表示,但大多數情況下我必須使用元素的方法,所以我想獲取這些實例。

$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(); 

我讀庫查找方法,但是這不是我想要的東西,或者我不完全理解它是如何工作的。

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到Threedprint的反向關係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

我讀了關於這個錯誤消息的每一個SO帖子,沒有得到解決方案不幸。 – PumpkinSeed