Home  >  Q&A  >  body text

symfony - doctrine one-to-many relationship optional

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_leeringa_lee2734 days ago452

reply all(1)I'll reply

  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-16 16:45:46

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

    ``
    class Article
    {

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

    }
    ``

    reply
    0
  • Cancelreply