【发布时间】:2012-03-02 00:17:13
【问题描述】:
使用 注释 为给定列设置默认值并初始化实体关系的集合非常简单:
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
}
}
如何使用 YAML 定义而不手动编写Category.php 来实现相同的效果? __construct() 是唯一的方法吗?
Acme\StoreBundle\Entity\Category:
type: entity
id:
id:
type: integer
generator: { strategy: AUTO }
fields:
is_visible:
type: bool
oneToMany:
products:
targetEntity: Product
mappedBy: category
【问题讨论】:
标签: symfony doctrine doctrine-orm