2

我正在尝试实现一个插件来将销售代表数据添加到我的商店并将这些数据与用户相关联。

在这种情况下(用户和销售代表),我有:

sales_rep- 销售代表表 sales_rep_user- 用户和销售代表之间的关系

第一个对于swg_sales_repswg_sales_rep_user关系(OneToMany)我可以毫无问题地创建它

SwgSalesRepresentative.php

...
**
 * @ORM\Entity
 * @ORM\Table(name="swg_sales_rep")
 */
class SwgSalesRepresentative extends ModelEntity
{
    ...
/**
     * INVERSE SIDE
     *
     * @var \Doctrine\Common\Collections\ArrayCollection
     *
     * @ORM\OneToMany(
     *      targetEntity="Shopware\CustomModels\SwagUserSalesRepresentative\SwgSalesRepresentative",
     *      mappedBy="salesRepresentative",
     *      orphanRemoval=true
     * )
     */
    protected $salesRepresentativeUsers;

...

SwgSalesRepresentativeUsers.php

/**
 * @ORM\Entity
 * @ORM\Table(name="swg_sales_rep_users")
 */
class SwgSalesRepresentativeUsers extends ModelEntity
{

...

/**
     *
     * @ORM\ManyToOne(targetEntity="Shopware\CustomModels\SwagUserSalesRepresentative\SwgSalesRepresentative")
     * @ORM\JoinColumn(name="sales_rep_id", referencedColumnName="id")
     */
    protected $salesRepresentative;

/**
     * @return mixed
     */
    public function getSalesRepresentative()
    {
        return $this->salesRepresentative;
    }

    /**
     * @param $salesRepresentative
     * @return ModelEntity
     */
    public function setSalesRepresentative($salesRepresentative)
    {
        return $this->setManyToOne(
            $salesRepresentative,
            '\Shopware\CustomModels\SwagUserSalesRepresentative\SwgSalesRepresentative',
            'salesRepresentativeUsers'
        );
    }

安装后,我用外键确定我的表。

swg_sales_rep_user对于和s_user(OneToOne)之间的关系,我有问题。我的第一个想法是扩展User模型并添加我们需要的额外逻辑。但这意味着要覆盖我的用户表,冒丢失数据的风险。我所做的是创建一个SwgUser扩展用户模型的模型,比如

SwgSalesRepresentativeUsers.php

/**
 * @ORM\Entity
 * @ORM\Table(name="swg_sales_rep_users")
 */
class SwgSalesRepresentativeUsers extends ModelEntity
{

...

    /**
     * @var \Shopware\CustomModels\SwagUserSalesRepresentative\SwgUser $user
     *
     * @ORM\OneToOne(targetEntity="Shopware\CustomModels\SwagUserSalesRepresentative\SwgUser", inversedBy="salesRepresentative")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    protected $user;

 /**
     * @return mixed
     */
    public function getUser()
    {
        return $this->user;
    }

    /**
     * @param $user
     * @return ModelEntity
     */
    public function setUser($user)
    {
        return $this->setOneToOne(
            $user,
            '\Shopware\CustomModels\SwagUserSalesRepresentative\SwgUser',
            'user',
            'salesRepresentative'
        );
    }

...

SwgUser.php

/**
 * @ORM\Entity
 * @ORM\Table(name="s_user")
 */
class SwgUser extends User
{

    /**
     *
     * @ORM\OneToOne(targetEntity="Shopware\CustomModels\SwagUserSalesRepresentative\SwgSalesRepresentativeUsers", mappedBy="user")
     */
    protected $salesRepresentative;
 ...

安装bootstrap.php/卸载看起来像

 /**
     * Install method
     *
     * @return bool
     */
    public function install()
    {
        $this->updateSchema();

        return true;
    }

    /**
     * Uninstall method
     *
     * @return bool
     */
    public function uninstall()
    {
        $this->registerCustomModels();

        $em = $this->Application()->Models();
        $tool = new \Doctrine\ORM\Tools\SchemaTool($em);

        $classes = array(
            $em->getClassMetadata('Shopware\CustomModels\SwagUserSalesRepresentative\SwgSalesRepresentative'),
            $em->getClassMetadata('Shopware\CustomModels\SwagUserSalesRepresentative\SwgUser'),
            $em->getClassMetadata('Shopware\CustomModels\SwagUserSalesRepresentative\SwgSalesRepresentativeUsers')
        );

        $tool->dropSchema($classes);

        return true;
    }


    /**
     * Creates the database scheme from existing doctrine models.
     *
     * Will remove the table first, so handle with care.
     */
    protected function updateSchema()
    {
        $this->registerCustomModels();

        $em = $this->Application()->Models();
        $tool = new \Doctrine\ORM\Tools\SchemaTool($em);

        $classes = array(
            $em->getClassMetadata('Shopware\CustomModels\SwagUserSalesRepresentative\SwgSalesRepresentative'),
            $em->getClassMetadata('Shopware\CustomModels\SwagUserSalesRepresentative\SwgUser'),
            $em->getClassMetadata('Shopware\CustomModels\SwagUserSalesRepresentative\SwgSalesRepresentativeUsers')
        );

        try {
            $tool->dropSchema($classes);
        } catch (Exception $e) {
            //ignore
        }
        $tool->createSchema($classes);
    }

我尝试使用单向关联映射,它创建了字段,但没有创建与s_user表的关系(外键)。

所以问题是,如何在不重新创建(删除/创建)核心表的情况下创建与商店用品上的核心表的关系?是否可以以编程方式更改表?满足这些需求的最佳方法是什么。你有一个例子来证明这一点吗?

感谢您的帮助。

4

1 回答 1

1

目前还没有办法创建与 shopware 核心表的双向关联。您肯定可以拥有单向关联,但到目前为止,您将无法将关系属性添加到核心实体。

除非您打算修改 shopware 核心本身,否则应随时避免。

唯一(也是非常微小)的可能性是尝试在核心实体属性表上创建关系,这在商店用品中是相当“神奇的东西”。

于 2016-02-09T19:01:58.003 回答