如何从反面编辑/更新Symfony 3中的多对多关系 [英] How to edit/update a many to many relationship in Symfony 3 from the inverse side

查看:78
本文介绍了如何从反面编辑/更新Symfony 3中的多对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个bash脚本,该脚本使用一些工具和Symfony 3 bin/console原理创建一个简单的项目,从零到CRUD":generate:* 它工作正常,但在M:N关联的情况下,我无法反面更新数据. 我在这里阅读了一些答案,并开始了一些测试,但是我对"cascade = {"all"}"选项,'by_reference'=> false和其他建议感到困惑. 从官方文档的基本示例开始,最简单的方法是什么?

I'm working on a bash script that create a simple project "from zero to CRUD" with some tool and Symfony 3 bin/console doctrine:generate:* It works fine but in the M:N association case i can't update data from the inverse side. I read some answers here and i started some tests but i'm confused among "cascade={"all"}" option, 'by_reference' => false and other suggests. What is the simplest way to do that starting from this basic example taken from the offical doctrine docs?

    /** @Entity */
class User {
    // ...

    /**
     * Many Users have Many Groups.
     * @ManyToMany(targetEntity="Group", inversedBy="users")
     */
    private $groups;

    public function __construct() {
        $this->groups = new \Doctrine\Common\Collections\ArrayCollection();
    }

    // ...
}

/** @Entity */
class Group {

    // ...
    /**
     * Many Groups have Many Users.
     * @ManyToMany(targetEntity="User", mappedBy="groups")
     */
    private $users;

    public function __construct() {
        $this->users = new \Doctrine\Common\Collections\ArrayCollection();
    }

    // ...
}

推荐答案

下面是Symfony和Doctrine中多对多关系的完整示例.我通常使用yaml而不是注释.因此,如果要使用注释,则必须使用注释转换代码.

Here's a complete example of a many-to-many relationship in Symfony and Doctrine. I usually use yaml instead of annotations. So you'll have to convert yourself the code using annotations if you want using annotations.

# AppBundle/Entity/User.php
//the id + more fields here if needed
//getters and setters for the other fields

/**
 * @var \Doctrine\Common\Collections\Collection
 */
private $groups;

public function __construct()
{
    $this->groups = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * Add group
 *
 * @param \AppBundle\Entity\Group $group
 *
 * @return User
 */
public function addGroup(\AppBundle\Entity\Group $group)
{
    if ($this->groups->contains($group)) {
        return;
    }

    //those two lines of code are the one you are seeking for, for saving both the owning side and the inverse side
    $this->groups[] = $group;
    $group->addUser($this);

    return $this;
}

/**
 * Remove group
 *
 * @param \AppBundle\Entity\Group $group
 */
public function removeGroup(\AppBundle\Entity\Group $group)
{
    if (!$this->groups->contains($group)) {
        return;
    }

    //those are the lines for removing the owning side and the inverse side
    $this->groups->removeElement($group);
    $group->removeUser($this);
}

/**
 * Get groups
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getGroups()
{
    return $this->groups;
}

# AppBundle/Resources/config/doctrine/User.orm.yml
AppBundle\Entity\User:
type: entity
table: users
repositoryClass: AppBundle\Repository\UserRepository
id:
    id:
        type: integer
        id: true
        generator:
            strategy: AUTO
manyToMany:
    groups:
        targetEntity: AppBundle\Entity\Group
        inversedBy: users
        joinTable:
            name: groups_users
            joinColumns:
                user_id:
                    referencedColumnName: id
                    nullable: true
            inverseJoinColumns:
                group_id:
                    referencedColumnName: id
        cascade: ['persist', 'remove']
        fetch: EAGER
fields:
    # more fields here if needed
lifecycleCallbacks: {  }

# AppBundle/Entity/Group.php
//the id + more fields here if needed
//getters and setters for the other fields

/**
 * @var \Doctrine\Common\Collections\Collection
 */
private $users;

public function __construct()
{
    $this->users = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * Add user
 *
 * @param \AppBundle\Entity\User $user
 *
 * @return Group
 */
public function addUser(\AppBundle\Entity\User $user)
{
    if ($this->users->contains($user)) {
        return;
    }

    //these lines saves both the inverse side and the owning side
    $this->users[] = $user;
    $user->addGroup($this);

    return $this;
}

/**
 * Remove user
 *
 * @param \AppBundle\Entity\User $user
 */
public function removeUser(\AppBundle\Entity\User $user)
{
    if (!$this->users->contains($user)) {
        return;
    }

    //these lines remove both the inverse side and the owning side
    $this->users->removeElement($user);
    $user->removeGroup($this);
}

/**
 * Get users
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getUsers()
{
    return $this->users;
}

# AppBundle/Resources/config/doctrine/Group.orm.yml
AppBundle\Entity\Group:
type: entity
table: groups
repositoryClass: AppBundle\Repository\GroupRepository
id:
    id:
        type: integer
        id: true
        generator:
            strategy: AUTO
manyToMany:
    users:
        targetEntity: AppBundle\Entity\User
        mappedBy: groups
        cascade: ['persist']
        fetch: EAGER
fields:
    # more fields here if needed
lifecycleCallbacks: {  }

我希望我没有犯任何错误,因为我在您的示例中改了我本地的Category-Product示例.

I hope I didn't made any mistake, as I adapted my local Category - Product example on yours.

接下来,您需要在每种表单类型中都有一个字段,以便能够为用户选择组,并为组选择用户.

Next, you need to have, in each form type, a field to be able to select groups for a user, and users for a group.

# AppBundle/Form/UserType.php
$builder
    ->add('name') // this is a field I used in my local example, you can add yours
    ->add('groups', EntityType::class, [
        'class' => 'AppBundle:Group',
        'placeholder' => 'Choose a Group',
        'query_builder' => function (EntityRepository $er) {
            return $er->createQueryBuilder('g')
                ->orderBy('g.name', 'DESC');
        },
        'choice_label' => 'name',
        'multiple'=>true,
        'expanded'=>false,
        'by_reference' => false,
    ])

# AppBundle/Form/GroupType.php
$builder
    ->add('name')
    ->add('users', EntityType::class, [
        'class' => 'AppBundle:User',
        'placeholder' => 'Choose User',
        'query_builder' => function (EntityRepository $er) {
            return $er->createQueryBuilder('u')
                ->orderBy('u.name', 'DESC');
        },
        'choice_label' => 'name',
        'multiple'=>true,
        'expanded'=>false,
        'by_reference' => false,
    ])

在更新架构之前,您需要创建两个实体,没有任何映射.然后更新架构,然后继续在实体之间创建多对多关系,并再次更新架构以应用该关系.试试看,让我们知道它是否有效.

Before updating the schema, you need to create both your entities, without any mapping. Then update the schema, then proceed with creating the many-to-many relation between the entities, and update the schema once again, to apply the relation. Give it a try, and let us know if it worked.

这篇关于如何从反面编辑/更新Symfony 3中的多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆