Mik*_*ike 4 symfony doctrine-orm
我对此感到头疼,我找不到解决办法.
我有2个实体:Movie.php和Category.php
我希望一部电影有多个类别,反之亦然.这就是我选择ManyToMany关系的原因.
现在我想知道......数据库网站上发生了什么?是否存在将movie_ids映射到category_ids的"中间"表?但事实并非如此.实际上我的第一次尝试是创建一个MovieCategory实体 - 我使用OneToMany将一部电影映射到多个类别,并且在MovieCategory实体中我创建了OneToOne连接以从我的Category实体获取类别名称.但我想这不是它应该如何运作,我是对的吗?
现在这是我的代码,我认为它应该如何工作,我真的很感激我能得到的任何帮助:
Movie.php
<?php
/**
* @ORM\Table(name="movies")
* @ORM\HasLifecycleCallbacks()
*/
class Movie
{
public function __construct()
{
$this->categories = new ArrayCollection();
}
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/** @ORM\Column(type="string") */
protected $moviename;
/**
* @ORM\ManyToMany(targetEntity="Category", mappedBy="movie")
*/
protected $categories;
}
Run Code Online (Sandbox Code Playgroud)
Category.php
<?php
/**
* @ORM\Table(name="categories")
* @ORM\HasLifecycleCallbacks()
*/
class Category
{
public function __construct()
{
$this->movies = new ArrayCollection();
}
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string")
*/
protected $name;
// ...
/**
* @ORM\ManyToMany(targetEntity="Movie", mappedBy="movie", cascade={"persist"})
*/
protected $movies;
}
Run Code Online (Sandbox Code Playgroud)
Mol*_*Man 12
根据Doctrine文档,它应该是这样的:
// Movie.php
/**
* @ORM\ManyToMany(targetEntity="Category", inversedBy="movies")
* @ORM\JoinTable(name="movies_categories")
*/
protected $categories;
// ...
// Category.php
/**
* @ORM\ManyToMany(targetEntity="Movie", mappedBy="categories")
*/
protected $movies;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6697 次 |
| 最近记录: |