关于教义装置的双向参考 [英] Bidirectional references on Doctrine Fixtures

查看:159
本文介绍了关于教义装置的双向参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库,其中包含电影奖励。关键是我想要的是能够获得电影奖和电影奖。当我尝试加载电影或奖励时,问题就开始了。当我加载其中一个时,我有一个错误,例如: undefined award-1 或反对(未定义的电影-1 ),因为这些参考文献尚未创建。

I have a db with movies and awards. The point is what I want is be able to know the movie of an award and the awards of a movie. The problem begins when I try to load a movie or an award. When I load one of them I have an error that says, for example: undefined award-1 or viceversa (undefined movie-1) because the references aren't created yet.

我的奖项 Fixture:

My Awards Fixture:

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


推荐答案

我第二次尝试解释: https://stackoverflow.com/a/19904128/875519 是你只需要先加载电影,然后加入奖励,并且只需在Award灯具中连接电影,而不是两者兼容。不要在电影院中链接奖项,因为在加载电影设备时,不会创建奖励,因此脚本会崩溃。

What I tried to explain in a second time here : https://stackoverflow.com/a/19904128/875519 is that you just need first to load Movies, then Awards, and just link movies in Award fixtures, not both. Don't link Awards in movie fixtures, because when loading movie fixtures, awards will not be created, and so the script will crash...

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

By adding movie to an award object, this is sufficient to create the relation between both object and you'll be able call $movie->getAwards() and $award->getMovie() !

所以,电影类,删除这些行:

So, class Movies, remove these lines :

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

//...

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

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

And set order to 0 (coming before 1, have to be loaded before Awards)

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

类奖励,不要像现在一样改变创建与电影的关系,确保您通过设置订单1之后加载奖项:

class Awards, don't change nothing as you currently create relations with movies, make sure you load Awards after Movies by setting order to 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
}

en奖励和电影将被保存,并在数据库领域movie_id(获奖类)将完成!

Relations between awards and movies will be saved and in the databases field movie_id (of Award class) will be completed !

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

So, you don't have to add relation in both fixtures. Choose one fixture class that will be loaded after the other one, and add the relation object in this chosen class, not in both, this is sufficient to create the relation between both objects and will solved your problem !

这篇关于关于教义装置的双向参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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