Php 原则2凌驾于多对一协会之上
是否可以覆盖Php 原则2凌驾于多对一协会之上,php,doctrine-orm,sylius,Php,Doctrine Orm,Sylius,是否可以覆盖@manytone(targetEntity) 我读了,但它没有提到如何覆盖targetEntity 这是我的密码: 名称空间AppBundle\Model\Order\Entity; 使用AppBundle\Model\Base\Entity\Identifier; 使用AppBundle\Model\Base\Entity\Product; 使用条令\ORM\Mapping作为ORM; 使用条令\ORM\Mapping\AttributeOverrides; 使用条令\ORM\M
@manytone(targetEntity)
我读了,但它没有提到如何覆盖targetEntity
这是我的密码:
名称空间AppBundle\Model\Order\Entity;
使用AppBundle\Model\Base\Entity\Identifier;
使用AppBundle\Model\Base\Entity\Product;
使用条令\ORM\Mapping作为ORM;
使用条令\ORM\Mapping\AttributeOverrides;
使用条令\ORM\Mapping\AttributeOverride;
/**
*类OrderItem
*
*
*@ORM\Entity
*@ORM\Table(name=“sylius\u order\u item”)
*@ORM\AssociationOverrides({
*@ORM\AssociationOverride(
*name=“variant”,
*joinColumns=@ORM\JoinColumn(
*name=“variant”,referencedColumnName=“id”,nullable=true
* )
* )
* })
*/
类OrderItem扩展\Sylius\Component\Core\Model\OrderItem
{
/**
*@var
*@ORM\manytone(targetEntity=“AppBundle\Model\Base\Entity\Product”)
*/
受保护的产品;
/**
*@返回混合
*/
公共函数getProduct()
{
返回$this->product;
}
/**
*@param混合$product
*/
公共函数集合产品($product)
{
$this->product=$product;
}
}
我可以覆盖“variant”列的定义,并将该列设置为null,但我不知道如何更改
targetEntity
如文档中所述,您不能更改关联的类型:
但是,您可以将targetEntity定义为接口(这似乎是默认的sylius配置)
扩展接口文件中的原始文件
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
这里描述的是:
希望它有帮助在使用注释时,我没有找到覆盖关联的
targetEntity
值的方法,但是在使用PHP映射时(在Symfony中PHP
或staticphp
是可能的)
我们可以创建一个函数:
function updateAssociationTargetEntity(ClassMetadata $metadata, $association, $targetEntity)
{
if (!isset($metadata->associationMappings[$association])) {
throw new \LogicException("Association $association not defined on $metadata->name");
}
$metadata->associationMappings[$association]['targetEntity'] = $targetEntity;
}
然后像这样使用它:
updateAssociationTargetEntity($metadata, 'product', AppBundle\Model\Base\Entity\Product::class);
这样,当条令加载类AppBundle\Model\Order\Entity\OrderItem
的元数据时,它首先加载父(\Sylius\Component\Core\Model\OrderItem
)元数据,然后它加载子类的PHP映射,在这里我们覆盖了在加载父类元数据时设置的关联映射
updateAssociationTargetEntity($metadata, 'product', AppBundle\Model\Base\Entity\Product::class);