Default column value with Doctrine2 and Symfony2 using YAML?
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
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
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...