【问题标题】:Getting fixtures to load after creating a join table entity在创建连接表实体后获取要加载的装置
【发布时间】:2014-10-26 16:26:01
【问题描述】:

我最近问了以下问题:

Many to Many, One to Many or Many to One

在完成@inanzzz 建议的更新后,我的数据库现在看起来很棒,但是我的固定装置似乎无法正常工作。尝试加载它们时出现以下错误:

[Symfony\Component\Debug\Exception\ContextErrorException]
Catchable Fatal Error: Argument 1 passed to BBQ\Charcoal\WebsiteBundle\Enti
ty\Templates::addModule() must be an instance of BBQ\Charcoal\WebsiteBundle
\Entity\TemplateModules, instance of Proxies\__CG__\BBQ\Charcoal\WebsiteBun
dle\Entity\Modules given, called in /Users/alexward/dev/symphony/symphony20
14/www/src/BBQ/Charcoal/WebsiteBundle/DataFixtures/ORM/TemplateFixtures.php
 on line 19 and defined in /Users/alexward/dev/symphony/symphony2014/www/sr
c/BBQ/Charcoal/WebsiteBundle/Entity/Templates.php line 141

templates.php 的第 141 行是:

/**
 * Add modules
 *
 * @param \BBQ\Charcoal\WebsiteBundle\Entity\TemplateModules $modules
 * @return Templates
 */
public function addModule(\BBQ\Charcoal\WebsiteBundle\Entity\TemplateModules $modules)
{
    $this->modules[] = $modules;

    return $this;
}

这是我的实体类:

class Modules
{
/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\Column(type="string", length=10)
 */
protected $type;

 /**
 * @ORM\Column(type="string")
 */
protected $name;

 /**
 * @ORM\Column(type="string")
 */
protected $friendly_name;

/**
 * @ORM\Column(type="text")
 */
protected $description;

/**
 * @ORM\Column(type="integer")
 */
protected $max_limit;

/**
 * @ORM\Column(type="integer")
 */
protected $min_limit;

/**
 * @var ArrayCollection
 * @ORM\OneToMany(targetEntity="TemplateModules", mappedBy="modules")
 */
private $templates;

/**
 * @var ArrayCollection
 * @ORM\OneToMany(targetEntity="Content", mappedBy="modules")
 */
private $content;

public function __construct() {
    $this->templates = new \Doctrine\Common\Collections\ArrayCollection();
    $this->content = new \Doctrine\Common\Collections\ArrayCollection();
}
}


class TemplateModules {
/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

 /**
 * @ORM\ManyToOne(targetEntity="Templates", inversedBy="modules")
 */
protected $templates;

/**
 * @ORM\ManyToOne(targetEntity="Modules", inversedBy="templates")
 */
protected $modules;

/**
 * @ORM\Column(type="integer")
 */
private $order_by;
}

class Templates {
/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

 /**
 * @ORM\Column(type="string")
 */
protected $name;

 /**
 * @ORM\Column(type="string")
 */
protected $friendly_name;

/**
 * @var ArrayCollection
 * @ORM\OneToMany(targetEntity="Pages", mappedBy="template")
 */
private $pages;

/**
 * @ORM\OneToMany(targetEntity="TemplateModules", mappedBy="templates")
 **/
protected $modules;

public function __construct() {
    $this->pages = new \Doctrine\Common\Collections\ArrayCollection();
    $this->modules = new \Doctrine\Common\Collections\ArrayCollection();
}
}

以下是我的两个部分的固定装置:

模块:

<?php

namespace BBQ\Charcoal\WebsiteBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use BBQ\Charcoal\WebsiteBundle\Entity\TemplateModules;
use BBQ\Charcoal\WebsiteBundle\Entity\Modules;

class ModulesFixtures extends AbstractFixture implements OrderedFixtureInterface
{
public function load(ObjectManager $manager)
{
    $module1 = new Modules();
    $module1->setName('info_box_three_up');
    $module1->setFriendlyName('Information Module (3 to a row)');
    $module1->setDescription('Information Module (3 to a row)');
    $module1->setMinLimit(1);
    $module1->setMaxLimit(1);
    $module1->setType('row');
    $manager->persist($module1);

    $module2 = new Modules();
    $module2->setName('cta_single_line_text');
    $module2->setFriendlyName('Single line CTA with button');
    $module2->setDescription('Single line CTA with button');
    $module2->setMinLimit(1);
    $module2->setMaxLimit(1);
    $module2->setType('row');
    $manager->persist($module2);

    $module3 = new Modules();
    $module3->setName('video');
    $module3->setFriendlyName('Video panel');
    $module3->setDescription('Video panel');
    $module3->setMinLimit(2);
    $module3->setMaxLimit(2);
    $module3->setType('row');
    $manager->persist($module3);

    $module4 = new Modules();
    $module4->setName('stories_two_up');
    $module4->setFriendlyName('Stories (2)');
    $module4->setDescription('Stories (2)');
    $module4->setMinLimit(2);
    $module4->setMaxLimit(2);
    $module4->setType('row');
    $manager->persist($module4);

    $module5 = new Modules();
    $module5->setName('cta_multi_line_text');
    $module5->setFriendlyName('Multiple line CTA with button');
    $module5->setDescription('Multiple line CTA with button');
    $module5->setMinLimit(1);
    $module5->setMaxLimit(1);
    $module5->setType('row');
    $manager->persist($module5);

    $manager->flush();

    $this->addReference('module-1', $module1);
    $this->addReference('module-2', $module2);
    $this->addReference('module-3', $module3);
    $this->addReference('module-4', $module4);
    $this->addReference('module-5', $module5);
}

public function getOrder()
{
    return 1;
}

}

模板:

<?php

namespace BBQ\Charcoal\WebsiteBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use BBQ\Charcoal\WebsiteBundle\Entity\Templates;
use BBQ\Charcoal\WebsiteBundle\Entity\Modules;
use BBQ\Charcoal\WebsiteBundle\Entity\TemplateModules;

class TemplatesFixtures extends AbstractFixture implements OrderedFixtureInterface
{
public function load(ObjectManager $manager)
{
    $template1 = new Templates();
    $template1->setName('T1_Homepage');
    $template1->setFriendlyName('Homepage Template');
    $template1->addModule($this->getReference('module-1'));
    $template1->addModule($this->getReference('module-2'));
    $template1->addModule($this->getReference('module-3'));
    $template1->addModule($this->getReference('module-4'));
    $template1->addModule($this->getReference('module-5'));

    $manager->persist($template1);

    $manager->flush();

    $this->addReference('template-1', $template1);
}

public function getOrder()
{
    return 2;
}
}

【问题讨论】:

    标签: symfony doctrine-orm fixtures


    【解决方案1】:

    这应该是这样的,因为您的表/方法/实体需要一个特定的对象,而不是其他东西,比如整数,另一个对象等等。

    ->addModule(instanbe of TemplateModules is expected here)
    but you have given
    ->addModule(instance of Modules is expected here)
    

    如果您说绝对应该是@​​987654322@,那么您可能设置了错误的关系或类似的东西。您可能需要检查关系。

    编辑:

    看看你的代码和我的代码之间的区别。您的注释中没有 JoinColumn 位。我以学生和课程为例。

    class Student
    class Modules
    {
        /**
         * @ORM\OneToMany(targetEntity="TemplateModules", mappedBy="modules")
         * 
         * @ORM\OneToMany(targetEntity="StudentCourse", mappedBy="studentMap")
         */
        private $templates;
        protected $studentInverse;
    
        public function __construct() {
            $this->templates = new \Doctrine\Common\Collections\ArrayCollection();
            $this->studentInverse = new \Doctrine\Common\Collections\ArrayCollection();
    
        }
    }
    
    class StudentCourse
    class TemplateModules
    {
        /**
         * @ORM\ManyToOne(targetEntity="Modules", inversedBy="templates")
         * 
         * @ORM\ManyToOne(targetEntity="Student", inversedBy="studentInverse")
         * @ORM\JoinColumn(name="student", referencedColumnName="id")
         */
        protected $modules;
        protected $studentMap;
    
         /**
         * @ORM\ManyToOne(targetEntity="Templates", inversedBy="modules")
         * 
         * @ORM\ManyToOne(targetEntity="Course", inversedBy="courseInverse")
         * @ORM\JoinColumn(name="course", referencedColumnName="id")
         */
        protected $templates;
        protected $courseMap;
    }
    
    
    class Course
    class Templates
    {
        /**
         * @ORM\OneToMany(targetEntity="TemplateModules", mappedBy="templates")
         * 
         * @ORM\OneToMany(targetEntity="StudentCourse", mappedBy="courseMap")
         **/
        protected $modules;
        protected $courseInverse;
    
        public function __construct() {
            $this->modules = new \Doctrine\Common\Collections\ArrayCollection();
            $this->courseInverse = new \Doctrine\Common\Collections\ArrayCollection();
        }
    }
    

    编辑 2:

    听起来这就是你的情况:

    $studentId = 1;
    $courseId = 1;
    
    $studentCourse = new StudentCourse();
    $studentCourse->setStudentMap($studentId);
    $studentCourse->setCourseMap($courseId);
    
    $em = $this->getDoctrine()->getManager();
    $em->persist($studentCourse);
    $em->flush();
    

    以上行不通!下面是它应该是这样的,因为你的中间表期待一个对象,而不是像整数这样的其他东西。

    ->addModule(instance of TemplateModules is expected here)
    but you have given
    ->addModule(instance of Modules)
    

    编辑 3:

    在我看来,您的装置有点复杂。简化的示例夹具。一件一件(一点点)做,这样你就可以轻松调试了。

    $em = $this->getDoctrine()->getManager();
    
    $modules = new Modules();
    $modules->.....
    $em->persist($modules);
    
    
    $templates = new Templates();
    $templates->.....
    $em->persist($templates);
    
    
    $templateModules = new TemplateModules();
    $templateModules->setModules($modules);
    $templateModules->setTemplates($templates);
    $em->persist($templateModules);
    
    $em->flush();
    

    【讨论】:

    • 我有点困惑。我添加了变量,然后运行了学说:生成:实体来创建其余的。那么生成器是否错误地创建了它们?如果是这样,我该怎么做才能解决它?
    • 嗯,好的。很有可能,我还没有完全掌握数据库关系,但我不认为。如果您不介意,我会在问题中发布我的实体文件,以便您查看。
    • doctrine:generate:entities 不会做错事。
    • 是的,我就是这么想的。那我一定是把关系设置错了。
    • 请检查编辑部分。
    猜你喜欢
    • 2020-03-29
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2017-04-04
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    • 2019-02-15
    相关资源
    最近更新 更多