我正在制作夹具,当我尝试加载它们时,我遇到了错误。我需要一个 Movie 对象的实例,但我给出的,我不知道为什么,是一个整数。出于这个原因,它告诉我我有以下错误:
[Symfony'Component'Debug'Exception'ContextErrorException]
Catchable Fatal Error: Argument 1 passed to Filmboot'MovieBundle'Document'A
ward::setMovie() must be an instance of Filmboot'MovieBundle'Document'Movie
, integer given, called in C:'Programming'xampp'htdocs'filmboot.web'src'Fil
mboot'MovieBundle'DataFixtures'MongoDB'Awards.php on line 143 and defined i
n C:'Programming'xampp'htdocs'filmboot.web'src'Filmboot'MovieBundle'Documen
t'Award.php line 107
这是我的夹具类:
namespace Filmboot'MovieBundle'DataFixtures'MongoDB;
use Doctrine'Common'DataFixtures'AbstractFixture;
use Doctrine'Common'DataFixtures'OrderedFixtureInterface;
use Doctrine'Common'Persistence'ObjectManager;
use Filmboot'MovieBundle'Document'Award;
class Awards extends AbstractFixture implements OrderedFixtureInterface {
public function load(ObjectManager $manager) {
$awards = array(
array(
"name" => "Sitges",
"year" => "1992",
"category" => "Best director"
);
foreach ($awards as $award) {
$document = new Award();
$document->setName ($award["name"]);
$document->setYear ($award["year"]);
$document->setCategory($award["category"]);
$manager->persist($document);
$this->addReference("award-" .$i, $award);
}
$manager->flush();
}
public function getOrder() {
return 1;
}
}
这是文档类:
namespace Filmboot'MovieBundle'Document;
use Doctrine'ODM'MongoDB'Mapping'Annotations as ODM;
use Doctrine'Common'Collections'ArrayCollection;
use Filmboot'MovieBundle'Util;
/**
* @ODM'Document(db="filmbootdb", collection="awards")
* @ODM'Document(repositoryClass="Filmboot'MovieBundle'Document'AwardRepository")
*/
class Award {
/**
* @ODM'Id
*/
private $id;
/**
* @ODM'String
*/
private $name;
/**
* @ODM'Int
*/
private $year;
/**
* @ODM'String
*/
private $category;
/**
* @ODM'ReferenceOne(targetDocument="Movie", mappedBy="awards", cascade={"persist"})
*/
private $movie;
public function getId() {
return $this->id;
}
public function setName($name) {
return $this->name = $name;
}
public function getName() {
return $this->name;
}
public function setYear($year) {
return $this->year = $year;
}
public function getYear() {
return $this->year;
}
public function setCategory($category) {
return $this->category = $category;
}
public function getCategory() {
return $this->category;
}
public function setMovie('Filmboot'MovieBundle'Document'Movie $movie) {
$movie->setAward($this);
return $this->movie = $movie;
}
}
正如我们所看到的,你明确地为电影提供了一个整数:
$awards = array(
array(
// ...
"movie" => 1,
),
);
// ...
$document->setMovie ($award["movie"]);
而不是电影对象,所以脚本崩溃,因为它需要一个电影对象:
public function setMovie('Filmboot'MovieBundle'Document'Movie $movie) {
return $this->movie = $movie;
}
因此,解决方案是创建电影的固定装置并给它们一个参考:
// When saving inside Movie fixtures
$manager->persist($movie);
$manager->flush();
$this->addReference('movie-'.$i, $movie); // Give the references as movie-1, movie-2...
然后首先使用 getOrder() 方法加载它们:
public function getOrder()
{
return 0; // 0, loaded first
}
控制电影后加载奖项:
public function getOrder()
{
return 1; // loaded after 0...
}
在您的奖项灯具中通过引用像这样检索它们之后,它将加载整个对象,而不仅仅是一个 id(整数):
$awards = array(
array(
// ...
"movie" => $this->getReference('movie-1'), // Get the Movie object, not just id
),
);
// ...
$document->setMovie ($award["movie"]);
请注意,如果你想使用引用和排序,你的夹具类需要实现 OrderedFixtureInterface :
namespace Acme'HelloBundle'DataFixtures'ORM;
use Doctrine'Common'DataFixtures'AbstractFixture;
use Doctrine'Common'DataFixtures'OrderedFixtureInterface;
use Doctrine'Common'Persistence'ObjectManager;
use Acme'HelloBundle'Entity'Group;
class LoadGroupData extends AbstractFixture implements OrderedFixtureInterface
你必须对所有其他实体(如演员、导演)这样做......
您可以在此处找到有关在灯具之间共享对象(通过参考)的文档。
编辑:
要使双向工作,请调整 奖项设定者:
public function setMovie('Filmboot'MovieBundle'Document'Movie $movie) {
$movie->setAward($this);
return $this->movie = $movie;
}
并通过级联持久性适应持久性:
/**
* @ODM'ReferenceOne(targetDocument="Movie", mappedBy="awards", cascade={"persist"})
*/
private $movie;
因为您收到的错误消息足够清楚。这是您可以解决此错误参数映射问题的方法。
而不是将整数设置为用于填充document
实例的awards
数组的movie
。为什么不只设置一个给定的Movie
实体,你已经坚持了。
为此,您必须加载一个或多个movies
(这取决于您的需求)并将此/那些 entit(y/ies) 设置为参数以填充您的document
实例。
举个例子,看看这个由已经持久化的用户组填充的用户的示例(关于问题)。你可以在这里使用同样的想法。