具有学说关系持久性问题的Symfony2形式

时间:2014-02-18 03:40:07

标签: doctrine-orm relationship symfony-forms persist

所以我有3个实体及其属性。 具有多个USERSKILLS的USER,每个用户具有单个相应的SKILL

以下是实体:

/**
 * @ORM\Table(name="skills")
 * @ORM\Entity()
 */
class Skill
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(name="name", type="string", length=30, unique=true)
     */
    private $name;

    /**
     * @ORM\Column(name="active", type="boolean")
     * @var bool
     */
    private $active = false;

    /**
     * @ORM\OneToMany(targetEntity="UserSkill", mappedBy="skill")
     */
    private $userSkills;
}


/**
 * @ORM\Table(name="user_skill")
 * @ORM\Entity()
 */
class UserSkill
{

    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="User", inversedBy="skills")
     * @var User
     */
    private $user;

    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="Skill", inversedBy="userSkills")
     * @var Skill
     */
    private $skill;

    /**
     * @ORM\Column(type="integer")
     * @var int
     */
    private $level = 0;
}

/**
 * StartupDriven\UserBundle\Entity\User
 *
 * @ORM\Table(name="users")
 * @ORM\Entity(repositoryClass="StartupDriven\UserBundle\Entity\UserRepository")
 */
class User implements AdvancedUserInterface, \Serializable
{
    /**
     * @ORM\OneToMany(targetEntity="UserSkill", mappedBy="user")
     * @var ArrayCollection
     */
    private $skills;
}

它们是使用2个symfony表单对象创建的,这些对象非常有效。

问题是当我在控制器中持久保存这些对象时......我收到错误

$form->handleRequest($request);

if ($form->isValid()) {

    /**
     * @var User $user
     */
    $user = $form->getData();

    /**
     * @var UserSkill $userSkill
     */
    foreach ($user->getSkills() as $userSkill) {

        // No updating skills... Only new ones.
        if (!$userSkill->getId()) {

            // Check skill name match.
            if ($matched_skill = $em->getRepository('StartupDrivenUserBundle:Skill')->findOneBy(array('name' => $userSkill->getName()))) {
                $userSkill->setSkill($matched_skill);
            }
            else {

                // No match. Create new generic skill.
                $em->persist($userSkill->getSkill());

                // THE ERROR HAPPENS ON THE FOLLOWING LINE!
                $em->flush();
            }

            // Set the User
            $userSkill->setUser($user);

            // Persist the user skill.
            $em->persist($userSkill);
            $em->flush();
        }
    }

    $em->persist($user);
    $em->flush();

错误讯息: A new entity was found through the relationship 'StartupDriven\UserBundle\Entity\User#skills' that was not configured to cascade persist operations for entity: StartupDriven\UserBundle\Entity\UserSkill@0000000053e8628e00007f7f2fd56e1f. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}). If you cannot find out which entity causes the problem implement 'StartupDriven\UserBundle\Entity\UserSkill#__toString()' to get a clue.

我已经尝试了坚持和放弃的每一种组合。我能想到的同花顺。我已经尝试了上面的级联选项(它给出了一个不同的错误,即“未设置主键规则所需的用户ID”)...我完全迷失了。这似乎是一种如此简单的关系......我哪里出错?

0 个答案:

没有答案