Symfony 运行时通知:访问静态属性代理\\uuu CG\uuuu\SimpleDiabundle\Entity\Media::$LazyProperties默认为非静态

Symfony 运行时通知:访问静态属性代理\\uuu CG\uuuu\SimpleDiabundle\Entity\Media::$LazyProperties默认为非静态,symfony,Symfony,我创建了一个名为SimpleMedia的自定义包。当我尝试加载加载公司的页面时,它会给我运行时通知 运行时通知:访问静态属性代理\uuuu CG\uuu\simpleDiabundle\Entity\Media::$LazyProperties默认为非静态 我知道这和我一对一的关系有关 (logoFile(Company Entity) -> id (Media Entity). 有人有什么想法吗?我正在使用Symfony 2.7 控制器 /** * @Route("/{id}

我创建了一个名为SimpleMedia的自定义包。当我尝试加载加载公司的页面时,它会给我运行时通知

运行时通知:访问静态属性代理\uuuu CG\uuu\simpleDiabundle\Entity\Media::$LazyProperties默认为非静态

我知道这和我一对一的关系有关

(logoFile(Company Entity) -> id (Media Entity).
有人有什么想法吗?我正在使用Symfony 2.7

控制器

  /**
   * @Route("/{id}", name="api_company_show")
   * @Method("GET")
   * @param $id
   * @return Response $response
   */
  public function showAction($id) {
    $company = $this->findCompanyById($id);

    $response = $this->createApiResponse($company, 200);
    return $response;
  }

  /**
   * Find company by id. Throw exception if it does not.
   * @param $id
   * @return Company $company
   */
  private function findCompanyById($id) {
    $company = $this->getDoctrine()
      ->getRepository('AppBundle:Company')
      ->find($id);

    if (!$company) {
      throw $this->createNotFoundException(sprintf(
        'No company found with id "%d"',
        $id
      ));
    }

    return $company;
  }
Company.php

  namespace AppBundle\Entity;

  use Doctrine\ORM\Mapping as ORM;
  use Doctrine\Common\Collections\ArrayCollection;
  use AppBundle\Entity\User;
  use Symfony\Component\HttpFoundation\File\File;
  use SimpleMediaBundle\Entity\Media;

  /**
   * Company
   * @ORM\Table()
   * @ORM\Entity(repositoryClass="AppBundle\Repository\CompanyRepository")
   */
  class Company
  {
    /**
     * @var integer
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /**
     * @var string
     * @ORM\Column(name="website", type="string", length=255, nullable=true)
     */
    private $website;

    /**
     * @var string
     * @ORM\Column(name="address_1", type="string", length=255, nullable=true)
     */
    private $address1;

    /**
     * @var string
     * @ORM\Column(name="address_2", type="string", length=255, nullable=true)
     */
    private $address2;

    /**
     * @var string
     * @ORM\Column(name="city", type="string", length=255, nullable=true)
     */
    private $city;

    /**
     * @var string
     * @ORM\Column(name="state", type="string", length=255, nullable=true)
     */
    private $state;

    /**
     * @var string
     * @ORM\Column(name="zipcode", type="string", length=50, nullable=true)
     */
    private $zipcode;

    /**
     * @var string
     * @ORM\Column(name="country", type="string", length=255, nullable=true)
     */
    private $country;

    /**
     * @var string
     * @ORM\Column(name="phone", type="string", length=50,  nullable=true)
     */
    private $phone;

    /**
     * @var string
     * @ORM\Column(name="fax", type="string", length=50,  nullable=true)
     */
    private $fax;

    /**
     * @var integer
     * @ORM\Column(name="years_in_business", type="integer", length=50,  nullable=true)
     */
    private $yearsInBusiness;

    /**
     * @var string
     * @ORM\Column(name="number_of_employees", type="integer", length=50,  nullable=true)
     */
    private $numberOfEmployees;

    /**
     * @var integer
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\User")
     */
    private $owners;

    /**
     * @ORM\OneToOne(targetEntity="SimpleMediaBundle\Entity\Media", cascade={"persist", "remove"}, orphanRemoval=true)
     * @ORM\JoinColumns({
     * @ORM\JoinColumn(name="logo_file", referencedColumnName="id", nullable=true)
     * })
     */
    protected $logoFile;

    /**
     * @ORM\Column(type="datetime", name="updated", nullable=true)
     * @var \DateTime $updated
     */
    protected $updated;


    public function __construct()
    {
      $this->owners = new ArrayCollection();
      $this->logoFile = new ArrayCollection();
    }

    /**
     * Get id
     * @return integer
     */
    public function getId()
    {
      return $this->id;
    }

    // Basic Getter and Setters

    /**
     * Set logo file id.
     * @param Media $media
     * @return $this
     */
    public function setLogoFile(Media $media)
    {
      $this->logoFile = $media;

      return $this;
    }

    /**
     * Get Log File.
     * @return File
     */
    public function getLogoFile()
    {
      return $this->logoFile;
    }

  }
namespace SimpleMediaBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use SimpleMediaBundle\Entity\MediaInterface;

/**
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="SimpleMediaBundle\Repository\MediaRepository")
 */
class Media implements MediaInterface
{
  /**
   * @var integer $id
   * @ORM\Column(name="id", type="integer", nullable=false)
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  private $id;

  /**
   * @var string $name
   * @ORM\Column(name="name", type="string", length=255)
   */
  private $name;

  /**
   * @var string $path
   * @ORM\Column(name="path", type="string", length=500)
   */
  private $path;

  /**
   * @var string $size
   * @ORM\Column(name="size", type="integer", nullable=true)
   */
  private $size;

  /**
   * @var string $createdAt
   * @ORM\Column(name="created_at", type="datetime")
   */
  private $createdAt;

  /**
   * @var string $createdAt
   * @ORM\Column(name="updated_at", type="datetime")
   */
  private $updatedAt;

  /**
   * @var string $contentType
   * @ORM\Column(name="content_type", type="string", length=50, nullable=true)
   */
  private $contentType;

  /**
   * @var string $type
   * @ORM\Column(name="type", type="string", length=255)
   */
  private $type;

  public function __construct()
  {
    $this->createdAt = new \DateTime();
    $this->updatedAt = new \DateTime();
  }

  /**
   * Get id
   * @return integer
   */
  public function getId()
  {
    return $this->id;
  }

  // Basic Getter and Setters

  /**
   * Transform to string
   * @return string
   */
  public function __toString()
  {
    return (string) $this->getId();
  }

}
Media.php

  namespace AppBundle\Entity;

  use Doctrine\ORM\Mapping as ORM;
  use Doctrine\Common\Collections\ArrayCollection;
  use AppBundle\Entity\User;
  use Symfony\Component\HttpFoundation\File\File;
  use SimpleMediaBundle\Entity\Media;

  /**
   * Company
   * @ORM\Table()
   * @ORM\Entity(repositoryClass="AppBundle\Repository\CompanyRepository")
   */
  class Company
  {
    /**
     * @var integer
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /**
     * @var string
     * @ORM\Column(name="website", type="string", length=255, nullable=true)
     */
    private $website;

    /**
     * @var string
     * @ORM\Column(name="address_1", type="string", length=255, nullable=true)
     */
    private $address1;

    /**
     * @var string
     * @ORM\Column(name="address_2", type="string", length=255, nullable=true)
     */
    private $address2;

    /**
     * @var string
     * @ORM\Column(name="city", type="string", length=255, nullable=true)
     */
    private $city;

    /**
     * @var string
     * @ORM\Column(name="state", type="string", length=255, nullable=true)
     */
    private $state;

    /**
     * @var string
     * @ORM\Column(name="zipcode", type="string", length=50, nullable=true)
     */
    private $zipcode;

    /**
     * @var string
     * @ORM\Column(name="country", type="string", length=255, nullable=true)
     */
    private $country;

    /**
     * @var string
     * @ORM\Column(name="phone", type="string", length=50,  nullable=true)
     */
    private $phone;

    /**
     * @var string
     * @ORM\Column(name="fax", type="string", length=50,  nullable=true)
     */
    private $fax;

    /**
     * @var integer
     * @ORM\Column(name="years_in_business", type="integer", length=50,  nullable=true)
     */
    private $yearsInBusiness;

    /**
     * @var string
     * @ORM\Column(name="number_of_employees", type="integer", length=50,  nullable=true)
     */
    private $numberOfEmployees;

    /**
     * @var integer
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\User")
     */
    private $owners;

    /**
     * @ORM\OneToOne(targetEntity="SimpleMediaBundle\Entity\Media", cascade={"persist", "remove"}, orphanRemoval=true)
     * @ORM\JoinColumns({
     * @ORM\JoinColumn(name="logo_file", referencedColumnName="id", nullable=true)
     * })
     */
    protected $logoFile;

    /**
     * @ORM\Column(type="datetime", name="updated", nullable=true)
     * @var \DateTime $updated
     */
    protected $updated;


    public function __construct()
    {
      $this->owners = new ArrayCollection();
      $this->logoFile = new ArrayCollection();
    }

    /**
     * Get id
     * @return integer
     */
    public function getId()
    {
      return $this->id;
    }

    // Basic Getter and Setters

    /**
     * Set logo file id.
     * @param Media $media
     * @return $this
     */
    public function setLogoFile(Media $media)
    {
      $this->logoFile = $media;

      return $this;
    }

    /**
     * Get Log File.
     * @return File
     */
    public function getLogoFile()
    {
      return $this->logoFile;
    }

  }
namespace SimpleMediaBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use SimpleMediaBundle\Entity\MediaInterface;

/**
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="SimpleMediaBundle\Repository\MediaRepository")
 */
class Media implements MediaInterface
{
  /**
   * @var integer $id
   * @ORM\Column(name="id", type="integer", nullable=false)
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  private $id;

  /**
   * @var string $name
   * @ORM\Column(name="name", type="string", length=255)
   */
  private $name;

  /**
   * @var string $path
   * @ORM\Column(name="path", type="string", length=500)
   */
  private $path;

  /**
   * @var string $size
   * @ORM\Column(name="size", type="integer", nullable=true)
   */
  private $size;

  /**
   * @var string $createdAt
   * @ORM\Column(name="created_at", type="datetime")
   */
  private $createdAt;

  /**
   * @var string $createdAt
   * @ORM\Column(name="updated_at", type="datetime")
   */
  private $updatedAt;

  /**
   * @var string $contentType
   * @ORM\Column(name="content_type", type="string", length=50, nullable=true)
   */
  private $contentType;

  /**
   * @var string $type
   * @ORM\Column(name="type", type="string", length=255)
   */
  private $type;

  public function __construct()
  {
    $this->createdAt = new \DateTime();
    $this->updatedAt = new \DateTime();
  }

  /**
   * Get id
   * @return integer
   */
  public function getId()
  {
    return $this->id;
  }

  // Basic Getter and Setters

  /**
   * Transform to string
   * @return string
   */
  public function __toString()
  {
    return (string) $this->getId();
  }

}

我认为你对这段关系的定义是错误的。我认为应该是:

/**
 *
 * @ORM\OneToOne(targetEntity="SimpleMediaBundle\Entity\Media", cascade={"persist", "remove"}, orphanRemoval=true)
 * @ORM\JoinColumn(name="logo_file", referencedColumnName="id", nullable=true)
 */
protected $logoFile;

另一方面,在company构造函数中,您不需要初始化$logoFile变量,因为它不是数组,只是一个值。

我认为您定义的关系是错误的。我认为应该是:

/**
 *
 * @ORM\OneToOne(targetEntity="SimpleMediaBundle\Entity\Media", cascade={"persist", "remove"}, orphanRemoval=true)
 * @ORM\JoinColumn(name="logo_file", referencedColumnName="id", nullable=true)
 */
protected $logoFile;

另一方面,在公司构造函数中,您不需要初始化$logoFile变量,因为它不是一个数组,它只是一个值。

将获取模式更改为“渴望”为我解决了这个问题

  /**
   * @var Media
   *
   * @ORM\OneToOne(targetEntity="SimpleMediaBundle\Entity\Media", cascade={"persist", "remove"}, orphanRemoval=true, fetch="EAGER")
   * @ORM\JoinColumn(name="logo_file", referencedColumnName="id", nullable=true)
   */
  private $logoFile;

将fetch模式更改为EAGER为我解决了这个问题

  /**
   * @var Media
   *
   * @ORM\OneToOne(targetEntity="SimpleMediaBundle\Entity\Media", cascade={"persist", "remove"}, orphanRemoval=true, fetch="EAGER")
   * @ORM\JoinColumn(name="logo_file", referencedColumnName="id", nullable=true)
   */
  private $logoFile;

在MAMPNo上不在本地运行在MAMPI上不在本地运行我想在这种情况下它应该是@ORM\JoinColumn而不是@ORM\JoinColumns。我想你是对的,它不是ArrayCollection()。我做了这些更改,但仍然存在相同的问题。我认为在这种情况下,应该是@ORM\JoinColumn而不是@ORM\JoinColumns。我想你是对的,它不是ArrayCollection()。我做了这些更改,但仍然存在相同的问题。