0

My problem is very simple !

2 entities :

/**
 * @ORM\Entity
 * @ORM\Table(name="tournament")
 */
class Tournament
{
    …
    …
    …

    /**
     * @ORM\OneToMany(targetEntity="Siriru\GSBundle\Entity\Match", mappedBy="tournament")
     */

    …

    protected $matches;

    public function start()
    {
        $this->addMatch(new Match());
    }
}

and

/**
 * @ORM\Entity
 * @ORM\Table(name="match")
 */
class Match
{

    /**
     * @ORM\ManyToOne(targetEntity="Siriru\GSBundle\Entity\Tournament", inversedBy="matches")
     * @ORM\JoinColumn(nullable=false)
     */
    protected $tournament;
}

I need to persist the new Match I created with the start() function. Is it possible ? Or maybe it's a very bad way to do it.

Thanks !!

Siriru

4

1 回答 1

1

添加:cascade={"persist"}到您在课堂上的$matches属性的选项。Tournament现在当你这样做时:

$myTournament->start();
$em->persist($myTournament);
$em->flush();

匹配记录将由 Doctrine 自动在数据库中创建。

于 2014-03-07T12:00:15.147 回答