2016-04-22 18 views
1

するのではなく、Entityクラスを取得しています。

$usersArray = $this->em->createQueryBuilder() 
         ->select("us") 
         ->from('Model\Repo\mytables\User', "us") 
         ->where("us.idUser = :idUser") 
         ->setParameter("idUser", $idUser) 
         ->getQuery() 
         ->execute(); 

なぜ私はその後、私はから(...)セクションに必要なクラスを指定した後でも、代わりにモデル\レポ\ MYTABLES \ユーザーのユーザー\クラスモデル\エンティティの\ MYTABLESのオブジェクトのリストを得るのですか?

答えて

2

実際、リポジトリはデータベースエントリの表現として使用できません。
つまり、リポジトリには、エンティティで表されるデータベースエントリを取得/作成/更新/削除するメソッドが含まれている必要があります。

これは、EntityManagerがエンティティマネージャと呼ばれる理由です。エンティティは管理しますが、リポジトリクラスは管理しません。例えば

、あなたは完璧に行うことができます。

// Return an instance of the Repository Model\Repo\mytables\User 
$repository = $this->em->getRepository('Model\Entity\mytables\User'); 

// The repository is used to create the QueryBuilder, so the from statement is already filled by doctrine as model\Entity\mytables\User 
$query = $repository->createQueryBuilder('u') 
// ... 

は、これはあなたが行うことができます理由もある:

$repository = $this->em->getRepository('Model\Entity\mytables\User'); 
// Return all entries of the 'users' table as Model\Entity\mytables\User instances 
$users = $repository->findAll(); 

私はあなたのクエリのfrom文が生成されないことを驚いています"Model \ Entity \ mytables \ Userは有効なエンティティではありません"のようなエラーです。

また、構造が混乱しているように見える場合は、リポジトリ(モデル)をエンティティから適切に区別して、それぞれの役割に従って使用する必要があります。

関連する問題

 関連する問題