gpt4 book ai didi

php - 可捕获的 fatal error : Argument 1 passed to (. ..) 必须是给定的 (...) 整数的实例

转载 作者:可可西里 更新时间:2023-11-01 09:36:46 28 4
gpt4 key购买 nike

我正在制作固定装置,当我尝试加载它们时出现错误。我需要一个 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

这是我的 Fixture 类:

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

所以解决方案是创建电影的固定装置并给它们一个 reference :

// 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...
}

并且在您的 Award fixtures 中通过引用检索它们之后,它将加载整个对象,而不仅仅是一个 id(整数):

$awards = array(
array(
// ...
"movie" => $this->getReference('movie-1'), // Get the Movie object, not just id
),
);

// ...

$document->setMovie ($award["movie"]);

请注意,如果您想使用引用和顺序,您的 fixture 类需要实现 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

你必须对所有其他实体(如 Actor 、导演...)执行此操作

您可以找到有关在灯具之间共享对象(通过引用)的文档 here .

编辑:

为了双向工作,调整奖励 setter :

public function setMovie(\Filmboot\MovieBundle\Document\Movie $movie)   {
$movie->setAward($this);
return $this->movie = $movie;
}

并使用 cascade persist 调整持久性:

/**
* @ODM\ReferenceOne(targetDocument="Movie", mappedBy="awards", cascade={"persist"})
*/
private $movie;

关于php - 可捕获的 fatal error : Argument 1 passed to (. ..) 必须是给定的 (...) 整数的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19903315/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com