爲了解決一個problem I asked about earlier,我想創建一個自定義的庫函數,將確定的Repair
一個實例是否是唯一的基礎上,device
,name
和colors
限制。學說:自定義庫,以確定UniqueEntity不起作用
這是我爲Repair
類主義詮釋。請注意,device
屬性是多對一(許多修復爲一個設備),並且colors
是多對多。
/**
* @ORM\Table(name="repair")
* @ORM\Entity(repositoryClass="AppBundle\Repository\RepairRepository")
* @UniqueEntity(fields={"name", "device", "colors"}, repositoryMethod="getSimilarRepairs", message="Repair {{ value }} already exists for this name, device and colour combination.")
*/
這是我的RepairRepository.php,其中$criteria['colors']
是一個數組。
public function getSimilarRepairs(array $criteria) {
$builder = $this->createQueryBuilder('r')
->where('r.device = :device')
->andWhere('r.colors = :colors')
->andWhere('r.name = :name')
->setParameters(['deviceid'=>$criteria['device'],'colors'=>$criteria['colors'],'name'=>$criteria['name']]);
return $builder->getQuery();
}
我有大概可以帶回一個三個問題:
- 編輯:每變化,造成重複或不,我得到一個重複的實體存在的消息。
- 編輯:儘管有錯誤消息,名稱更改仍然執行!
- 添加:我可以創建儘可能多的重複項,因爲我喜歡,從來沒有錯誤消息。
你是否看到這篇文章? http://stackoverflow.com/questions/16148678/symfony-2-uniqueentity-repositorymethod-fails-on-update-entity – DOZ
Yous自定義查詢與默認查詢的作用相同。這是正常的,它不會工作:)我會試一試。 – Alsatian