隨着推進我們有findOneOrCreate()Symfony2的教義FindOneOrCreate
實施例。
$bookTag = BookTagQuery::create()
->filterByBook($book)
->findOneOrCreate();
在控制器中的任何地方的教條我們可以做類似的事情。
...................
$filename='something';
$document_exists = $em->getRepository('DemoBundle:Document')
->findOneBy(array('filename' => $filename));
if (null === $document_exists) {
$document = new Document();
$document->setFilename($filename);
$em->persist($document);
$em->flush();
}
是否有另一種方法可以在Doctrine中實現這一點?
可以在Entity Repository中調用實體管理器嗎? 有什麼建議嗎?
我在_construct()函數中得到這個錯誤。我必須錯過別的東西? 「可捕獲的致命錯誤:傳遞給Acme \ DemoBundle \ Entity \ DemoFileRepository :: __ construct()的參數2必須是一個Acme \ DemoBundle \ Entity \ Mapping \ ClassMetadata實例,Doctrine \ ORM \ Mapping \ ClassMetadata實例」 – mirk
更改參數鍵入到'ClassMetaData'並添加'use Doctrine \ ORM \ Mapping'語句。否則,它會在您的名字空間中尋找該類。 – althaus
再次出現同樣的錯誤。 – mirk