JMS序列化程序无法序列化:无法访问私有属性

时间:2013-08-10 00:26:24

标签: php mysql symfony doctrine-orm jmsserializerbundle

我正在尝试使用JMS Serializer包将Doctrine2实体序列化为JSON。我已经设置了所有内容,但是当我尝试将某些内容序列化为JSON时,我收到以下错误:

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

这是我的控制器代码:

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
  );
}

最后,这是我的区实体:

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

有谁知道为什么会这样?如果我在我的实体中将所有内容设置为公开,似乎我可以得到更多,但有些东西告诉我,我不应该这样做。如果我这样做,我收到一个Symfony2错误说:

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'

提前感谢您提供的任何帮助!

1 个答案:

答案 0 :(得分:1)

点击这里

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

您可以指定访问类型,在您的情况下,您应该保持变量private / protected并添加/** @Accessor(getter="getId") */注释以告诉JMS如何获取id。

另外,正如@forgottenbas所述,您可能还需要添加@Type("integer")注释。

相关问题