12

是否有可能在 Symfony 2 和 Doctrine 2 中拥有一个不与实体关联的自定义存储库?我想在其中放入一些不适合其他存储库的本机 SQL(它可能引用抽象或实体层次结构)。

$this->getDoctrine()->getRepositoty(/* ??? */)应该如何替换控制器代码?

4

3 回答 3

16

您可以拥有任意数量的存储库。但是,只有一个存储库可以与实体管理器链接。

您需要定义一些服务来添加自定义存储库。

<!-- My custom repository -->
<service id="acme.repository.my_entity" class="Acme\FQCN\MyEntityRepository" >
    <argument type="service" id="doctrine.orm.entity_manager" />
    <argument type="service" id="acme.metadata.my_entity" />
</service>

<!-- MyEntity metadata -->
<service id="acme.metadata.my_entity" class="Doctrine\ORM\Mapping\ClassMetaData">
    <argument>Acme\FQCN\MyEntity</argument>
</service>

存储库类必须继承自EntityRepository.

namespace Acme\FQCN;

use Doctrine\ORM\EntityRepository;

class MyEntityRepository extends EntityRepository
{
    /** 
     * If you want to inject any custom dependencies, you'd have either have to
     * add them to the construct or create setters. I'd suggest using setters
     * in which case you wouldn't need to use the constructor in this class.
     *
     * public function __construct($em, Doctrine\ORM\Mapping\ClassMetadata $class, $custom_dependency)
     * {
     *     parent::__construct($em, $class);
     * }
     *
     */
}

不幸的是,您将无法通过学说服务检索它。相反,直接从容器中检索它:

$this->get('acme.repository.my_entity');

编辑

如果您正在创建不应链接到任何实体的存储库,只需创建一个服务并注入必要的依赖项。

<!-- Repository for misc queries -->
<service id="acme.repository.misc" class="Acme\FQCN\MiscRepsitory">
    <argument type="service" id="database_connection" />
</service>

由于您没有在自定义存储库中使用任何 Doctrine 的 ORM 功能,因此无需扩展EntityManager.

namespace Acme\FQCN;

use \Doctrine\DBAL\Connection;

class MiscRepository
{
    protected $conn;

    public function __construct(Connection $conn)
    {
        $this->conn = $conn;
    }
}
于 2012-07-10T10:27:11.470 回答
6

我采用了一个稍微不同的解决方案,使用 Symfony2 父服务。

首先,我创建了一个父服务,一个GenericRepository公开了几个方法的类,如果我们想在将来重构我们的代码,它会让生活变得更轻松。

services.yml

acme_core.generic_repository:
    abstract: true
    class: Acme\Bundle\CoreBundle\Repository\GenericRepository
    arguments: [@doctrine.orm.entity_manager]

Acme\Bundle\CoreBundle\Repository\GenericRepository

<?php

namespace Acme\Bundle\CoreBundle\Repository;

use Doctrine\ORM\EntityManager;

/**
 * Class GenericRepository
 * @package Acme\Bundle\CoreBundle\Repository
 */
abstract class GenericRepository {
    /**
     * @var EntityManager
     */
    private $entityManager;

    /**
     * @param EntityManager $entityManager
     */
    public function __construct(EntityManager $entityManager) {
        $this->entityManager = $entityManager;
    }

    /**
     * @return EntityManager
     */
    public function getEntityManager() {
        return $this->entityManager;
    }

    /**
     * @return \Doctrine\DBAL\Connection
     */
    public function getConnection() {
        return $this->getEntityManager()->getConnection();
    }

    /**
     * @return string
     */
    abstract function getTable();
}

现在我们要定义一个新的存储库:

services.yml

# Repositories
acme_product.repository.product_batch:
    parent: acme_core.generic_repository
    class: Acme\Bundle\ProductBundle\Repository\ProductBatchRepository

Acme\Bundle\ProductBundle\Repository\ProductBatchRepository

<?php

namespace Acme\Bundle\ProductBundle\Repository;

use Acme\Bundle\CoreBundle\Repository\GenericRepository;

/**
 * Class ProductBatchRepository
 * @package Acme\Bundle\ProductBundle\Repository
 */
class ProductBatchRepository extends GenericRepository {
    /**
     * @param int $batchId
     * @return integer The number of affected rows.
     */
    public function deleteBatch($batchId) {
        $table = $this->getTable();

        return $this->getConnection()->delete($table, [
            'id' => $batchId
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function getTable() {
        return 'product_batch';
    }
}

deleteBatch()方法创建并执行以下查询:

DELETE FROM product_batch WHERE id = ?

最后在我们的控制器中:

public function deleteAction() {
    $batchId = $this->getRequest()->get('batchId');

    $affectedRows = $this->get('acme_product.repository.product_batch')->deleteBatch($batchId);

    return $this->render(/**/);
}

For further information and entity manager / connection usage please refer to the official documentation: http://doctrine-orm.readthedocs.org/en/latest/reference/native-sql.html

于 2014-10-28T15:04:19.410 回答
1

我的建议是在构造函数中创建一个带有所需依赖项的普通 PHP 类,并通过服务容器获取它。

于 2012-07-10T10:26:55.117 回答