2012-03-02 78 views
5

使用註釋是很簡單的給定列設置默認值和實體間關係初始化集合:使用YAML定義,而不是使用YAML與Doctrine2和Symfony2的默認列值?

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

如何同樣可以實現,而無需手動編寫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 

回答

13

我想你錯誤地理解了註釋,因爲默認值是通過純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 
} 

使用YAML映射作爲默認值沒有區別。原因很簡單,在這裏你類是如何尋找與註釋:

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映射:

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

在第二個例子中所不同的是沒有更多的註解因爲映射是通過YAML完成的。班級的建設完全一樣。因此,默認值是在建造時在純PHP中完成的。

此任務的註釋和YAML映射沒有區別。所以,底線,你需要編輯生成的PHP類來把你的默認值。至少,在我們說話的時候,您無法在YAML中設置它,並讓原則爲您提供此代碼。

也許我誤解了你的問題:),如果是這樣的話,請不要猶豫,糾正我。

希望它有幫助。

問候,
馬特

+0

沒有註釋對我來說很清楚(或許我的英語說得不好)。但是你確認了我的意思:使用YAML你必須編輯生成的PHP類來設置默認值,對嗎?沒有其他方式來做到這一點? – gremo 2012-03-02 01:45:46

+1

無法使用註釋執行此操作。您必須手動添加構造方法並添加默認值。 – Sgoettschkes 2012-03-02 08:36:16

+0

是的,正如你所說的和@Boo證實的那樣,你需要將你的默認值直接放到生成的PHP類中。我編輯了我的答案以突出這一事實。 – Matt 2012-03-02 15:27:12

2

你可以嘗試添加默認值與columnDefinition,但它是DDL,它依賴於特定的DBMS(壞事)。繼使用MySQL的例子,該領域* is_visible *:

is_visible: 
    type: bool 
    columnDefinition: is_visible tinyint(1) NOT NULL DEFAULT '1' 

在一般情況下,這是不是一個好主意,你是鼓勵代碼實體類中使用構造方法或屬性初始化..

11

您可以在註釋或yaml中使用options屬性添加列的默認值。你可以在doctrine annotation documentation閱讀更多。

實施例用於註釋:

爲YAML
/** 
* @ORM\Column(type="bool", name="is_visible", options={"default": false}) 
*/ 
protected $isVisible; 

實施例:

isVisible: 
    type: boolean 
    column: is_visible 
    options: 
     default: false 
+0

謝謝,如果您在添加一個新列的時候擔心數據庫遷移,這看起來像唯一有效的答案,該列的默認值必須爲所有現有對象設置。在PHP中設置值只適用於新對象。 – youen 2016-09-15 11:59:57

1

的時間已經過去了。現在您可以通過yaml爲列設置默認值。

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