2012-07-11 74 views
3

我從git倉庫中使用了Doctrine MongoDB ODM和Symfony2的master分支以及mongo擴展1.2.10。Doctrine MongoDB ODM nullable = false不起作用

我已經創建了一些類/文件類似於註釋:

namespace Acme\StoreBundle\Document; 

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB; 

/** 
* @MongoDB\Document 
*/ 
class Person 
{ 
    /** 
    * @MongoDB\Id 
    */ 
    protected $id; 

    /** 
    * @MongoDB\String(nullable=false) 
    */ 
    protected $name; 

    /** 
    * @MongoDB\ReferenceOne(targetDocument="PersonType", inversedBy="person", nullable=false) 
    */ 
    protected $personType; 
} 

當我創建並堅持一個新的文檔,而無需設置我收到任何錯誤的值或引用。我誤解了可以使用nullable選項,並且需要在生命週期回調中調用驗證代碼,錯誤地使用註釋,或者可能是Doctrine中的錯誤?

+0

當然這是我的谷歌搜索的最後一個鏈接... [鏈接](https://github.com/doctrine/mongodb-odm/blob/master/lib/Doctrine/ODM/MongoDB/Persisters /PersistenceBuilder.php) 我假設可空選項與ORM中的工作方式相同,但實際上是控制空值是否存儲在數據庫中。看起來我必須創建一些驗證邏輯來​​執行約束。 – CrEOF 2012-07-11 16:58:32

回答

1

use Symfony\Component\Validator\Constraints as Assert;

/** * @mongodb\Field(name="name", type="string") * @Assert\NotBlank() */ private $name;

希望你得到的線索。

1

如果檢出annotation reference,則設置nullable=false並不意味着它將在空值上驗證失敗。

從文檔:

爲空的 - 默認情況下,ODM將$unset領域MongoDB中,如果PHP值爲null。爲此選項指定true以強制ODM將空值存儲在數據庫中,而不是取消設置該字段。

相關問題