简体   繁体   中英

Symfony 3 : DateTime null even if it is specified in the Entity constructor

I'm under Symfony 3.0.6 I use loadFixtures with this Entity :

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Categorie
 *
 * @ORM\Table(name="categorie")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\CategorieRepository")
 */
class Categorie
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="Libelle", type="string", length=100)
     */
    private $libelle;

    /**
     * @var datetime
     *
     * @ORM\Column(name="created_at", type="datetime")
     */
    private $created_at;

    /**
     * @ORM\ManyToOne(targetEntity="\AppBundle\Entity\User", inversedBy="categories")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id", onDelete="CASCADE")
     */
    private $user;

    public function __construct()
    {
        $this->createdAt = new \DateTime('now');
    }

    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set libelle
     *
     * @param string $libelle
     *
     * @return Categorie
     */
    public function setLibelle($libelle)
    {
        $this->libelle = $libelle;

        return $this;
    }

    /**
     * Get libelle
     *
     * @return string
     */
    public function getLibelle()
    {
        return $this->libelle;
    }

    /**
     * Set user
     *
     * @param \AppBundle\Entity\User $user
     *
     * @return Categorie
     */
    public function setUser(User $user = null)
    {
        $this->user = $user;

        return $this;
    }

    /**
     * Get user
     *
     * @return \AppBundle\Entity\User
     */
    public function getUser()
    {
        return $this->user;
    }

    /**
     * Set createdAt
     *
     * @param datetime $createdAt
     */
    public function setCreatedAt($createdAt)
    {
        $this->createdAt = $createdAt;
    }

    /**
     * Get createdAt
     *
     * @return datetime
     */
    public function getCreatedAt()
    {
        return $this->createdAt;
    }
}

And when i try to insert some values with this :

public function loadCategorie(ObjectManager $manager, $user)
{
    $aLib = array('Categ1','Categ2','Categ3','Categ4','Categ5','Categ6','Categ7');

    foreach($aLib as $catP)
    {
        $cat = new Categorie();
        $cat->setLibelle("[".$user->getUsername()."] ".$catP);
        $cat->setUser($user);
        $manager->persist($cat);
    }
    $manager->flush();
}

I have this error :

[Doctrine\\DBAL\\Exception\\NotNullConstraintViolationException] An exception occurred while executing 'INSERT INTO categorie (Libelle, created_at, user_id) VALUES (?, ?, ?)' with params ["[cyriladmin] Categ1", null, 1]: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'created_at' cannot be null

any ideas ?

You have a bug in the entity: private $created_at; vs $this->createdAt = new \\DateTime('now'); <- different variables names.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM