Question

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:

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 is my 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, this is my District entity:

<?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->id;
  }

  /**
   * Set name
   *
   * @param string $name
   * @return District
   */
  public function setName($name) {
    $this->name = $name;

    return $this;
  }

  /**
   * Get 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;
  }

  /**
   * Get 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;
  }

  /**
   * Get 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);
  }

  /**
   * Get 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);
  }

  /**
   * Get 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);
  }

  /**
   * Get 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;
  }

  /**
   * Get 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;
  }

  /**
   * Get 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);
  }

  /**
   * Get userbookmarks
   *
   * @return \Doctrine\Common\Collections\Collection
   */
  public function getUserbookmarks() {
    return $this->userbookmarks;
  }
}

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:

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'

Thanks in advance for any help you can provide!

Was it helpful?

Solution

Check here

http://jmsyst.com/libs/serializer/master/reference/annotations#accesstype

You can specify the access type, in your case you should keep the variables private/protected and add the /** @Accessor(getter="getId") */ annotation to tell JMS how to get to the id.

Also, as noted by @forgottenbas, you may also need to add the @Type("integer") annotation as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top