Eu tenho algum promblem. Eu estava pesquisando e tentei todos sugerir, mas ninguém trabalha.
e eu acabei com:
Argument 1 passed to Entity\User::addCategories() must be an instance of Entity\Category, string given,
Tenho muitos relacionamentos, usuário, categoria de usuário e categoria
do utilizador
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; } }
categoria
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; } }
Inserir function
// 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();
Tudo está bem em começar, mas eu sou tão confuso para trabalhar.
Obrigado pela ajuda. Desculpe pelo meu Inglês.
Você tem muitos erros (preste atenção à gramática):
ao invés de
public $categories; public function __construct() { $this->category = new \Doctrine\Common\Collections\ArrayCollection(); }
deveria ser:
protected $categories; public function __construct() { $this->categories = new \Doctrine\Common\Collections\ArrayCollection(); }
ao invés de:
public $user; public function __construct() { $this->user = new \Doctrine\Common\Collections\ArrayCollection(); }
usar
protected $users; public function __construct() { $this->users = new \Doctrine\Common\Collections\ArrayCollection(); }
Ao invés de
public function addCategories(Category $category = null) { $this->categories = $category; }
deve ser
public function addCategory(Category $category = null) { $this->categories->add($category); }
e
public function removeCategory(Category $category) { $this->categories->removeElement($category) ; } public function setCategories($categories) { $this->categories = categories; }
A mesma lógica em ambos os lados. Não sei como a CI funciona, mas a Symfony encontrará automaticamente methods addSomething / removeSomething. Mesmo que a CI não ofereça suporte a esse recurso, você ainda deve alterar seu código como acima.