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
I think you misunderstood annotations somehow because the default value is set via plain php.
There is no difference using the YAML mapping for the default value. The reason is simple, here how you class is looking with annotations:
And this is how it looks with YAML mapping:
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