问题描述
我有两个实体产品和类别,我已经成功地创建了许多关系,因此创建了一个新表,如下所示: ┌──────────────────────────────────────────────────────────────────────────────────────────── ┐┌─────────────
|产品| | product_categories | |类别|
├────────────┤├───────────────────────────────────────────────────────────────────────────────────────────── ────────┤
| id#├---------┤product_id | ┌----┤id#|
| - | | cat_id├----┘| - |
| - | | | | |
└─────────────└───────────────────────────────────────────────────────────────────────────────────── ────────┘
分类
/ **
* @ ORM\ManyToMany(targetEntity =Products,mappedBy =category )
* /
protected $ product;
产品
/ **
* @ ORM\ManyToMany(targetEntity =categories,inversedBy =product)
* @ ORM\JoinTable product_categories,
* joinColumns = {@ ORM\JoinColumn(name =product_id,referencedColumnName =id)},
* inverseJoinColumns = {@ ORM\JoinColumn(name =cat_id ,referencedColumnName =id)}
*)
** /
protected $ category;
现在我希望product_categories表中的产品和cat_id是唯一的。如何可以完成?
如果您通过注释(<$)隐式指定多对多关系c $ c> @ManyToMany )Doctrine将创建复合主键,包含两个字段:product_id和cat_id,因此您可以保证它是唯一的。
如果您通过附加实体(让我们说 ProductCategories
)明确指定了您的多对多关联,您可以添加UniqueEntity约束。
注释示例:
使用Symfony\Bridge\Doctrine\\ \\Validator\Constraints\UniqueEntity;
/ **
* @ ORM\Entity
*(...)
* @UniqueEntity(fields = {product_id,cat_id} )
* /
class ProductCategories {
//(...)
编辑:
此外,请确保您已正确生成数据库。您应该使用Doctrine的命令来做到这一点(不要手动执行!):
php -f ./app/console doctrine:database:drop
php -f ./app/console doctrine:database:create
php -f ./app/console doctrine:schema $ create
这样,根据您的实体,Doctrine将创建适当的数据库模式。如果您将来会更改任何内容(在您的实体中),您将可以更新模式:
php - f ./app/console doctrine:schema:update --force
(以上大部分命令你还可以添加 - dump-sql
参数,结果只显示sql而不是更改数据库)
I have two entities products and categories, i have successfully created a many-many relationship b/w them, as a result a new table has been created which looks like :
┌────────────────┐ ┌───────────────────┐ ┌────────────────┐
| products | | product_categories| | categories |
├────────────────┤ ├───────────────────┤ ├────────────────┤
| id# ├---------┤ product_id | ┌----┤ id# |
| -- | | cat_id ├----┘ | -- |
| -- | | | | |
└────────────────┘ └───────────────────┘ └────────────────┘
Categories
/**
* @ORM\ManyToMany(targetEntity="Products", mappedBy="category")
*/
protected $product;
Products
/**
* @ORM\ManyToMany(targetEntity="categories", inversedBy="product")
* @ORM\JoinTable(name="product_categories",
* joinColumns={@ORM\JoinColumn(name="product_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="cat_id", referencedColumnName="id")}
* )
**/
protected $category;
Now i want product and cat_id to be unique in product_categories table. how it can be done ?
If you specify your many-to-many relation implicitly for example through an annotation (@ManyToMany
) Doctrine will create composite primary key consists with both fields: product_id and cat_id, so you have a guarantee that it would be unique.
If you specify your many-to-many associations explicitly through additional entity (let's say ProductCategories
) you could just add UniqueEntity constraint.
UniqueEntity constraint documentation
An example with annotations:
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ORM\Entity
* (...)
* @UniqueEntity(fields={"product_id", "cat_id"})
*/
class ProductCategories {
// (...)
edit:
In addition make sure that you've generated your database correctly. You should use Doctrine's commands to do that (don't do it manually!):
php -f ./app/console doctrine:database:drop
php -f ./app/console doctrine:database:create
php -f ./app/console doctrine:schema:create
In that way, based on your entities, Doctrine will create appropriate db schema. If you will change anything in future (in your entities), you'll be able to just update your schema:
php -f ./app/console doctrine:schema:update --force
(with most of above commands you can also add --dump-sql
argument which results in showing only sql instead of changing your database)
这篇关于加入表symfony2中的唯一ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!