php - Doctrine 2 重写多对一关联

标签 php doctrine-orm sylius

是否可以覆盖@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/

相关文章:

php - 如何在现有的 Symfony2 应用程序中实现 Sylius OrderBundle

php - Symfony2/Sylius 未检测到 stripe/stripe-php 库

php - 如何将点击的验证码的响应作为变量获取

php - 同时在数据库中插入和更新数据

xml - XML 中的 Doctrine 类继承

php - InvalidArgumentException Synfomy2

西利乌斯 : How to filter products by attributes in Sylius?

php - 我可以安全地删除cpanel中的cagefs/var/cpanel/php/sessions/ea-phpxx(xx是版本)中的 session 吗?

php - 使用 knpmenu bundle 在 symfony 2.1 中创建面包屑

php - 奇怪的学说结果数组结构如果我在 DQL 查询中传递三次与相同的相关实体连接