Когда я использую:
php app/console doctrine:fixtures:load --fixtures=/var/www/Symfony/src/BISSAP/ForumBundle/DataFixtures/ORM
Я получил :
[Doctrine\DBAL\DBALException]
An exception occurred while executing 'INSERT INTO Post (content, date_creation, date_modif, slug, topic_id, user_id) VALUES (?, ?
, ?, ?, ?, ?)' with params ["Contenu du post!! gGasp Strategie duplication article no commentSyndrome 60", "2015-08-21 14:55:24",
"2015-08-21 14:55:24", "slug-slug", null, 34]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'topic_id' cannot be null
[PDOException]
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'topic_id' cannot be null
Entity Post.php:
<?php
namespace BISSAP\ForumBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Post
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="BISSAP\ForumBundle\Entity\PostRepository")
*/
class Post
{
/**
* @ORM\ManyToOne(targetEntity="topic", inversedBy="Posts", cascade={"persist"})
* @ORM\JoinColumn(nullable=false)
*/
private $topic;
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="Posts", cascade={"persist"})
* @ORM\JoinColumn(nullable=false)
*/
private $user;
/**
* @var string
*
* @ORM\Column(name="content", type="text")
*/
private $content;
/**
* @var \DateTime
*
* @ORM\Column(name="date_creation", type="datetime")
*/
private $dateCreation;
/**
* @var \DateTime
*
* @ORM\Column(name="date_modif", type="datetime")
*/
private $dateModif;
/**
* @var string
*
* @ORM\Column(name="slug", type="string", length=255)
*/
private $slug;
/**
* Constructor
*/
public function __construct()
{
$this->dateCreation = new \DateTime();
$this->dateModif = new \DateTime();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set content
*
* @param string $content
* @return Post
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Get content
*
* @return string
*/
public function getContent()
{
return $this->content;
}
/**
* Set dateCreation
*
* @param \DateTime $dateCreation
* @return Post
*/
public function setDateCreation($dateCreation)
{
$this->dateCreation = $dateCreation;
return $this;
}
/**
* Get dateCreation
*
* @return \DateTime
*/
public function getDateCreation()
{
return $this->dateCreation;
}
/**
* Set dateModif
*
* @param \DateTime $dateModif
* @return Post
*/
public function setDateModif($dateModif)
{
$this->dateModif = $dateModif;
return $this;
}
/**
* Get dateModif
*
* @return \DateTime
*/
public function getDateModif()
{
return $this->dateModif;
}
/**
* Set slug
*
* @param string $slug
* @return Post
*/
public function setSlug($slug)
{
$this->slug = $slug;
return $this;
}
/**
* Get slug
*
* @return string
*/
public function getSlug()
{
return $this->slug;
}
/**
* Set Topic
*
* @param \BISSAP\ForumBundle\Entity\Topic $topic
* @return Post
*/
public function setTopic(\BISSAP\ForumBundle\Entity\Topic $topic)
{
$this->Topic = $topic;
return $this;
}
/**
* Get Topic
*
* @return \BISSAP\ForumBundle\Entity\Topic
*/
public function getTopic()
{
return $this->Topic;
}
/**
* Set user
*
* @param \BISSAP\ForumBundle\Entity\User $user
* @return Post
*/
public function setUser(\BISSAP\ForumBundle\Entity\User $user)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* @return \BISSAP\ForumBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
}
Entity Topic.php:
<?php
namespace BISSAP\ForumBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/*use BISSAP\BodyConcept\Entity\Forum;
*/
/**
* Topic
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="BISSAP\ForumBundle\Entity\TopicRepository")
*/
class Topic
{
/**
* @ORM\ManyToOne(targetEntity="Forum", inversedBy="Topics", cascade={"persist"})
* @ORM\JoinColumn(nullable=false)
*/
private $forum;
/**
* @var ArrayCollection $Posts
*
* @ORM\OneToMany(targetEntity="Post", mappedBy="Topic", cascade={"persist", "remove", "merge"})
*/
private $posts;
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="Topics", cascade={"persist"})
* @ORM\JoinColumn(nullable=false)
*/
private $user;
/**
* @var integer
*
* @ORM\Column(name="view_count", type="integer")
*/
private $viewCount;
/**
* @var \DateTime
*
* @ORM\Column(name="date_creation", type="datetime")
*/
private $dateCreation;
/**
* @var integer
*
* @ORM\Column(name="reply_count", type="integer")
*/
private $replyCount;
/**
* @var string
*
* @ORM\Column(name="slug", type="string", length=255)
*/
private $slug;
/**
* @var string
*
* @ORM\Column(name="genre", type="string", length=255)
*/
private $genre;
/**
* @var integer
*
* @ORM\Column(name="last_post", type="integer")
*/
private $lastPost;
/**
* @var string
*
* @ORM\Column(name="content", type="text")
*/
private $content;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* @param string $title
* @return Topic
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set user
*
* @param integer $user
* @return Topic
*/
public function setUser($user)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* @return integer
*/
public function getUser()
{
return $this->user;
}
/**
* Set viewCount
*
* @param integer $viewCount
* @return Topic
*/
public function setViewCount($viewCount)
{
$this->viewCount = $viewCount;
return $this;
}
/**
* Get viewCount
*
* @return integer
*/
public function getViewCount()
{
return $this->viewCount;
}
/**
* Set dateCreation
*
* @param \DateTime $dateCreation
* @return Topic
*/
public function setDateCreation($dateCreation)
{
$this->dateCreation = $dateCreation;
return $this;
}
/**
* Get dateCreation
*
* @return \DateTime
*/
public function getDateCreation()
{
return $this->dateCreation;
}
/**
* Set replyCount
*
* @param integer $replyCount
* @return Topic
*/
public function setReplyCount($replyCount)
{
$this->replyCount = $replyCount;
return $this;
}
/**
* Get replyCount
*
* @return integer
*/
public function getReplyCount()
{
return $this->replyCount;
}
/**
* Set slug
*
* @param string $slug
* @return Topic
*/
public function setSlug($slug)
{
$this->slug = $slug;
return $this;
}
/**
* Get slug
*
* @return string
*/
public function getSlug()
{
return $this->slug;
}
/**
* Set genre
*
* @param string $genre
* @return Topic
*/
public function setGenre($genre)
{
$this->genre = $genre;
return $this;
}
/**
* Get genre
*
* @return string
*/
public function getGenre()
{
return $this->genre;
}
/**
* Set lastPost
*
* @param integer $lastPost
* @return Topic
*/
public function setLastPost($lastPost)
{
$this->lastPost = $lastPost;
return $this;
}
/**
* Get lastPost
*
* @return integer
*/
public function getLastPost()
{
return $this->lastPost;
}
/**
* Set content
*
* @param string $content
* @return Topic
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Get content
*
* @return string
*/
public function getContent()
{
return $this->content;
}
/**
* Set forum
*
* @param Forum $forum
* @return Topic
*/
/*public function setForum(\BISSAP\BodyConceptBundle\Entity\Forum $forum)*/
public function setForum(Forum $forum)
{
$this->forum = $forum;
return $this;
}
/**
* Get forum
*
* @return \BISSAP\BodyConceptBundle\Entity\Forum
*/
public function getForum()
{
return $this->forum;
}
/**
* Constructor
*/
public function __construct()
{
$this->posts = new \Doctrine\Common\Collections\ArrayCollection();
$this->dateCreation = new \DateTime();
}
/**
* Add posts
*
* @param \BISSAP\ForumBundle\Entity\Post $posts
* @return Topic
*/
public function addPost(\BISSAP\ForumBundle\Entity\Post $posts)
{
$this->posts[] = $posts;
return $this;
}
/**
* Remove posts
*
* @param \BISSAP\ForumBundle\Entity\Post $posts
*/
public function removePost(\BISSAP\ForumBundle\Entity\Post $posts)
{
$this->posts->removeElement($posts);
}
/**
* Get posts
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getPosts()
{
return $this->posts;
}
}
Светильники: **
** LaodPost.php:
namespace BISSAP\ForumBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use BISSAP\ForumBundle\Entity\Post;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
class LoadPost extends AbstractFixture implements FixtureInterface, OrderedFixtureInterface
{
public function load(ObjectManager $manager)
{
$data = array(
array($this->getReference('BISSAP\ForumBundle\Entity\User-0'),'Contenu du post!! gGasp Strategie duplication article no commentSyndrome 60','slug-slug',$this->getReference('BISSAP\ForumBundle\Entity\Topic-0')),
array($this->getReference('BISSAP\ForumBundle\Entity\User-0'),'Contenu du post!! gGasp Strategie duplication article no commentSyndrome 60','slug-slug',$this->getReference('BISSAP\ForumBundle\Entity\Topic-1')));
foreach ($data as $P) {
$post = new Post();
$post->setUser($P[0]);
$post->setContent($P[1]);
$post->setSlug($P[2]);
$post->setTopic($P[3]);
$manager->persist($post);
}
/*TEST OK*/
$myFile = fopen('/home/sebastien/test_topic.txt','a+');
fputs($myFile, $this->getReference('BISSAP\ForumBundle\Entity\Topic-0')->getUser()->getPseudo());
fclose($myFile);
/*END TEST*/
$manager->flush();
}
public function getOrder()
{
return 5;
}
}
LoadTopic.php:
<?php
namespace BISSAP\ForumBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use BISSAP\ForumBundle\Entity\Forum;
use BISSAP\ForumBundle\Entity\Category;
use BISSAP\ForumBundle\Entity\Topic;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
class LoadTopic extends AbstractFixture implements FixtureInterface, OrderedFixtureInterface
{
public function load(ObjectManager $manager)
{
$data = array(
array('Perte de Volume Musculaire Inexpliqué',$this->getReference('BISSAP\ForumBundle\Entity\User-0'),'60','25','slug-slug','genre','256','Besoins d\'aide pitié...',$this->getReference('BISSAP\ForumBundle\Entity\Forum-0')),
array('Perte de Volume Mental Inexpliqué',$this->getReference('BISSAP\ForumBundle\Entity\User-1'),'60','25','slug-slug','genre','256','Besoins d\'aide pitié...',$this->getReference('BISSAP\ForumBundle\Entity\Forum-0')),
array('Perte de Volume Cosmik Inexpliqué',$this->getReference('BISSAP\ForumBundle\Entity\User-1'),'60','25','slug-slug','genre','256','Besoins d\'aide pitié...',$this->getReference('BISSAP\ForumBundle\Entity\Forum-0')),
array('Perte de Volume Sonor Inexpliqué',$this->getReference('BISSAP\ForumBundle\Entity\User-2'),'60','25','slug-slug','genre','256','Besoins d\'aide pitié...',$this->getReference('BISSAP\ForumBundle\Entity\Forum-0')),
array('Perte de Volume Sonor Inexpliqué',$this->getReference('BISSAP\ForumBundle\Entity\User-2'),'60','25','slug-slug','genre','256','Besoins d\'aide pitié...',$this->getReference('BISSAP\ForumBundle\Entity\Forum-0')),
array('Perte de Volume Sonor Inexpliqué',$this->getReference('BISSAP\ForumBundle\Entity\User-2'),'60','25','slug-slug','genre','256','Besoins d\'aide pitié...',$this->getReference('BISSAP\ForumBundle\Entity\Forum-0')),
array('Perte de Volume Strategique Inexpliqué',$this->getReference('BISSAP\ForumBundle\Entity\User-0'),'60','25','slug-slug','genre','256','Besoins d\'aide pitié...',$this->getReference('BISSAP\ForumBundle\Entity\Forum-0')));
$i=0;
foreach ($data as $Top) {
$topic = new Topic();
$topic->setTitle($Top[0]);
$topic->setUser($Top[1]);
$topic->setViewCount($Top[2]);
$topic->setReplyCount($Top[3]);
$topic->setSlug($Top[4]);
$topic->setGenre($Top[5]);
$topic->setLastPost($Top[6]);
$topic->setContent($Top[7]);
$topic->setForum($Top[8]);
$this->addReference('BISSAP\ForumBundle\Entity\Topic-'.$i,$topic);
$i++;
$manager->persist($topic);
}
$manager->flush();
}
public function getOrder()
{
return 4;
}
}
Я загружаю, сначала LoadTopic.php, а затем LoadPost.php
В целом, у меня есть 5 светильников (LoadCategory.php, LoadForum.php, LoadPost.php, LoadTopic.php, LoadUser.php)
Спасибо.
Задача ещё не решена.
Других решений пока нет …