我最终得到了:
Argument 1 passed to Entity\User::addCategories() must be an instance of Entity\Category, string given,
我有很多关系,用户,user_category和类别
用户
<?php
namespace Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
/**
* @Entity
* @Table(name="user")
*/
class User
{
/**
* @Id
* @Column(type="integer", nullable=false)
* @GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @Column(type="string", length=255, unique=true, nullable=false)
*/
public $name;
/**
* @ManyToMany(targetEntity="Entity\Category", inversedBy="user")
* @ORM\JoinTable(name="user_category")
*/
public $categories;
public function __construct() {
$this->category = new \Doctrine\Common\Collections\ArrayCollection();
}
public function getCategories()
{
return $this->categories;
}
public function addCategories(Category $category = null)
{
$this->categories = $category;
}
public function setName($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
}
类别
<?php
namespace Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
/**
* @Entity
* @Table(name="category")
*/
class Category
{
/**
* @Id
* @Column(type="integer", nullable=false)
* @GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @Column(type="string", length=255, unique=true, nullable=false)
*/
public $name;
/**
* @ManyToMany(targetEntity="Entity\User", mappedBy="category")
*/
public $user;
public function __construct() {
$this->user = new \Doctrine\Common\Collections\ArrayCollection();
}
public function getUser()
{
return $this->user;
}
public function addUser(User $user = null)
{
$user->addCategory($this);
$this->user = $user;
}
public function setName($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
}
插入功能
// check existence object in database
$res = $this->em->find('Entity\User', $this->input->post('id'));
if($res){
$data = $this->em->find('Entity\User', $this->input->post('id'));
}else{
// create a new User object
$data = new Entity\User;
}
$data->setName($this->input->post('name'));
$data->addCategories($this->input->post('category'));
// save the data object to the database
$this->em->persist($data);
$this->em->flush();
一切都很顺利,但我很困惑,因为开始工作。
感谢您的帮助。 对不起我的英文。
答案 0 :(得分:10)
你有很多错误(注意语法):
而不是
public $categories;
public function __construct() {
$this->category = new \Doctrine\Common\Collections\ArrayCollection();
}
它应该是:
protected $categories;
public function __construct() {
$this->categories = new \Doctrine\Common\Collections\ArrayCollection();
}
而不是:
public $user;
public function __construct() {
$this->user = new \Doctrine\Common\Collections\ArrayCollection();
}
使用
protected $users;
public function __construct() {
$this->users = new \Doctrine\Common\Collections\ArrayCollection();
}
而不是
public function addCategories(Category $category = null)
{
$this->categories = $category;
}
一定是
public function addCategory(Category $category = null)
{
$this->categories->add($category);
}
和
public function removeCategory(Category $category)
{
$this->categories->removeElement($category) ;
}
public function setCategories($categories)
{
$this->categories = categories;
}
双方的逻辑相同。我不知道CI的工作原理,但Symfony会自动找到addSomething / removeSomething方法。即使CI不支持该功能,您仍应按上述方式更改代码。