我最近问过以下问题:
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;
}
}
答案 0 :(得分:1)
这应该是这样的,因为你的表/方法/实体期望一个特定的对象,而不是整数,其他对象等等。
->addModule(instanbe of TemplateModules is expected here)
but you have given
->addModule(instance of Modules is expected here)
如果你说它肯定应该是$this->modules[] = $modules;
,那么你可能已经设置了错误或类似的关系。您可能需要检查关系。
修改强>
看看你和我的代码之间的区别。您的注释中没有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();