【问题标题】:Doctrine2 dont want to insert the columns in the tableDoctrine2 不想在表中插入列
【发布时间】:2016-01-21 09:09:20
【问题描述】:

这是我的Insert Query,我怎么知道,created_at(current time-stamp), is_active(default 1) 需要在mysql db结构中设置。

当我在插入操作中省略 $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}]";
//    }
}

这是我的插入操作:

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");
            }
        }

【问题讨论】:

    标签: php orm doctrine-orm zend-framework2


    【解决方案1】:

    如果您想设置默认值,最好尽可能在您的对象模型中设置它们。

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

    对于时间戳虽然有点不同...
    我建议查看 the Gedmo doctrine extensions library,其中包括 createdAt 的解决方案以及您模型的其他常见列。无需重新发明轮子......

    【讨论】:

    • 谢谢,关于活动案例,你是对的,但是 created_at 不起作用
    • @user3929758 好的。我从答案中删除了 created_at 部分。您应该认真检查学说扩展...
    • Gedmo 学说扩展库是 Doctrine2 的一个包吗
    • @user3929758 你应该与 Doctrine 一起使用,但它不是官方的 Doctrine 2 存储库...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-18
    • 1970-01-01
    • 2015-05-02
    • 2011-11-02
    • 2013-09-10
    • 2012-11-19
    相关资源
    最近更新 更多