Doctrine2实体,告诉不更新某些列
Doctrine2 entity , tell not update certain columns
我不想更新 table 的某些列。
在问题table中,我只想更新问题栏:
question:
id
question
created_by
created_at
modified_by
modified_at
is_Active
在上面的table栏中,我不需要更新create_at
、created_by
、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}]";
// }
}
这是我的更新函数:
public function updateQuestion($userId, $data ) {
try{
$surveyId = 1;
$survey = $this->entityManager->getRepository('Survey\Entity\Survey')->find($surveyId);
$question = new Question();
$question->setQuestion($data['question']);
$question->setSurveys($survey);
$question->setId(1);
$this->entityManager->merge($question);
$this->entityManager->flush();
} catch (Exception $ex) {
throw new Exception("Couldnt update the Question");
}
以下是我收到的错误消息:
An exception occurred while executing 'UPDATE survey_questions SET is_active = ?, created_at = ?, created_by = ?, modified_at = ?, modified_by = ? WHERE id = ?' with params [null, null, null, null, null, 1]:\n\nSQLSTATE[23000]: Integrity constraint violation: 1048 Column 'created_at' cannot be null
这是我的插入操作:
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");
}
}
问题是您正在创建一个新实体,Question
。
When calling EntityManager::flush()
Doctrine computes the changesets of all the currently managed entities and saves the differences to the database. In case of object properties (@Column(type=”datetime”)
or @Column(type=”object”)
) these comparisons are always made BY REFERENCE.
"By reference" 很重要,因为您的新实体具有所有日期时间字段的 null
值(而不是之前设置的 \DateTime
个实例)。当调用 flush()
时,Doctrine 正确检测到这些字段已更改并执行在 数据库 级别失败的查询。
当 'editing' 个实体时,Doctrine 中不需要 merge()
操作,这些实体用于 分离的 需要再次管理的实例。
要解决这个问题,您应该首先加载实体的托管实例;然后修改字段并刷新。
$question = $entityManager->find(1);
$question->setQuestion($data['question']);
$entityManager->flush();
我不想更新 table 的某些列。
在问题table中,我只想更新问题栏:
question:
id
question
created_by
created_at
modified_by
modified_at
is_Active
在上面的table栏中,我不需要更新create_at
、created_by
、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}]";
// }
}
这是我的更新函数:
public function updateQuestion($userId, $data ) {
try{
$surveyId = 1;
$survey = $this->entityManager->getRepository('Survey\Entity\Survey')->find($surveyId);
$question = new Question();
$question->setQuestion($data['question']);
$question->setSurveys($survey);
$question->setId(1);
$this->entityManager->merge($question);
$this->entityManager->flush();
} catch (Exception $ex) {
throw new Exception("Couldnt update the Question");
}
以下是我收到的错误消息:
An exception occurred while executing 'UPDATE survey_questions SET is_active = ?, created_at = ?, created_by = ?, modified_at = ?, modified_by = ? WHERE id = ?' with params [null, null, null, null, null, 1]:\n\nSQLSTATE[23000]: Integrity constraint violation: 1048 Column 'created_at' cannot be null
这是我的插入操作:
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");
}
}
问题是您正在创建一个新实体,Question
。
When calling
EntityManager::flush()
Doctrine computes the changesets of all the currently managed entities and saves the differences to the database. In case of object properties (@Column(type=”datetime”)
or@Column(type=”object”)
) these comparisons are always made BY REFERENCE.
"By reference" 很重要,因为您的新实体具有所有日期时间字段的 null
值(而不是之前设置的 \DateTime
个实例)。当调用 flush()
时,Doctrine 正确检测到这些字段已更改并执行在 数据库 级别失败的查询。
当 'editing' 个实体时,Doctrine 中不需要 merge()
操作,这些实体用于 分离的 需要再次管理的实例。
要解决这个问题,您应该首先加载实体的托管实例;然后修改字段并刷新。
$question = $entityManager->find(1);
$question->setQuestion($data['question']);
$entityManager->flush();