Type error: Argument 1 passed to Doctrine\Common\Collections\ArrayCollection::__construct() must be of the type array, object given
<p>Actually... more or less I know where is my problem.
I'm receiving this error in a particular controller where I try to <code>persist($object)</code>...</p>

<p>Actually I'm developing a webapp for me to have a register to all my books I'm reading.. and I use Google Books API for that. So, I have the next Entities:</p>

<ul>
<li>Admin</li>
<li>Users</li>
<li>Books</li>
<li>Categories </li>
</ul>

<p>I was thinking about the <code>db</code> and I wanted a table with <code>user_id, book_id</code>
so I decided to do a <code>ManyToMany</code> but, I don't know if this is the way.. or not. <code>(Because my familiars are going to use it)</code></p>

<p>It has to be like many users can have the same book and a user can have many books obviusly.</p>

<p>So, the error I'm getting I guess it's because I'm not implementing well done the <code>ManyToMany...</code> I write below the <code>Controller</code> and <code>Entities</code></p>

<p><strong>Where Users is like:</strong></p>

<pre><code>/**
 * @ORM\Entity
 * @ORM\Table(name="Users")
 * @ORM\Entity(repositoryClass="UsersRepository")
 * @UniqueEntity("username")
 * @UniqueEntity("email")
 */
class Users implements UserInterface, \Serializable
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="text")
     * @Assert\NotBlank()
     */
    private $name;

    /**
     * @ORM\Column(type="text")
     * @Assert\NotBlank()
     */
    private $lastname;

    /**
     * @ORM\Column(type="text")
     * @Assert\NotBlank()
     */
    private $username;

    /**
     * @ORM\Column(type="string", length=255, unique=true)
     * @Assert\NotBlank()
     * @Assert\Email()
     */
    private $email;

    /**
     *
     * @Assert\Length(max=4096)
     */
    private $plainPassword;

    /**
     *
     * @ORM\Column(type="string", length=64)
     */
    private $password;

    /**
     * @ORM\Column(type="text")
     * @Assert\NotBlank()
     */
    private $language;

    /**
     * @ORM\Column(type="boolean")
     */
    private $isActive;


    /*****************
     * Users constructor.
     */
    public function __construct() {
        $this-&gt;language = 'es';
        $this-&gt;isActive = true;
    }

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this-&gt;id;
    }

    /**
     * @param mixed $id
     */
    public function setId($id)
    {
        $this-&gt;id = $id;
    }

    /**
     * @return mixed
     */
    public function getName()
    {
        return $this-&gt;name;
    }

    /**
     * @param mixed $name
     */
    public function setName($name)
    {
        $this-&gt;name = $name;
    }

    /**
     * @return mixed
     */
    public function getLastname()
    {
        return $this-&gt;lastname;
    }

    /**
     * @param mixed $lastname
     */
    public function setLastname($lastname)
    {
        $this-&gt;lastname = $lastname;
    }

    /**
     * @return mixed
     */
    public function getUsername()
    {
        return $this-&gt;username;
    }

    /**
     * @param mixed $username
     */
    public function setUsername($username)
    {
        $this-&gt;username = $username;
    }

    /**
     * @return mixed
     */
    public function getEmail()
    {
        return $this-&gt;email;
    }

    /**
     * @param mixed $email
     */
    public function setEmail($email)
    {
        $this-&gt;email = $email;
    }

    /**
     * @return mixed
     */
    public function getPlainPassword()
    {
        return $this-&gt;plainPassword;
    }

    /**
     * @param mixed $plainPassword
     */
    public function setPlainPassword($plainPassword)
    {
        $this-&gt;plainPassword = $plainPassword;
    }

    /**
     * @return mixed
     */
    public function getPassword()
    {
        return $this-&gt;password;
    }

    /**
     * @param mixed $password
     */
    public function setPassword($password)
    {
        $this-&gt;password = $password;
    }

    /**
     * @return mixed
     */
    public function getLanguage()
    {
        return $this-&gt;language;
    }

    /**
     * @param mixed $language
     */
    public function setLanguage($language)
    {
        $this-&gt;language = $language;
    }

    /**
     * @return mixed
     */
    public function getIsActive()
    {
        return $this-&gt;isActive;
    }

    /**
     * @param mixed $isActive
     */
    public function setIsActive($isActive)
    {
        $this-&gt;isActive = $isActive;
    }

    //implementaciones de la interface

    public function getSalt()
    {
        // you *may* need a real salt depending on your encoder
        // see section on salt below
        return null;
    }

    public function getRoles()
    {
        return array('ROLE_USER');
    }

    public function eraseCredentials()
    {
    }

    /** @see \Serializable::serialize() */
    public function serialize()
    {
        return serialize(array(
            $this-&gt;id,
            $this-&gt;username,
            $this-&gt;password,
            $this-&gt;isActive,
        ));
    }

    /** @see \Serializable::unserialize() */
    public function unserialize($serialized)
    {
        list (
            $this-&gt;id,
            $this-&gt;username,
            $this-&gt;password,
            $this-&gt;isActive,
            ) = unserialize($serialized);
    }
}
</code></pre>

<p><strong>and Books is like:</strong></p>

<pre><code>/**
 * @ORM\Entity
 * @ORM\Table(name="Book")
 * @ORM\Entity(repositoryClass="BookRepository")
 */
class Book
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="text")
     * @Assert\NotBlank()
     */
    private $title;

    /**
     * @ORM\Column(type="text")
     * @Assert\NotBlank()
     */
    private $author;

    /**
     * @ORM\Column(type="text")
     */
    private $language;

    /**
     * @ORM\Column(type="text")
     * @Assert\NotBlank()
     */
    private $genre;

    /**
     * @ORM\Column(type="integer")
     * @Assert\NotBlank()
     */
    private $price;

    /**
     * @ORM\Column(type="text")
     */
    private $pages;

    /**
     * @ORM\Column(type="text")
     * @Assert\NotBlank()
     */
    private $synopsis;

    /**
     * @ORM\Column(type="text", nullable=true)
     */
    private $rate;

    /**
     * @ORM\Column(type="text")
     */
    private $id_google;

    /**
     * @ORM\ManyToMany(targetEntity="Users", mappedBy="books")
     */
    private $users;



    /*****************
     * Book constructor.
     */
    public function __construct() {
        $this-&gt;users    = new ArrayCollection();
    }

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this-&gt;id;
    }

    /**
     * @param mixed $id
     */
    public function setId($id)
    {
        $this-&gt;id = $id;
    }

    /**
     * @return mixed
     */
    public function getTitle()
    {
        return $this-&gt;title;
    }

    /**
     * @param mixed $title
     */
    public function setTitle($title)
    {
        $this-&gt;title = $title;
    }

    /**
     * @return mixed
     */
    public function getAuthor()
    {
        return $this-&gt;author;
    }

    /**
     * @param mixed $author
     */
    public function setAuthor($author)
    {
        $this-&gt;author = $author;
    }

    /**
     * @return mixed
     */
    public function getLanguage()
    {
        return $this-&gt;language;
    }

    /**
     * @param mixed $language
     */
    public function setLanguage($language)
    {
        $this-&gt;language = $language;
    }

    /**
     * @return mixed
     */
    public function getGenre()
    {
        return $this-&gt;genre;
    }

    /**
     * @param mixed $genre
     */
    public function setGenre($genre)
    {
        $this-&gt;genre = $genre;
    }

    /**
     * @return mixed
     */
    public function getPrice()
    {
        return $this-&gt;price;
    }

    /**
     * @param mixed $price
     */
    public function setPrice($price)
    {
        $this-&gt;price = $price;
    }

    /**
     * @return mixed
     */
    public function getPages()
    {
        return $this-&gt;pages;
    }

    /**
     * @param mixed $pages
     */
    public function setPages($pages)
    {
        $this-&gt;pages = $pages;
    }

    /**
     * @return mixed
     */
    public function getSynopsis()
    {
        return $this-&gt;synopsis;
    }

    /**
     * @param mixed $synopsis
     */
    public function setSynopsis($synopsis)
    {
        $this-&gt;synopsis = $synopsis;
    }

    /**
     * @return mixed
     */
    public function getRate()
    {
        return $this-&gt;rate;
    }

    /**
     * @param mixed $rate
     */
    public function setRate($rate)
    {
        $this-&gt;rate = $rate;
    }

    /**
     * @return mixed
     */
    public function getIdGoogle()
    {
        return $this-&gt;id_google;
    }

    /**
     * @param mixed $id_google
     */
    public function setIdGoogle($id_google)
    {
        $this-&gt;id_google = $id_google;
    }

    /**
     * @return mixed
     */
    public function getUsers()
    {
        return $this-&gt;users;
    }

    /**
     * @param mixed $users
     */
    public function setUsers(Users $users = null)
    {
        $this-&gt;users = $users;
    }

    /**
     * Add user
     *
     * @param \AppBundle\Entity\Users $user
     *
     * @return Book
     */
    public function addUser(Users $user)
    {
        $this-&gt;users[] = $user;

        return $this;
    }

    /**
     * Remove user
     *
     * @param \AppBundle\Entity\Users $user
     */
    public function removeUser(Users $user)
    {
        $this-&gt;users-&gt;removeElement($user);
    }
}
</code></pre>

<p><strong>LibraryController</strong></p>

<pre><code>class LibraryController extends BaseController
{
    private $APIkey = "&amp;key=" . "AIzaSyAN638WbYe1vOfGya989p7ZbfXnPzBLfkg";
    /**
     * @Route ("/libreria", name="libreria")
     */
    public function getLibreria(){

        $securityContext = $this-&gt;container-&gt;get('security.authorization_checker');
        if ($securityContext-&gt;isGranted('IS_AUTHENTICATED_REMEMBERED')) {
            $em = $this-&gt;getDoctrine()-&gt;getManager();

            $user = $this-&gt;get('security.token_storage')-&gt;getToken()-&gt;getUser();

            $this-&gt;addData('user', $user);
            return $this-&gt;render('AppBundle:libreria:libreria.html.twig', $this-&gt;getData());
        }

        return $this-&gt;redirect("login");
    }

    /**
     * Adds to the library the requested book
     * @Route ("/libreria/addBook/{id_google}", name="addBook")
     */
    public function addBook($id_google){

        $securityContext = $this-&gt;container-&gt;get('security.authorization_checker');
        if ($securityContext-&gt;isGranted('IS_AUTHENTICATED_REMEMBERED')) {
            $em   = $this-&gt;getDoctrine()-&gt;getManager();

            $user = $this-&gt;get('security.token_storage')-&gt;getToken()-&gt;getUser();

            $infoBook   = $this-&gt;getBookFromAPI($id_google); // Cogemos toda la información del libro en cuestión
            $vInfo      = $infoBook['volumeInfo']; // Para ahorrarnos código y usar este subarray de manera practica
            $book       = new Book();


            /**
             * Hacemos los seters pertinentes para poner el contenido en el libro
             * y dejarlo listo para poder hacer un flush
             */
            $book-&gt;setTitle($vInfo['title']);
            $book-&gt;setAuthor($vInfo['authors'][0]);
            $book-&gt;setLanguage($vInfo['language']);
            $book-&gt;setGenre($vInfo['categories'][0]);
            $book-&gt;setPrice($infoBook['saleInfo']['retailPrice']['amount']);
            $book-&gt;setPages($vInfo['printedPageCount']);
            $book-&gt;setSynopsis($vInfo['description']);

            if($vInfo['averageRating']){
                $book-&gt;setRate($vInfo['averageRating']);
            }

            $book-&gt;setIdGoogle($id_google);
            $book-&gt;setUsers($user);

            // Guardamos el libro en la bbdd
            $em-&gt;persist($book);
            $em-&gt;flush();

            $this-&gt;sendResponseStatus('OK');

            // Generamos los datos para la respuesta ajax
            return new JSONResponse($this-&gt;getData());
        }

        return $this-&gt;redirect("login");
    }


    // Coge la información de un libro gracias a su id
    public function getBookFromAPI($id){
        // Instancia del BuzzBundle para peticiones HTTP externas
        $buzz = $this-&gt;container-&gt;get('buzz');

        // Resultado de la peticion HTTP 'GET'
        $responseAPI = $buzz-&gt;get('https://www.googleapis.com/books/v1/volumes/'.$id);
        $jsonLibro = $responseAPI-&gt;getContent();

        return $this-&gt;transformarStringToArray($jsonLibro);
    }


    // Se usa para hacer que el String devuelto por la API de Google sea un array
    public function transformarStringToArray($string){
        $stringDecoded = json_decode($string);
        $array = json_decode(json_encode($stringDecoded), true);

        return $array;

    }
}
</code></pre>

<p>So, the error appears I guess when I try to persist the <code>db</code> because there is something bad implemented...</p>

<p><strong>Any clue or solution?</strong></p>

<p><strong>Thanks to everyone! Have a nice day!</strong></p>

<p><strong>Victor</strong></p>