I have a problem with my code. I have this error :
The property "users" in class "Wiki\AdminBundle\Entity\Post" can be defined with the methods "addUser()", "removeUser()" but the new value must be an array or an instance of \Traversable, "Wiki\AdminBundle\Entity\User" given.
I don't understand what's happened with my code.
This is my FormType :
<?php
namespace Wiki\AdminBundle\Form;
use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Tests\Fixtures\Entity;
use Wiki\AdminBundle\Entity\Post;
class PostType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title')
->add('users', EntityType::class, [
'class' => 'WikiAdminBundle:User',
'choice_label' => 'username',
])
->add('status')
->add('save', SubmitType::class)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Post::class,
]);
}
}
And here my Post Entity :
<?php
namespace Wiki\AdminBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Wiki\AdminBundle\Traits\Entity as EntityTraits;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity
* @ORM\Table(name="post")
* @ORM\Entity(repositoryClass="Wiki\AdminBundle\Entity\Repository\PostRepository")
* @ORM\HasLifecycleCallbacks
*/
class Post
{
use EntityTraits\IdTrait;
use EntityTraits\DateCreated;
/**
* @ORM\Column(name="title", type="text")
*/
private $title;
/**
* @ORM\ManyToMany(targetEntity="Wiki\AdminBundle\Entity\User", mappedBy="posts", cascade={"persist"})
* @ORM\Column(name="author", type="text")
*/
private $users;
/**
* @ORM\Column(name="status", type="integer")
*/
private $status;
public function __construct()
{
$this->users = new ArrayCollection();
}
/**
* @return mixed
*/
public function getTitle()
{
return $this->title;
}
/**
* @param mixed $title
*/
public function setTitle($title)
{
$this->title = $title;
}
/**
* @return mixed
*/
public function getStatus()
{
return $this->status;
}
/**
* @param mixed $status
*/
public function setStatus($status)
{
$this->status = $status;
}
public function addUser(User $user)
{
$this->users->add($user);
$user->addPost($this);
}
public function removeUser(User $user)
{
$this->users->removeElement($user);
}
/**
* @return mixed
*/
public function getUsers()
{
return $this->users;
}
}
Can someone help me to resolve my problem ?
best regards,