1.3Symfony模型(Doctrine)之关联(Association)

Symfony支持ManyToOne, OneToMany, ManyToMany, OneToOne关联。

$ php bin/console make:entity Category

New property name (press <return> to stop adding fields):
> name

Field type (enter ? to see all types) [string]:
> string

Field length [255]:
> 255

Can this field be null in the database (nullable) (yes/no) [no]:
> no

New property name (press <return> to stop adding fields):
>
(press enter again to finish)
// src/Entity/Category.php
namespace App\Entity;

// ...

class Category
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string")
     */
    private $name;

    // ... getters and setters
}
$ php bin/console make:entity

Class name of the entity to create or update (e.g. BraveChef):
> Product

New property name (press <return> to stop adding fields):
> category

Field type (enter ? to see all types) [string]:
> relation

What class should this entity be related to?:
> Category

What type of relationship is this?
 ------------ ------------------------------------------------------------------- 
  Type         Description                                                        
 ------------ ------------------------------------------------------------------- 
  ManyToOne    Each Product relates to (has) one Category.                            
               Each Category can relate to (can have) many Product objects            
                                                                                  
  OneToMany    Each Product can relate to (can have) many Category objects.           
               Each Category relates to (has) one Product                             
                                                                                  
  ManyToMany   Each Product can relate to (can have) many Category objects.           
               Each Category can also relate to (can also have) many Product objects  
                                                                                  
  OneToOne     Each Product relates to (has) exactly one Category.                    
               Each Category also relates to (has) exactly one Product.               
 ------------ ------------------------------------------------------------------- 

Relation type? [ManyToOne, OneToMany, ManyToMany, OneToOne]:
> ManyToOne

Is the Product.category property allowed to be null (nullable)? (yes/no) [yes]:
> no

Do you want to add a new property to Category so that you can access/update
Product objects from it - e.g. $category->getProducts()? (yes/no) [yes]:
> yes

New field name inside Category [products]:
> products

Do you want to automatically delete orphaned App\Entity\Product objects
(orphanRemoval)? (yes/no) [no]:
> no

New property name (press <return> to stop adding fields):
>
(press enter again to finish)

在product中添加category实现了ManyToOne。但是后来在category中添加products实现了OneToMany并不是必须的,这是可选的,除非您想使用$category->getProducts()去查询一个category关联的多个Products。

 php bin/console doctrine:migrations:diff
 php bin/console doctrine:migrations:migrate

新增关联实体

$category = new Category();
$category->setName('Computer Peripherals');

$product = new Product();
$product->setName('Keyboard');
$product->setPrice(19.99);
$product->setDescription('Ergonomic and stylish!');

// relates this product to the category
$product->setCategory($category);

$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($category);
$entityManager->persist($product);
$entityManager->flush();

查询关联实体

$product = $this->getDoctrine()
            ->getRepository(Product::class)
            ->find($id);

        // ...

$categoryName = $product->getCategory()->getName();

原创文章,作者:huoxiaoqiang,如若转载,请注明出处:https://www.huoxiaoqiang.com/php/symfony/6322.html

发表评论

登录后才能评论