1

我有一个带有moviesand的数据库awards。关键是我想要的是能够知道一个奖项的电影和一个电影的奖项。当我尝试加载电影或奖项时,问题就开始了。当我加载其中一个时,我有一个错误,例如: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;
    }
}   
4

1 回答 1

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);
}

并将 order 设置为 0(在 1 之前,必须在 Awards 之前加载)

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

类奖项,当您当前与电影建立关系时不要更改任何内容,请确保通过将 order 设置为 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(奖项类)中完成!

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

于 2013-11-17T17:32:27.250 回答