使用YAML的Doctrine2和Symfony2的默认列值? [英] Default column value with Doctrine2 and Symfony2 using YAML?

查看:109
本文介绍了使用YAML的Doctrine2和Symfony2的默认列值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用注释非常简单,可以为给定的列设置默认值,并初始化实体关系的集合:

Using annotations is quite simple to set a default value for a given column and initialize collections for entity relations:

use Doctrine\Common\Collections\ArrayCollection;

class Category
{   
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\OneToMany(targetEntity="Product", mappedBy="category")
     */
    protected $products;

    /**
     * @ORM\Column(type="bool")
     */
    protected $is_visible;

    public function __construct()
    {
        $this->products   = new ArrayCollection();
        $this->is_visible = true; // Default value for column is_visible
    }
}

可以使用 YAML 定义来实现,而无需手动写入 Category.php ?是 __ construct()这样做的唯一方法?

How the same can be achieved using YAML definition instead, without manually write Category.php? Is __construct() the only method for doing this?

Acme\StoreBundle\Entity\Category:
    type: entity
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        is_visible:
            type: bool
    oneToMany:
        products:
            targetEntity: Product
            mappedBy: category


推荐答案

我认为你误解了注释,因为默认值是通过简单的php设置的。

I think you misunderstood annotations somehow because the default value is set via plain php.

/**
 * @ORM\Column(type="bool") <- This is an annotation
 */
protected $is_visible;

public function __construct()
{
    $this->products   = new ArrayCollection(); // <- This is not an annotation
    $this->is_visible = true; // <- This is not an annotation
}

使用YAML映射为默认值。原因很简单,在这里你如何看着注释:

There is no difference using the YAML mapping for the default value. The reason is simple, here how you class is looking with annotations:

use Doctrine\Common\Collections\ArrayCollection;

class Category
{   
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\OneToMany(targetEntity="Product", mappedBy="category")
     */
    protected $products;

    /**
     * @ORM\Column(type="bool")
     */
    protected $is_visible;

    public function __construct()
    {
        $this->products   = new ArrayCollection();
        $this->is_visible = true; // Default value for column is_visible
    }
}

这就是它使用YAML映射:

And this is how it looks with YAML mapping:

    use Doctrine\Common\Collections\ArrayCollection;

class Category
{   
    protected $id;
    protected $products;
    protected $is_visible;

    public function __construct()
    {
        $this->products   = new ArrayCollection();
        $this->is_visible = true; // Default value for column is_visible
    }
}

第二个例子是没有更多的注释,因为映射是通过YAML完成的。课程的建设完全一样。因此,默认值在构建时设置,这是在纯PHP中完成的。

The difference in the second example is there is no more annotations, since the mapping is done via YAML. The construction of the class is done exactly the same. Thus, default values are set at construction time which is done in plain PHP.

此任务的注释和YAML映射之间没有区别。所以,底线,你需要编辑生成的PHP类来放置你的默认值。没有办法可以在YAML中设置它,并且至少在我们说话的时候让学说给你这个代码。

There is no difference between annotations and YAML mapping for this task. So, bottom line, you need to edit the generated PHP class to put your default values. There is no way you can set it in YAML and let doctrine put this code for you, at least, at the time we speak.

也许我误解了你的问题:)如果是这种情况,请不要犹豫纠正我。

Maybe I misunderstood your question :), if it is the case, don't hesitate to correct me.

希望有帮助。


Matt

Regards,
Matt

这篇关于使用YAML的Doctrine2和Symfony2的默认列值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆