繁体   English   中英

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

[英]JMS serializer can't serialize: cannot access private property

我正在尝试使用JMS序列化程序包将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'

预先感谢您提供的任何帮助!

在这里检查

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

您可以指定访问类型,在这种情况下,应将变量设置为私有/受保护,并添加/** @Accessor(getter="getId") */批注以告知JMS如何获取ID。

同样,如@forgottenbas所述,您可能还需要添加@Type("integer")批注。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM