6

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

4 Answers 4

16

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this looks like the only valid answer if you are concerned about database migrations when adding a new column whose default value must be set for all existing objects. Setting the value in PHP would only work for new objects.
13

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

6 Comments

Nope annotation was clear to me (maybe my english it not so good to explai well). But you confirmed what i'm saying: using YAML you have to edit generated PHP class in order to set default values, right? There is not other way for doing this?
There is no way to do this with annotations either. You have to manually add the construct method and add the default value.
Yep, as you said and @Boo confirmed, you need to put your default value in the generated PHP class directly. I have edited my answer to highlight this fact.
Instead of using the constructor you should probably do something like protected $is_visible = true; Not positive if it really matters but I do know that doctrine hydrates it's objects without calling the constructor. Less code anyways.
@Cerad is it really true but I do know that doctrine hydrates it's objects without calling the constructor? I was always wondering what exactly is algorithm Doctrine uses for hydrating entities. The problem I am working now: how to ensure integrity of the data when schema is updated (ie. field added) on the existing populated DB.. Because as this field did not exist before, what should be the default value of it... and how to behave around it.
|
2

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

Comments

1

The time has passed. Now you can set default value for a column through yaml.

columnName:
     type: string
     options:
          default: "someText"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.