是否可以覆盖@ManyToOne(targetEntity)
?
我读了this Doctrine documentation page ,但它没有提到如何覆盖 targetEntity
。
这是我的代码:
namespace AppBundle\Model\Order\Entity;
use AppBundle\Model\Base\Entity\Identifier;
use AppBundle\Model\Base\Entity\Product;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\AttributeOverrides;
use Doctrine\ORM\Mapping\AttributeOverride;
/**
* Class OrderItem
*
*
* @ORM\Entity
* @ORM\Table(name="sylius_order_item")
* @ORM\AssociationOverrides({
* @ORM\AssociationOverride(
* name="variant",
* joinColumns=@ORM\JoinColumn(
* name="variant", referencedColumnName="id", nullable=true
* )
* )
* })
*/
class OrderItem extends \Sylius\Component\Core\Model\OrderItem
{
/**
* @var
* @ORM\ManyToOne(targetEntity="AppBundle\Model\Base\Entity\Product")
*/
protected $product;
/**
* @return mixed
*/
public function getProduct()
{
return $this->product;
}
/**
* @param mixed $product
*/
public function setProduct($product)
{
$this->product = $product;
}
}
我能够覆盖“变体”列的定义并将此列设置为空,但我不知道如何更改 targetEntity
。
最佳答案
如文档中所述,您无法更改关联类型: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html#association-override
但是,您可以将 targetEntity 定义为接口(interface)(这似乎是默认的 sylius conf),
targetEntity="AppBundle\Entity\ProductInterface"
扩展接口(interface)文件中的原始接口(interface)
namespace AppBundle\Entity;
use Sylius\Component\Core\Model\ProductInterface as BaseProductInterface;
interface ProductInterface extends BaseProductInterface {}
并在你的配置中添加映射
doctrine:
orm:
resolve_target_entities:
AppBundle\Entity\ProductInterface: AppBundle\Entity\Product
此处描述:http://symfony.com/doc/current/doctrine/resolve_target_entity.html
希望对你有帮助
关于php - Doctrine 2 重写多对一关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39598654/