php - Type error: Argument 1 passed to Doctrine\Common\Collections\ArrayCollection::__construct() must be of the type array, object given -


actually... more or less know problem. i'm receiving error in particular controller try persist($object)...

actually i'm developing webapp me have register books i'm reading.. , use google books api that. so, have next entities:

  • admin
  • users
  • books
  • categories

i thinking db , wanted table user_id, book_id decided manytomany but, don't know if way.. or not. (because familiars going use it)

it has many users can have same book , user can have many books obviusly.

so, error i'm getting guess it's because i'm not implementing done manytomany... write below controller , entities

where users like:

/**  * @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->language = 'es';         $this->isactive = true;     }      /**      * @return mixed      */     public function getid()     {         return $this->id;     }      /**      * @param mixed $id      */     public function setid($id)     {         $this->id = $id;     }      /**      * @return mixed      */     public function getname()     {         return $this->name;     }      /**      * @param mixed $name      */     public function setname($name)     {         $this->name = $name;     }      /**      * @return mixed      */     public function getlastname()     {         return $this->lastname;     }      /**      * @param mixed $lastname      */     public function setlastname($lastname)     {         $this->lastname = $lastname;     }      /**      * @return mixed      */     public function getusername()     {         return $this->username;     }      /**      * @param mixed $username      */     public function setusername($username)     {         $this->username = $username;     }      /**      * @return mixed      */     public function getemail()     {         return $this->email;     }      /**      * @param mixed $email      */     public function setemail($email)     {         $this->email = $email;     }      /**      * @return mixed      */     public function getplainpassword()     {         return $this->plainpassword;     }      /**      * @param mixed $plainpassword      */     public function setplainpassword($plainpassword)     {         $this->plainpassword = $plainpassword;     }      /**      * @return mixed      */     public function getpassword()     {         return $this->password;     }      /**      * @param mixed $password      */     public function setpassword($password)     {         $this->password = $password;     }      /**      * @return mixed      */     public function getlanguage()     {         return $this->language;     }      /**      * @param mixed $language      */     public function setlanguage($language)     {         $this->language = $language;     }      /**      * @return mixed      */     public function getisactive()     {         return $this->isactive;     }      /**      * @param mixed $isactive      */     public function setisactive($isactive)     {         $this->isactive = $isactive;     }      //implementaciones de la interface      public function getsalt()     {         // *may* need real salt depending on 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->id,             $this->username,             $this->password,             $this->isactive,         ));     }      /** @see \serializable::unserialize() */     public function unserialize($serialized)     {         list (             $this->id,             $this->username,             $this->password,             $this->isactive,             ) = unserialize($serialized);     } } 

and books like:

/**  * @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->users    = new arraycollection();     }      /**      * @return mixed      */     public function getid()     {         return $this->id;     }      /**      * @param mixed $id      */     public function setid($id)     {         $this->id = $id;     }      /**      * @return mixed      */     public function gettitle()     {         return $this->title;     }      /**      * @param mixed $title      */     public function settitle($title)     {         $this->title = $title;     }      /**      * @return mixed      */     public function getauthor()     {         return $this->author;     }      /**      * @param mixed $author      */     public function setauthor($author)     {         $this->author = $author;     }      /**      * @return mixed      */     public function getlanguage()     {         return $this->language;     }      /**      * @param mixed $language      */     public function setlanguage($language)     {         $this->language = $language;     }      /**      * @return mixed      */     public function getgenre()     {         return $this->genre;     }      /**      * @param mixed $genre      */     public function setgenre($genre)     {         $this->genre = $genre;     }      /**      * @return mixed      */     public function getprice()     {         return $this->price;     }      /**      * @param mixed $price      */     public function setprice($price)     {         $this->price = $price;     }      /**      * @return mixed      */     public function getpages()     {         return $this->pages;     }      /**      * @param mixed $pages      */     public function setpages($pages)     {         $this->pages = $pages;     }      /**      * @return mixed      */     public function getsynopsis()     {         return $this->synopsis;     }      /**      * @param mixed $synopsis      */     public function setsynopsis($synopsis)     {         $this->synopsis = $synopsis;     }      /**      * @return mixed      */     public function getrate()     {         return $this->rate;     }      /**      * @param mixed $rate      */     public function setrate($rate)     {         $this->rate = $rate;     }      /**      * @return mixed      */     public function getidgoogle()     {         return $this->id_google;     }      /**      * @param mixed $id_google      */     public function setidgoogle($id_google)     {         $this->id_google = $id_google;     }      /**      * @return mixed      */     public function getusers()     {         return $this->users;     }      /**      * @param mixed $users      */     public function setusers(users $users = null)     {         $this->users = $users;     }      /**      * add user      *      * @param \appbundle\entity\users $user      *      * @return book      */     public function adduser(users $user)     {         $this->users[] = $user;          return $this;     }      /**      * remove user      *      * @param \appbundle\entity\users $user      */     public function removeuser(users $user)     {         $this->users->removeelement($user);     } } 

librarycontroller

class librarycontroller extends basecontroller {     private $apikey = "&key=" . "aizasyan638wbye1vofgya989p7zbfxnpzblfkg";     /**      * @route ("/libreria", name="libreria")      */     public function getlibreria(){          $securitycontext = $this->container->get('security.authorization_checker');         if ($securitycontext->isgranted('is_authenticated_remembered')) {             $em = $this->getdoctrine()->getmanager();              $user = $this->get('security.token_storage')->gettoken()->getuser();              $this->adddata('user', $user);             return $this->render('appbundle:libreria:libreria.html.twig', $this->getdata());         }          return $this->redirect("login");     }      /**      * adds library requested book      * @route ("/libreria/addbook/{id_google}", name="addbook")      */     public function addbook($id_google){          $securitycontext = $this->container->get('security.authorization_checker');         if ($securitycontext->isgranted('is_authenticated_remembered')) {             $em   = $this->getdoctrine()->getmanager();              $user = $this->get('security.token_storage')->gettoken()->getuser();              $infobook   = $this->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->settitle($vinfo['title']);             $book->setauthor($vinfo['authors'][0]);             $book->setlanguage($vinfo['language']);             $book->setgenre($vinfo['categories'][0]);             $book->setprice($infobook['saleinfo']['retailprice']['amount']);             $book->setpages($vinfo['printedpagecount']);             $book->setsynopsis($vinfo['description']);              if($vinfo['averagerating']){                 $book->setrate($vinfo['averagerating']);             }              $book->setidgoogle($id_google);             $book->setusers($user);              // guardamos el libro en la bbdd             $em->persist($book);             $em->flush();              $this->sendresponsestatus('ok');              // generamos los datos para la respuesta ajax             return new jsonresponse($this->getdata());         }          return $this->redirect("login");     }       // coge la información de un libro gracias su id     public function getbookfromapi($id){         // instancia del buzzbundle para peticiones http externas         $buzz = $this->container->get('buzz');          // resultado de la peticion http 'get'         $responseapi = $buzz->get('https://www.googleapis.com/books/v1/volumes/'.$id);         $jsonlibro = $responseapi->getcontent();          return $this->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;      } } 

so, error appears guess when try persist db because there bad implemented...

any clue or solution?

thanks everyone! have nice day!

victor

no $book->setusers($user); $book->adduser($user);

and, in general, method setusers not valid

public function setusers(users $users = null) {     $this->users = $users; } 

you must not redefine property after being initialized in constructor. can add or remove elements it, assigning new value break doctrine's functionality.


Comments

Popular posts from this blog

elasticsearch python client - work with many nodes - how to work with sniffer -

angular - Is it possible to get native element for formControl? -

Upload file with tags through OwnCloud or NextCloud API -