symfony - doctrine one-to-many relationship optional
ringa_lee
ringa_lee 2017-05-16 16:43:46
0
1
497

ExistingArticle,CategoryTwo entities, the relationship is onetomany;
Article does not have to correspond to category. If article does not have a corresponding category, then category_id =0;

Then the question comes

$article = new Article();
$article->setTitle('This is a test article');
//...
$em->persist($article);
$em->flush();

The error is reported as follows, category_id cannot be empty

  [Doctrine\DBAL\Driver\PDOException]
  SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'category_id' cannot be null



  [PDOException]
  SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'category_id' cannot be null

How to set up this unnecessary Association

ringa_lee
ringa_lee

ringa_lee

reply all(1)
曾经蜡笔没有小新

Set the nullable attribute of Article::$category to true

``
class Article
{

/**
 * @ORM\OneToMany(targetEntity="AppBundle\Entity\Category", nullable=true)
 */
protected $category;

}
``

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template