我正在Symfony 3中编写一个非常简单的API用于学习目的。我想覆盖服务区。
我有一个存储库,我想要注入Doctrine ORM,这样做我试图在我的services.xml文件中创建Doctrine ORM服务。
的services.xml:
<service
id="orm.doctrine.service"
class="Book\BookBundle\DoctrineORM\DoctrineRepo">
<argument type="service" id="doctrine.orm.entity_manager" />
</service>
<service
id="book.repo.create_book_repository"
class="Book\BookBundle\Repositories\CreateBookRepository">
<argument type="service" id="orm.doctrine.service" />
</service>
doctrine是一个随作曲家安装的供应商库,因此我的Bundle中没有类。我试着引用它。
DoctrineRepo:
<?php
namespace Book\BookBundle\DoctrineORM;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class DoctrineRepo extends Controller
{
private $db;
public function initDoctrine()
{
$this->db = $this->get('doctrine.orm.entity_manager');
return $this->db;
}
}
CreateBookRepository:
<?php
命名空间Book \ BookBundle \ Repositories;
使用Book \ BookBundle \ Entity \ BookEntity; 使用Book \ BookBundle \ DoctrineORM \ DoctrineRepo;
类CreateBookRepository {
private $db;
public function __construct(
DoctrineRepo $doctrineRepository
) {
$this->db = $doctrineRepository;
}
public function createRepository()
{
$product = new BookEntity();
$product->setTitle('Test Title');
$product->setAuthor('Leps');
$product->setReleaseDate(new \DateTime);
$product->setGender('Scfi');
$product->setDescription('Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.');
$em = $this->db->initDoctrine()->getDoctrine()->getManager();
$em->persist($product);
$em->flush();
}
}
我在这里缺少什么或不理解......?
答案 0 :(得分:2)
如果您需要在特定服务中注入doctrine实体管理器,请查看:
<service id="your_service" class="path_to_your_class_service">
<argument type="service" id="doctrine.orm.entity_manager" />
</service>
答案 1 :(得分:1)
首先,让我们配置我们的服务:
#../services.xml
<service id="book.manager" class="Book\BookBundle\Manager\BookManager">
<argument type="service" id="doctrine.orm.entity_manager" />
</service>
其次,创建服务(BookManager)
namespace Book\BookBundle\Manager;
use Doctrine\ORM\EntityManager;
use Book\BookBundle\Entity\BookEntity;
class BookManager
{
/** @var Entity Manager */
protected $em;
/**
* Constructor
*
* @param \Doctrine\ORM\EntityManager $em
*/
public function __construct(EntityManager $em)
{
$this->em = $em;
}
public function createBook(BookEntity $book)
{
/... a specific logic if you need .../
$this->em->persist($book);
$this->em->flush();
}
}
要完成,请在您需要的地方致电您的服务(例如在控制器中)
public function yourSpecificAction()
{
// We instanciate a new object
$book = new BookEntity();
$book->setTitle('Test Title');
$book->setAuthor('Leps');
$book->setReleaseDate(new \DateTime);
$book->setGender('Scfi');
$book->setDescription('Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.');
// we call the service
$bookManager = $this->get('book.manager');
// we call the method you need
$bookManager->createBook($book);
// same to: $bookManager = $this->get('book.manager')->createBook($book);
}
希望它能帮到你......