Doctrineでは、Entityというものを使うけど、これは基本的にデータを保持するためだけのクラス。
で、実際に find などの様にデータベース上のデータをフェッチするには、Repository を使用する。
こんな感じ。(http://docs.symfony.gr.jp/symfony2/book/doctrine.htmlより引用)
$product = $this->getDoctrine()
-$gt;getRepository('AcmeStoreBundle:Product')
-$gt;find($id);
...で、このgetRepositoryを見てみると、
/**
* Gets the repository for an entity class.
*
* @param string $entityName The name of the entity.
* @return EntityRepository The repository class.
*/
public function getRepository($entityName)
{
$entityName = ltrim($entityName, '\\');
if (isset($this->repositories[$entityName])) {
return $this->repositories[$entityName];
}
$metadata = $this->getClassMetadata($entityName);
$customRepositoryClassName = $metadata->customRepositoryClassName;
if ($customRepositoryClassName !== null) {
$repository = new $customRepositoryClassName($this, $metadata);
} else {
$repository = new EntityRepository($this, $metadata);
}
$this->repositories[$entityName] = $repository;
return $repository;
}
の様になっている。
で、ここで注目するのは
if ($customRepositoryClassName !== null) {
$repository = new $customRepositoryClassName($this, $metadata);
} else {
$repository = new EntityRepository($this, $metadata);
}のとこ。つまりカスタムリポジトリクラスがあれば、それを使用するけど、そうじゃなきゃEntityRepositoryを使うよーってことですね。
ってことで、自分で Repositoryを作成するには、EntityRepositoryを継承して独自にカスタマイズすればOK。
データ取得とかはコントローラでなくてモデル側に書いたほうが良いので、そういうのは独自のリポジトリを用意してそこで処理をさせたほうが綺麗!
ってことらしいです。



0 コメント:
コメントを投稿