2016-01-21 88 views
0

这里是我的插入查询,我怎么能告诉,created_at(当前时间戳),is_active(默认1)在需要采取的mysql数据库结构中设置。Doctrine2不想插入表中的列

当我在插入操作省略$question->setCreatedAt($this->createdAt);它显示我的完整性约束违规,你知道是什么问题?

在问题表:

question: 

id 
question 
created_by 
created_at 
modified_by 
modified_at 
is_Active 

实体:

<?php 

    namespace Library\Entity; 

    use Doctrine\ORM\Mapping as ORM; 

    /** 
    * Base class for all the Entities 
    * This class maps id, active, created and modified columns 
    * 
    * @author 

*/ 
/** 
* @ORM\MappedSuperclass 
*/ 
class BaseEntity { 

    /** 
    * @ORM\Id 
    * @ORM\GeneratedValue 
    * @ORM\Column(name="id", type="integer") 
    * @var integer 
    */ 
    protected $id; 

    /** 
    * @ORM\Column(name="is_active", type="boolean") 
    * @var boolean 
    */ 
    protected $active; 

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

    /** 
    * @ORM\Column(name="created_by", type="integer", nullable=true) 
    * @var integer 
    */ 
    protected $createdBy; 

    /** 
    * @ORM\Column(name="modified_at", type="datetime") 
    * @var datetime 
    */ 
    protected $modifiedAt; 

    /** 
    * @ORM\Column(name="modified_by", type="integer") 
    * @var integer 
    */ 
    protected $modifiedBy; 

    public function getId() { 
     return $this->id; 
    } 

    public function getActive() { 
     return $this->active; 
    } 

    public function getCreatedAt() { 
     return $this->createdAt; 
    } 

    public function getCreatedBy() { 
     return $this->createdBy; 
    } 

    public function getModifiedAt() { 
     return $this->modifiedAt; 
    } 

    public function getModifiedBy() { 
     return $this->modifiedBy; 
    } 

    public function setId($id) { 
     $this->id = $id; 
    } 

    public function setActive($active) { 
     $this->active = $active; 
    } 

    public function setCreatedAt($createdAt) { 
     $this->createdAt = $createdAt; 
    } 

    public function setCreatedBy($createdBy) { 
     $this->createdBy = $createdBy; 
    } 

    public function setModifiedAt($modifiedAt) { 
     $this->modifiedAt = $modifiedAt; 
    } 

    public function setModifiedBy($modifiedBy) { 
     $this->modifiedBy = $modifiedBy; 
    } 
} 

这是我Question实体:

<?php 

namespace Survey\Entity; 

use Doctrine\Common\Collections\ArrayCollection; 
use Doctrine\ORM\Mapping as ORM; 
use Library\Entity\BaseEntity; 
use Survey\Entity\Survey; 

/** 
* Description of Survey Questions 
* 
* @author Mubarak 
*/ 

/** 
* @ORM\Entity 
* @ORM\Table(name="survey_questions") 
*/ 

class Question extends BaseEntity{ 

    /** 
    * @ORM\Column(name="question", type="string") 
    * @var string 
    */ 
    protected $question; 

    /** 
    * @ORM\ManyToOne(targetEntity="Survey\Entity\Survey", inversedBy="questions") 
    * @ORM\JoinColumn(name="survey_id", referencedColumnName="id") 
    */ 
    private $surveys; 

    public function getQuestion() { 
     return $this->question; 
    } 

    public function setQuestion($question) { 
     $this->question = $question; 
    } 

    public function getSurveys() { 
     return $this->surveys; 
    } 

// public function setSurveys(ArrayCollection $survey) { 
    public function setSurveys(Survey $surveys = null) { 
     $this->surveys = $surveys; 
    } 

// public function __toString() { 
//  return __CLASS__ . ": [id: {$this->id}, name: {$this->name}]"; 
// } 
} 

这里是我的插入OPERAT离子:

public function insertQuestion($userId, $survey, $questionArr) { 
     try{ 
      $question = new Question(); 
      $question->setQuestion($questionArr['question']); 
      $question->setSurveys($survey); 
      $question->setActive(1); 
      $question->setCreatedBy($userId); 
      $question->setCreatedAt($this->createdAt); 
      $question->setModifiedBy($userId); 
      $question->setModifiedAt($this->modifiedAt); 
      $this->entityManager->persist($question); 
      $this->entityManager->flush(); 
      return $question; 
     }catch(Exception $ex){ 
      throw new Exception("Couldnt insert the question"); 
     } 
    } 

这是好的,它的正常工作,但我不希望插入Created_at,modified_at

public function insertQuestion($userId, $survey, $questionArr) { 
      try{ 
       $question = new Question(); 
       $question->setQuestion($questionArr['question']); 
       $question->setSurveys($survey); 
       $question->setActive(1); 
       $question->setCreatedBy($userId); 
       $question->setModifiedBy($userId); 
       $this->entityManager->persist($question); 
       $this->entityManager->flush(); 
       return $question; 
      }catch(Exception $ex){ 
       throw new Exception("Couldnt insert the question"); 
      } 
     } 

回答

1

如果你想设置的默认值最好是将它们设置在你的对象模型尽可能。

/** 
* @ORM\Column(name="is_active", type="boolean") 
* @var boolean 
*/ 
protected $active = true; 

对于时间戳,虽然这是一个有点不同的故事的...
我建议看看the Gedmo doctrine extensions library其中包括createdAt和模型解决方案的其他公共列。不需要重新发明轮子......。

+0

谢谢,关于积极的情况下,你是对的,但created_at不工作 – user3929758

+0

@ user3929758好吧。我从我的答案中删除了'created_at'部分。你应该认真检查出学说扩展名... – Wilt

+0

它是格迪莫学说扩展库是Doctrine2的一个包装 – user3929758