且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

加入表symfony2中的唯一ID

更新时间:2023-09-18 23:11:34

如果您通过注释( @ManyToMany )Doctrine将创建复合主键,包含两个字段:product_id和cat_id,因此您可以保证它是唯一的。



如果您通过附加实体(让我们说 ProductCategories )明确指定了您的多对多关联,您可以添加UniqueEntity约束。



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)