php - Call to a member function on a non-object on modify -
i trying update value try set returns error
error: call member function setpguid() on non-object in d:\xampp\htdocs\symfony\src\cq\intranetbundle\controller\defaultcontroller.php line 19
this code defaultcontroller , since copied symfony2 docs have not slightest id might wrong.
class defaultcontroller extends controller { public function indexaction() { $em = $this->getdoctrine()->getmanager(); $karte = $em->getrepository('cqintranetbundle:karte')->findbypguid('00f8e77c-d8c2-4c7f-8e4a-384cc7689da0'); if (!$karte) { throw $this->createnotfoundexception( 'no product found id ' ); } $karte->setpguid('new product name!'); $em->flush(); } }
also if a
exit(\doctrine\common\util\debug::dump($karte));
it returns this
array(1) { [0]=> object(stdclass)#497 (9) { ["__class__"]=> string(30) "cq\intranetbundle\entity\karte" ["pguid"]=> string(36) "00f8e77c-d8c2-4c7f-8e4a-384cc7689da0" ["pbarcode"]=> string(13) "3321231140001" ["panreise"]=> string(8) "datetime" ["pabreise"]=> string(8) "datetime" ["pmguid"]=> string(36) "07b7c4cf-3441-4339-9b3c-1a581e1271ba" ["pdatecreate"]=> string(8) "datetime" ["pdatemodify"]=> string(8) "datetime" ["pdateaccess"]=> string(8) "datetime" } }
according documentation:
http://symfony.com/doc/current/book/doctrine.html
you using method returns array, no matter how many results return.
in example, may return 1 result, since key using unique.
in case can use:
$karte = $em->getrepository('cqintranetbundle:karte')->findbypguid('00f8e77c-d8c2-4c7f-8e4a-384cc7689da0')[0];
which directly point $karte
first (and only) object array.
the same result achieve using method returns 1 result, previous answerer pointed findoneby...()
Comments
Post a Comment