我有两个bundle,我想在一个bundle中覆盖另一个bundle的存储库:
我有一个源bundle:SourceBundle。 我有我的覆盖bundle:OverrideBundle。
首先,在OurVendorOverrideBundle.php文件中,我添加了以下内容:
public function getParent()
{
return 'SomeVendorSourceBundle';
}
接着我想为“SourceBundle”实体的存储库添加自定义方法。源实体是Response.php,其存储库为ResponseRepository.php。
所以我做了以下步骤:
<?php
namespace OurVendor\OverrideBundle\Repository;
use Doctrine\ORM\EntityRepository;
use SomeVendor\SourceBundle\Repository\ResponseRepository as BaseRepository;
class ResponseRepository extends BaseRepository
{
/**
*
* @return array
*/
public function getQueryExerciseAllResponsesForAllUsers($exoId)
{
$qb = $this->createQueryBuilder('r');
$qb->join('r.paper', 'p')
->join('p.exercise', 'e')
->where('e.id = ?1')
->andWhere('p.interupt = ?2')
->setParameters(array(1 => $exoId, 2 => 0));
return $qb->getQuery();
}
}
如果我没有在OverrideBundle中设置实体,就会出现以下错误:
The autoloader expected class "CPASimUSante\ExoverrideBundle\Entity\Response" to be defined in file "/home/www/myproject/vendor/ourvendor/override-bundle/OurVendor/OverrideBundle/Entity/Response.php". The file was found but the class was not in it, the class name or namespace probably has a typo.
SourceBundle实体是:
<?php
namespace SomeVendor\SourceBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* SomeVendor\SourceBundle\Entity\Response
*
* @ORM\Entity(repositoryClass="SomeVendor\SourceBundle\Repository\ResponseRepository")
* @ORM\Table(name="source_response")
*/
class Response
{
...
}
所以我在OverrideBundle中添加了实体:
<?php
namespace OurVendor\OverrideBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use SomeVendor\SourceBundle\Entity\Response as BaseEntity;
/**
* SomeVendor\SourceBundle\Entity\Response
*
* @ORM\Entity(repositoryClass="OurVendor\OverrideBundle\Repository\ResponseRepository")
* @ORM\Table(name="override_response")
*/
class Response extends BaseEntity
{
public function __construct()
{
parent::__construct();
}
}
但是我遇到了这个错误:
An exception occurred while executing 'SELECT u0_.id AS id0, u0_.ip AS ip1, u0_.mark AS mark2, u0_.nb_tries AS nb_tries3, u0_.response AS response4, u0_.paper_id AS paper_id5, u0_.interaction_id AS interaction_id6 FROM ujm_exoverride_response u1_ INNER JOIN ujm_paper u2_ ON u0_.paper_id = u2_.id INNER JOIN ujm_exercise u3_ ON u2_.exercise_id = u3_.id WHERE u3_.id = ? AND u2_.interupt = ?' with params ["1", 0]:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'u0_.id' in 'field list'
这似乎表明找不到表中的字段。因此我进行了更改,改成了:
<?php
namespace OurVendor\OverrideBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use SomeVendor\SourceBundle\Entity\Response as BaseEntity;
/**
* SomeVendor\SourceBundle\Entity\Response
*
* @ORM\Entity(repositoryClass="OurVendor\OverrideBundle\Repository\ResponseRepository")
* @ORM\Table(name="source_response")
*/
class Response extends BaseEntity
{
public function __construct()
{
parent::__construct();
}
}
并且它起作用了。
...但是当我重新安装捆绑包以测试是否一切正常时,我遇到了致命错误,指出source_response已经定义(确实如此)。
那么我该怎么办呢?
我还阅读过,除非源扩展了MappedSuperclass,否则无法覆盖实体,在我的情况下不是这样。
但是,如果我只想覆盖其存储库,则注定要失败吗? 是否可能? 如果可以,正确的方法是什么?
如果我完全删除覆盖实体的注释,我就有了:
Class "OurVendor\OverrideBundle\Entity\Response" sub class of "SomeVendor\SourceBundle\Entity\Response" is not a valid entity or mapped super class.
500 Internal Server Error - MappingException