当我运行symfony命令加载灯具时
我有错误
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'topic_id' cannot be null
也许它因为表格标签中的coloumn topic_id与表格主题coloumn topic_id有关系manyToMany
我的装置
<?php
class LoadTopicContentData extends AbstractFixture implements OrderedFixtureInterface
{
public function getOrder()
{
return 1; // the order in which fixtures will be loaded
}
public function load(ObjectManager $manager)
{
$contentTopic1 = new TopicContent();
$contentTopic1->setTopicId(1);
$manager->flush();
}
}
我的实体
<?php
namespace Application\ClubBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* TopicContent
*/
class TopicContent
{
/**
* Set topic_id
*
*/
public function setTopicId($topicId)
{
$this->topic_id = $topicId;
return $this;
}
答案 0 :(得分:0)
public function load(ObjectManager $manager)
{
$contentTopic1 = new TopicContent();
$contentTopic1->setTopicId(1);
$manager->persist($contentTopic1);
$manager->flush();
}
您必须在调用flush之前保留该对象(阅读doctrine文档以获取更多信息)。
答案 1 :(得分:0)
我找到了解决方案。 必须在加载夹具之前添加合并引用。
$contentTopic1->setTopic( $manager->merge($this->getReference('topic1')));
和
$this->addReference('topic1', $topic1);
主夹具中的