Doctrine Fixtures上的双向引用

时间:2013-11-13 12:03:37

标签: symfony doctrine-orm fixtures bidirectional

我有一个moviesawards的数据库。关键是我想要的是能够知道奖项的电影和电影的奖项。当我尝试加载电影或奖励时,问题就开始了。当我加载其中一个时,我有一个错误,例如:undefined award-1或反之(undefined movie-1),因为尚未创建引用。

我的奖励灯具:

namespace MyProject\MovieBundle\DataFixtures\MongoDB;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;

use MyProject\MovieBundle\Document\Award;

class Awards extends AbstractFixture implements OrderedFixtureInterface {
    public function load(ObjectManager $manager) {
        $awards = array(
            array(
                "name"     => "Sitges",
                "year"     => "1992",
                "category" => "Best director"
                "movie"    => $this->getReference("movie-1"),
            array(
                "name"     => "Toronto''s festival",
                "year"     => "1992",
                "category" => "FIPRESCI award",
                "movie"    => $this->getReference("movie-1")
        );

        foreach ($awards as $i => $award) {
            $i++;
            $document = new Award();
            $document->setName    ($award["name"]);
            $document->setYear    ($award["year"]);
            $document->setCategory($award["category"]);

            $manager->persist($document);
            $this->addReference("award-" . $i, $document);
        }

        $manager->flush();
    }

    public function getOrder() {
        return 1;
    }
}

电影灯具:

namespace Filmboot\MovieBundle\DataFixtures\MongoDB;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;

use Filmboot\MovieBundle\Document\Movie;

class Movies extends AbstractFixture implements OrderedFixtureInterface {
    public function load(ObjectManager $manager) {
        $movies = array(
            array(
                "title"      => "Reservoir Dogs",
                "duration"   => "95",
                "year"       => "1992",
                "country"    => "USA",
                "producer"   => "Live Entertainment, Dog Eat Dog Productions Inc.",
                "image"      => "reservoirdogs.png",
                "largeImage" => "reservoirdogsLarge.png",
                "awards"     => [$this->getReference("award-1"), $this->getReference("award-2")
        );

        foreach ($movies as $i => $movie) {
            $i++;
            $document = new Movie();
            $document->setTitle     ($movie["title"]);
            $document->setDuration  ($movie["duration"]);
            $document->setyear      ($movie["year"]);
            $document->setProducer  ($movie["producer"]);
            $document->setImage     ($movie["image"]);
            $document->setLargeImage($movie["largeImage"]);

            foreach ($movie["awards"] as $award)         {
                $document->addAward($award);
            }
           $manager->persist($document);
           $manager->flush();

           $this->addReference("movie-".$i, $document);
        }
    }

    public function getOrder() {
        return 1;
    }
}   

1 个答案:

答案 0 :(得分:3)

我试图在这里第二次解释:https://stackoverflow.com/a/19904128/875519是你只需要先加载电影,然后奖励,然后只在奖励设备中链接电影,而不是两者。不要在电影设备中链接奖项,因为在加载电影设备时,不会创建奖励,因此脚本会崩溃......

通过向奖励对象添加电影,这足以创建两个对象之间的关系,您可以调用$ movie-> getAwards()和$ award-> getMovie()!

所以,课堂电影,删除这些行:

"awards"     => [$this->getReference("award-1"), $this->getReference("award-2")

//...

foreach ($movie["awards"] as $award) {
      $document->addAward($award);
}

并将顺序设置为0(在1之前,必须在奖励之前加载)

public function getOrder() {
    return 0; // Load before awards
}

班级奖励,不要因为您目前与电影建立关系而改变任何内容,请确保通过将订单设置为1来加载电影奖项:

public function load(ObjectManager $manager) {

    $awards = array(
        array(
            "name"     => "Sitges",
            "year"     => "1992",
            "category" => "Best director"
            "movie"    => $this->getReference("movie-1"), // Link to a movie
        array(
            "name"     => "Toronto''s festival",
            "year"     => "1992",
            "category" => "FIPRESCI award",
            "movie"    => $this->getReference("movie-1") // Link to a movie
    );

    // ...
}

public function getOrder() {
     return 1; // Load after movies
}

奖项和电影之间的关系将被保存,在数据库领域,movie_id(奖励类)将完成!

因此,您不必在两个灯具中添加关系。选择一个将在另一个之后加载的fixture类,并在这个选择的类中添加关系对象,而不是在两者中,这足以创建两个对象之间的关系,并将解决您的问题!