php - Doctrine2 dont want to insert the columns in the table -
here insert query, how can tell that, created_at
(current time-stamp), is_active
(default 1) set in mysql db structure needs taken.
when omit $question->setcreatedat($this->createdat);
in insert operation shows me integrity constraint violation, know issue?
in questions table:
question: id question created_by created_at modified_by modified_at is_active
entity:
<?php namespace library\entity; use doctrine\orm\mapping orm; /** * base class entities * class maps id, active, created , 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; } }
this question
entity:
<?php namespace survey\entity; use doctrine\common\collections\arraycollection; use doctrine\orm\mapping 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}]"; // } }
here insert operation:
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 question"); } }
this ok, working properly, dont want insert 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 question"); } }
if want set default values best set them in object model possible.
/** * @orm\column(name="is_active", type="boolean") * @var boolean */ protected $active = true;
for time-stamps though bit of different story...
suggest take @ the gedmo doctrine extensions library includes solutions createdat
, other common columns model. no need reinvent wheel... .
Comments
Post a Comment