我创建了一个名为 SimpleMedia 的自定义捆绑包。当我尝试加载加载公司的页面时,它会给我运行时通知
运行时注意:访问静态属性Proxies__CG __ \ SimpleMediaBundle \ Entity \ Media :: $ lazyPropertiesDefaults为非静态。
我知道这与我的一对一关系有关
(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();
}
}
答案 0 :(得分:0)
我认为你已经把关系定义错了。我认为应该是:
/**
*
* @ORM\OneToOne(targetEntity="SimpleMediaBundle\Entity\Media", cascade={"persist", "remove"}, orphanRemoval=true)
* @ORM\JoinColumn(name="logo_file", referencedColumnName="id", nullable=true)
*/
protected $logoFile;
另一方面,在公司构造函数中,您不需要初始化$ logoFile变量,因为它不是数组,只是一个值。
答案 1 :(得分:0)
将获取模式更改为 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;