Note that there are some explanatory texts on larger screens.

plurals
  1. POJMS serializer can't serialize: cannot access private property
    primarykey
    data
    text
    <p>I'm trying to serialize a Doctrine2 entity to JSON using the JMS Serializer bundle. I have everything set up, but when I try to serialize something to JSON I get the following error:</p> <p><code>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</code></p> <p>This is my controller code:</p> <pre><code>public function indexAction() { $em = $this-&gt;getDoctrine()-&gt;getManager(); $districts = $em-&gt;getRepository('SnowFrontBundle:District')-&gt;findAll(); $serializer = $this-&gt;get('jms_serializer'); echo $serializer-&gt;serialize($districts, 'json'); return array( 'districts' =&gt; $districts ); } </code></pre> <p>And lastly, this is my District entity:</p> <pre><code>&lt;?php namespace Snow\FrontBundle\Entity; use Doctrine\ORM\Mapping as 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; /** * Get id * * @return integer */ public function getId() { return $this-&gt;id; } /** * Set name * * @param string $name * @return District */ public function setName($name) { $this-&gt;name = $name; return $this; } /** * Get name * * @return string */ public function getName() { return $this-&gt;name; } /** * Set info * * @param string $info * @return District */ public function setInfo($info) { $this-&gt;info = $info; return $this; } /** * Get info * * @return string */ public function getInfo() { return $this-&gt;info; } /** * Set city * * @param \Snow\FrontBundle\Entity\City $city * @return District */ public function setCity(\Snow\FrontBundle\Entity\City $city = null) { $this-&gt;city = $city; return $this; } /** * Get city * * @return \Snow\FrontBundle\Entity\City */ public function getCity() { return $this-&gt;city; } /** * Constructor */ public function __construct() { $this-&gt;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-&gt;locations[] = $locations; return $this; } /** * Remove locations * * @param \Snow\FrontBundle\Entity\District $locations */ public function removeLocation(\Snow\FrontBundle\Entity\District $locations) { $this-&gt;locations-&gt;removeElement($locations); } /** * Get locations * * @return \Doctrine\Common\Collections\Collection */ public function getLocations() { return $this-&gt;locations; } /** * Add articles * * @param \Snow\FrontBundle\Entity\Article $articles * @return District */ public function addArticle(\Snow\FrontBundle\Entity\Article $articles) { $this-&gt;articles[] = $articles; return $this; } /** * Remove articles * * @param \Snow\FrontBundle\Entity\Article $articles */ public function removeArticle(\Snow\FrontBundle\Entity\Article $articles) { $this-&gt;articles-&gt;removeElement($articles); } /** * Get articles * * @return \Doctrine\Common\Collections\Collection */ public function getArticles() { return $this-&gt;articles; } /** * * @return string */ public function __toString() { return $this-&gt;getName(); } /** * Add workers * * @param \Snow\FrontBundle\Entity\User $workers * @return District */ public function addWorker(\Snow\FrontBundle\Entity\User $workers) { $this-&gt;workers[] = $workers; return $this; } /** * Remove workers * * @param \Snow\FrontBundle\Entity\User $workers */ public function removeWorker(\Snow\FrontBundle\Entity\User $workers) { $this-&gt;workers-&gt;removeElement($workers); } /** * Get workers * * @return \Doctrine\Common\Collections\Collection */ public function getWorkers() { return $this-&gt;workers; } /** * Set bigimage * * @param string $bigimage * @return District */ public function setBigimage($bigimage) { $this-&gt;bigimage = $bigimage; return $this; } /** * Get bigimage * * @return string */ public function getBigimage() { return $this-&gt;bigimage; } /** * Set smallimage * * @param string $smallimage * @return District */ public function setSmallimage($smallimage) { $this-&gt;smallimage = $smallimage; return $this; } /** * Get smallimage * * @return string */ public function getSmallimage() { return $this-&gt;smallimage; } /** * Add userbookmarks * * @param \Snow\FrontBundle\Entity\User $userbookmarks * @return District */ public function addUserbookmark(\Snow\FrontBundle\Entity\User $userbookmarks) { $this-&gt;userbookmarks[] = $userbookmarks; return $this; } /** * Remove userbookmarks * * @param \Snow\FrontBundle\Entity\User $userbookmarks */ public function removeUserbookmark(\Snow\FrontBundle\Entity\User $userbookmarks) { $this-&gt;userbookmarks-&gt;removeElement($userbookmarks); } /** * Get userbookmarks * * @return \Doctrine\Common\Collections\Collection */ public function getUserbookmarks() { return $this-&gt;userbookmarks; } } </code></pre> <p>Does anyone know why this is happening? It seems like I can get a bit further if I set everything to public in my entity, but something tells me I'm not supposed to do that. If I do so, I get a Symfony2 error saying:</p> <pre><code>An exception occurred while executing 'SELECT t0.id AS id1, t0.title AS title2, t0.path AS path3, t0.description AS description4, t0.createddate AS createddate5, t0.publisheddate AS publisheddate6, t0.deleteddate AS deleteddate7, t0.published AS published8, t0.url AS url9, t0.location_id AS location_id10, t0.user_id AS user_id11, t0.mediatype_id AS mediatype_id12 FROM media t0 WHERE t0.user_id = ?' with params {"1":6}: SQLSTATE[42S22]: Column not found: 1054 Unknown column 't0.location_id' in 'field list' </code></pre> <p>Thanks in advance for any help you can provide!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload