php - JMS serializer can't serialize: cannot access private property -
i'm trying serialize doctrine2 entity json using jms serializer bundle. have set up, when try serialize json following error:
fatal error: cannot access private property snow\frontbundle\entity\district::$id in e:\school\socialgeo augustus\coding\socialgeo-php\vendor\jms\metadata\src\metadata\propertymetadata.php on line 46
this controller code:
public function indexaction() { $em = $this->getdoctrine()->getmanager(); $districts = $em->getrepository('snowfrontbundle:district')->findall(); $serializer = $this->get('jms_serializer'); echo $serializer->serialize($districts, 'json'); return array( 'districts' => $districts ); }
and lastly, district entity:
<?php namespace snow\frontbundle\entity; use doctrine\orm\mapping orm; /** * district * * @orm\table(name="district") * @orm\entity(repositoryclass="snow\frontbundle\entity\repositories\districtrepository") */ class district { /** * @var integer * * @orm\column(name="id", type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ public $id; /** * @var string * * @orm\column(name="name", type="string", length=100) */ public $name; /** * @var string * * @orm\column(name="bigimage", type="string", length=100) */ public $bigimage; /** * @var string * * @orm\column(name="smallimage", type="string", length=100) */ public $smallimage; /** * @var string * * @orm\column(name="info", type="text") */ public $info; /** * * @orm\manytoone(targetentity="city", inversedby="districts") * @orm\joincolumn(name="city_id", referencedcolumnname="id") */ protected $city; /** * * @orm\onetomany(targetentity="district", mappedby="city") */ protected $locations; /** * * @var \doctrine\common\collections\arraycollection $articles * @orm\manytomany(targetentity="article", mappedby="districts") */ protected $articles; /** * * @var \doctrine\common\collections\arraycollection $workers * * @orm\manytomany(targetentity="user", inversedby="districts") * @orm\jointable(name="workers_districts", * joincolumns={@orm\joincolumn(name="district_id", referencedcolumnname="id")}, * inversejoincolumns={@orm\joincolumn(name="user_id", referencedcolumnname="id")} * ) */ protected $workers; /** * * @var \doctrine\common\collections\arraycollection $userbookmarks * @orm\manytomany(targetentity="user", mappedby="favdistricts") */ protected $userbookmarks; /** * id * * @return integer */ public function getid() { return $this->id; } /** * set name * * @param string $name * @return district */ public function setname($name) { $this->name = $name; return $this; } /** * name * * @return string */ public function getname() { return $this->name; } /** * set info * * @param string $info * @return district */ public function setinfo($info) { $this->info = $info; return $this; } /** * info * * @return string */ public function getinfo() { return $this->info; } /** * set city * * @param \snow\frontbundle\entity\city $city * @return district */ public function setcity(\snow\frontbundle\entity\city $city = null) { $this->city = $city; return $this; } /** * city * * @return \snow\frontbundle\entity\city */ public function getcity() { return $this->city; } /** * constructor */ public function __construct() { $this->locations = new \doctrine\common\collections\arraycollection(); } /** * add locations * * @param \snow\frontbundle\entity\district $locations * @return district */ public function addlocation(\snow\frontbundle\entity\district $locations) { $this->locations[] = $locations; return $this; } /** * remove locations * * @param \snow\frontbundle\entity\district $locations */ public function removelocation(\snow\frontbundle\entity\district $locations) { $this->locations->removeelement($locations); } /** * locations * * @return \doctrine\common\collections\collection */ public function getlocations() { return $this->locations; } /** * add articles * * @param \snow\frontbundle\entity\article $articles * @return district */ public function addarticle(\snow\frontbundle\entity\article $articles) { $this->articles[] = $articles; return $this; } /** * remove articles * * @param \snow\frontbundle\entity\article $articles */ public function removearticle(\snow\frontbundle\entity\article $articles) { $this->articles->removeelement($articles); } /** * articles * * @return \doctrine\common\collections\collection */ public function getarticles() { return $this->articles; } /** * * @return string */ public function __tostring() { return $this->getname(); } /** * add workers * * @param \snow\frontbundle\entity\user $workers * @return district */ public function addworker(\snow\frontbundle\entity\user $workers) { $this->workers[] = $workers; return $this; } /** * remove workers * * @param \snow\frontbundle\entity\user $workers */ public function removeworker(\snow\frontbundle\entity\user $workers) { $this->workers->removeelement($workers); } /** * workers * * @return \doctrine\common\collections\collection */ public function getworkers() { return $this->workers; } /** * set bigimage * * @param string $bigimage * @return district */ public function setbigimage($bigimage) { $this->bigimage = $bigimage; return $this; } /** * bigimage * * @return string */ public function getbigimage() { return $this->bigimage; } /** * set smallimage * * @param string $smallimage * @return district */ public function setsmallimage($smallimage) { $this->smallimage = $smallimage; return $this; } /** * smallimage * * @return string */ public function getsmallimage() { return $this->smallimage; } /** * add userbookmarks * * @param \snow\frontbundle\entity\user $userbookmarks * @return district */ public function adduserbookmark(\snow\frontbundle\entity\user $userbookmarks) { $this->userbookmarks[] = $userbookmarks; return $this; } /** * remove userbookmarks * * @param \snow\frontbundle\entity\user $userbookmarks */ public function removeuserbookmark(\snow\frontbundle\entity\user $userbookmarks) { $this->userbookmarks->removeelement($userbookmarks); } /** * userbookmarks * * @return \doctrine\common\collections\collection */ public function getuserbookmarks() { return $this->userbookmarks; } }
does know why happening? seems can bit further if set public in entity, tells me i'm not supposed that. if so, symfony2 error saying:
an exception occurred while executing 'select t0.id id1, t0.title title2, t0.path path3, t0.description description4, t0.createddate createddate5, t0.publisheddate publisheddate6, t0.deleteddate deleteddate7, t0.published published8, t0.url url9, t0.location_id location_id10, t0.user_id user_id11, t0.mediatype_id mediatype_id12 media t0 t0.user_id = ?' params {"1":6}: sqlstate[42s22]: column not found: 1054 unknown column 't0.location_id' in 'field list'
thanks in advance can provide!
check here
http://jmsyst.com/libs/serializer/master/reference/annotations#accesstype
you can specify access type, in case should keep variables private/protected , add /** @accessor(getter="getid") */
annotation tell jms how id.
also, noted @forgottenbas, may need add @type("integer")
annotation well.
Comments
Post a Comment