Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default column value with Doctrine2 and Symfony2 using YAML?

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
    }
}

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
like image 218
gremo Avatar asked Mar 02 '12 00:03

gremo


3 Answers

You can add default value for the column using options property in annotation or yaml. You can read more at doctrine annotation documentation.

Example for annotation:

/**
 * @ORM\Column(type="bool", name="is_visible", options={"default": false})
 */
protected $isVisible;

Example for YAML:

isVisible:
    type: boolean
    column: is_visible
    options: 
        default: false
like image 110
Artsiom Tymchanka Avatar answered Nov 17 '22 05:11

Artsiom Tymchanka


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
}

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
    }
}

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
    }
}

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.

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.

Hope it helps.

Regards,
Matt

like image 24
Matt Avatar answered Nov 17 '22 04:11

Matt


You can try adding DEFAULT value with columnDefinition, but it is DDL and it depends on specific DBMS (bad thing). Following your example, the field *is_visible* using MySQL:

is_visible:
    type: bool
    columnDefinition: is_visible tinyint(1) NOT NULL DEFAULT '1'

In general, this is not a good idea and you are encouraged to use the constructor method or property initialization by code within the entity class...

like image 2
caligari Avatar answered Nov 17 '22 04:11

caligari