Doctrine orm Zend Framework 2条令2多对多实体关系问题
我有以下表格:电影、类别、电影和实体: Film.phpDoctrine orm Zend Framework 2条令2多对多实体关系问题,doctrine-orm,many-to-many,entity,zend-framework2,Doctrine Orm,Many To Many,Entity,Zend Framework2,我有以下表格:电影、类别、电影和实体: Film.php <?php namespace Admin\Entity; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Table(name="films") */ class Film{ /*
<?php
namespace Admin\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table(name="films")
*/
class Film{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
* @ORM\Column(length=11)
*/
private $id;
/**
* @ORM\Column(type="string")
*/
private $name;
/* .... */
/**
* @ORM\ManyToMany(targetEntity="Category")
* @ORM\JoinTable(name="films_categories",
* joinColumns={@ORM\JoinColumn(name="film_id", referencedColumnName = "id")},
* inverseJoinColumns={@ORM\JoinColumn(name="category_id", referencedColumnName="id")})
*/
private $categories;
public function __construct(){
$this->categories = new ArrayCollection();
}
public function getCategoriesNames(){
$names = array();
foreach($this->categories as $category){
$names[] = $category->getName();
}
return $names;
}
public function getId(){
return $this->id;
}
public function setId($id){
$this->id = $id;
}
/* ... */
/**
* @return Collection
*/
public function getCategories(){
return $this->categories;
}
public function addCategories(Collection $categories){
foreach($categories as $category){
$this->categories->add($category);
}
}
public function removeCategories(Collection $categories){
foreach($categories as $category){
$this->categories->removeElement($category);
}
}
}
结果是出现各种错误和异常,并显示消息“在关联Admin\entity\Film\categories上找到类型为的实体,但应为Admin\entity\categories”
请帮帮我,我真的被这吓坏了!谢谢:)您需要为targetEntity定义FQCN 更改:
* @ORM\ManyToMany(targetEntity="Category")
致:
我从中发现,您的实体正在接收Admin\entity\Film\categories,在这部分中,categories是一个什么样的值。其中,您的实体需要类型为Admin\entity\Film\categories的对象 要克服这一点,你必须创建一个
public function SetCategory(Admin\Entity\Category Category)
{
$this->categories(or category or w/e your variable name is)= Category;
}
public function getCategory()
{
return $this->categories(or category or w/e your variable name is);
}
然后,在你的动作中,你必须将类别的对象传递给电影实体
$Film->SetCategory($categoryObj);
当然,您必须根据您的部件设置您的业务逻辑,但是此方法应该消除此错误 这与水合作用和惰性负荷有关。我不是这方面的专家,但是#categories是一个代理对象,当您要保存它时,它会抱怨,因为它需要是一个实际的集合,而不是集合的代理对象 如果您使用noobie php的方法,那么您将在主对象上附加新的类别对象,因此它们不再是代理
这是令人沮丧的,因为教义本来应该可以省去很多麻烦,但它实际上并没有达到你们在很多情况下自然期望的效果。我在不久前发现了一个错误通知单,但现在找不到它-如果我能找到它,我会将它添加到此中。只要实体在同一名称空间中,它们就不需要完全限定;)从错误中我发现,Admin\Entity\Film#categories意味着您正在向您的实体传递一些值,因为它期望一个类型为“Admin\Entity\Category”的对象。经过一些修改,它实际上起了作用,尽管我以前尝试过类似的方法。谢谢:)不客气,如果你不介意的话,你也可以发布修改,这样其他人也可以得到帮助
public function addAction() {
$em = $this->getEntityManager();
$form = $this->getForm();
$film = new Film();
$form->bind($film);
if($request->isPost()){
$post = array_merge_recursive(
$request->getPost()->toArray(),
$request->getFiles()->toArray()
);
$form->setData($post);
if($form->isValid()){
$categories = array();
foreach($post['film']['categories'] as $categoryId){
$categories[] = $em->getRepository('Admin\Entity\Category')->find($categoryId);
}
$film->addCategories($categories);
$em->persist($film);
$em->flush();
}else{
// the form is not valid
}
}
* @ORM\ManyToMany(targetEntity="Category")
* @ORM\ManyToMany(targetEntity="Admin\Entity\Category")
public function SetCategory(Admin\Entity\Category Category)
{
$this->categories(or category or w/e your variable name is)= Category;
}
public function getCategory()
{
return $this->categories(or category or w/e your variable name is);
}
$Film->SetCategory($categoryObj);