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