【问题标题】:symfony2 doctrine allow null values?symfony2 原则允许空值?
【发布时间】:2012-04-16 15:36:56
【问题描述】:

所以,我对 symfony 还是很陌生,但我学得很快……我创建了一个实体和一个 crud(我忘记了我实际上是怎么做的,但它是通过命令行)..

实体是在其中创建的:

namespace Ecs\CrmBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Ecs\CrmBundle\Entity\TimeClock
 */
class TimeClock
{
    /**
     * @var integer $id
     */
    private $id;

    /**
     * @var datetime $in1
     */
    private $in1;

    /**
     * @var datetime $out1
     */
    private $out1;

    /**
     * @var datetime $in2
     */
    private $in2;

    /**
     * @var datetime $out2
     */
    private $out2;

    /**
     * @var datetime $in3
     */
    private $in3;

    /**
     * @var datetime $out3
     */
    private $out3;


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

    /**
     * Set in1
     *
     * @param datetime $in1
     * @return TimeClock
     */
    public function setin1($in1)
    {
        $this->in1 = $in1;
        return $this;
    }

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

    /**
     * Set in2
     *
     * @param datetime $in2
     * @return TimeClock
     */
    public function setin2($in2)
    {
        $this->in2 = $in2;
        return $this;
    }

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

    /**
     * Set in3
     *
     * @param datetime $in3
     * @return TimeClock
     */
    public function setin3($in3)
    {
        $this->in3 = $in3;
        return $this;
    }

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

    /**
     * Set in4
     *
     * @param datetime $in4
     * @return TimeClock
     */
    public function setin4($in4)
    {
        $this->in4 = $in4;
        return $this;
    }

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

    /**
     * Set in5
     *
     * @param datetime $in5
     * @return TimeClock
     */
    public function setin5($in5)
    {
        $this->in5 = $in5;
        return $this;
    }

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

    /**
     * Set in6
     *
     * @param datetime $in6
     * @return TimeClock
     */
    public function setin6($in6)
    {
        $this->in6 = $in6;
        return $this;
    }

    /**
     * Get in6
     *
     * @return datetime 
     */
    public function getin6()
    {
        return $this->in6;
    }
    /**
     * @var Ecs\AgentManagerBundle\Entity\User
     */
    private $noteBy;


    /**
     * Set noteBy
     *
     * @param Ecs\AgentManagerBundle\Entity\User $noteBy
     * @return TimeClock
     */
    public function setNoteBy(\Ecs\AgentManagerBundle\Entity\User $noteBy = null)
    {
        $this->noteBy = $noteBy;
        return $this;
    }

    /**
     * Get noteBy
     *
     * @return Ecs\AgentManagerBundle\Entity\User 
     */
    public function getNoteBy()
    {
        return $this->noteBy;
    }
    /**
     * @var datetime $in1
     */
    private $in1;

    /**
     * @var datetime $out1
     */
    private $out1;

    /**
     * @var datetime $in2
     */
    private $in2;

    /**
     * @var datetime $out2
     */
    private $out2;

    /**
     * @var datetime $in3
     */
    private $in3;

    /**
     * @var datetime $out3
     */
    private $out3;

    /**
     * @var varchar $totaltime
     */
    private $totaltime;

    /**
     * @var datetime $daydate
     */
    private $daydate;


    /**
     * Set in1
     *
     * @param datetime $in1
     * @return TimeClock
     */
    public function setIn1($in1)
    {
        $this->in1 = $in1;
        return $this;
    }

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

    /**
     * Set out1
     *
     * @param datetime $out1
     * @return TimeClock
     */
    public function setOut1($out1)
    {
        $this->out1 = $out1;
        return $this;
    }

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

    /**
     * Set in2
     *
     * @param datetime $in2
     * @return TimeClock
     */
    public function setIn2($in2)
    {
        $this->in2 = $in2;
        return $this;
    }

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

    /**
     * Set out2
     *
     * @param datetime $out2
     * @return TimeClock
     */
    public function setOut2($out2)
    {
        $this->out2 = $out2;
        return $this;
    }

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

    /**
     * Set in3
     *
     * @param datetime $in3
     * @return TimeClock
     */
    public function setIn3($in3)
    {
        $this->in3 = $in3;
        return $this;
    }

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

    /**
     * Set out3
     *
     * @param datetime $out3
     * @return TimeClock
     */
    public function setOut3($out3)
    {
        $this->out3 = $out3;
        return $this;
    }

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

    /**
     * Set totaltime
     *
     * @param varchar $totaltime
     * @return TimeClock
     */
    public function setTotaltime(\varchar $totaltime)
    {
        $this->totaltime = $totaltime;
        return $this;
    }

    /**
     * Get totaltime
     *
     * @return varchar 
     */
    public function getTotaltime()
    {
        return $this->totaltime;
    }

    /**
     * Set daydate
     *
     * @param datetime $daydate
     * @return TimeClock
     */
    public function setDaydate($daydate)
    {
        $this->daydate = $daydate;
        return $this;
    }

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

然后当我这样做时,我想到了学说:生成:实体的事情(我可能完全错了——我忘记了我做了什么来创建它)..

它生成一个 timeclock.orm.yml 并在数据库中创建表或类似的东西..

timeclock.orm.yml 有这个:

Ecs\CrmBundle\Entity\TimeClock:
    type: entity
    table: null
    fields:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
        in1:
            type: datetime
        out1:
            type: datetime
        in2:
            type: datetime
        out2:
            type: datetime
        in3:
            type: datetime
        out3:
            type: datetime
        totaltime:
            type: string
        daydate:
            type: datetime
    manyToOne:
        noteBy:
            targetEntity:  Ecs\AgentManagerBundle\Entity\User
    lifecycleCallbacks: {  }

问题是,totaltime,所有inout 字段都需要默认为NULL,我找不到如何做到这一点.. 我一直在寻找最后30 个在我来这里之前几分钟试图弄清楚..

那么,一旦我编辑了我需要的任何文件,如何让它在数据库中也更新它?

【问题讨论】:

    标签: symfony doctrine


    【解决方案1】:

    我一直在寻找解决方案,但对接受的解决方案不满意。 [编辑:虽然我现在知道这是因为我使用了注释]。 我发现对我有用的方法是设置可空:

    /**
     * @ORM\Column(type="varchar", nullable=true)
     */
    private $totaltime = null;
    

    【讨论】:

    • 接受的答案可以为空,但您使用的是带注释的格式,而问题作者使用的是 yml。
    • 这是真的,但是当我发布这个时,我对其他方法不是很熟悉。我认为它对某些人仍然有用。
    • ...确实如此。谢谢@DanielP。
    • 不是$totaltime默认null吗?
    • 是的,但我当时很挣扎。
    【解决方案2】:

    直接在实体文件中设置默认值:

    /**
     * @var varchar $totaltime
     */
    private $totaltime = null;
    

    然后添加到你的 yml 文件中:

        totaltime:
            type: string
            nullable: TRUE
    

    然后是这样的:

    php app/console doctrine:schema:update --dump-sql
    

    更新数据库。

    但是,您确实需要放慢速度,仔细阅读手册中的示例。猜测你是怎么做的真的不会让你走得太远。

    【讨论】:

    猜你喜欢
    • 2011-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-14
    • 2013-05-11
    • 1970-01-01
    • 2015-09-06
    相关资源
    最近更新 更多