ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

php-可捕获的致命错误:传递给(…)的参数1必须是给定()整数的实例

2019-11-22 14:30:39  阅读:272  来源: 互联网

标签:fixtures mongodb symfony orm php


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

因此,解决方案是创建电影的固定装置,并将其设置为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灯具中通过引用检索它们之后,它将加载整个对象,而不仅仅是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

您必须对所有其他实体(例如演员,导演)执行此操作.

您可以找到在夹具here之间共享对象(通过参考)的文档.

编辑:

使双向工作,适应奖项制定者:

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;

标签:fixtures,mongodb,symfony,orm,php
来源: https://codeday.me/bug/20191122/2060376.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有