为啥 symfony 学说 findall() 返回 id NULL
Posted
技术标签:
【中文标题】为啥 symfony 学说 findall() 返回 id NULL【英文标题】:Why symfony doctrine findall() return id NULL为什么 symfony 学说 findall() 返回 id NULL 【发布时间】:2016-01-17 20:25:10 【问题描述】:我有服务等级:
<?php
namespace AppBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Service
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="AppBundle\Entity\ServiceRepository")
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="type", type="string")
* @ORM\DiscriminatorMap("newsletter" = "Newsletter", "email" = "Email", "service" = "Service")
*
*/
class Service
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="serviceTitle", type="string", length=255)
*/
private $serviceTitle;
/**
* Get id
*
* @return integer
*/
public function getId()
return $this->id;
/**
* Set serviceTitle
*
* @param string $serviceTitle
*
* @return Service
*/
public function setServiceTitle($serviceTitle)
$this->serviceTitle = $serviceTitle;
return $this;
/**
* Get serviceTitle
*
* @return string
*/
public function getServiceTitle()
return $this->serviceTitle;
/**
* @ORM\ManyToOne(targetEntity="Service", inversedBy="children")
*/
private $parent;
/**
* @ORM\OneToMany(targetEntity="Service", mappedBy="parent")
*/
private $children;
/**
* Constructor
*/
public function __construct()
$this->children = new \Doctrine\Common\Collections\ArrayCollection();
/**
* Set parent
*
* @param \AppBundle\Entity\Service $parent
*
* @return Service
*/
public function setParent(\AppBundle\Entity\Service $parent = null)
$this->parent = $parent;
return $this;
/**
* Get parent
*
* @return \AppBundle\Entity\Service
*/
public function getParent()
return $this->parent;
/**
* Add child
*
* @param \AppBundle\Entity\Service $child
*
* @return Service
*/
public function addChild(\AppBundle\Entity\Service $child)
$this->children[] = $child;
return $this;
/**
* Remove child
*
* @param \AppBundle\Entity\Service $child
*/
public function removeChild(\AppBundle\Entity\Service $child)
$this->children->removeElement($child);
/**
* Get children
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getChildren()
return $this->children;
和扩展服务类的电子邮件类:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Email
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="AppBundle\Entity\EmailRepository")
*/
class Email extends Service
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="emailAddress", type="string", length=255)
*/
private $emailAddress;
/**
* Get id
*
* @return integer
*/
public function getId()
return $this->id;
/**
* Set emailAddress
*
* @param string $emailAddress
*
* @return Email
*/
public function setEmailAddress($emailAddress)
$this->emailAddress = $emailAddress;
return $this;
/**
* Get emailAddress
*
* @return string
*/
public function getEmailAddress()
return $this->emailAddress;
我为电子邮件生成 crud,我的电子邮件控制器索引操作显示电子邮件列表,它有学说 findall() 函数,但问题是,这个 findall 函数返回 ID->NULL 的实体
谁能告诉我为什么它返回null???
public function indexAction()
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('AppBundle:Email')->findAll();
var_dump($entities);
exit();
return $this->render('AppBundle:Email:index.html.twig', array(
'entities' => $entities,
));
【问题讨论】:
为什么在扩展类中又定义了id? id应该继承自Service吧? 啊,在创建实体 ID 时自动创建,但之后我忘了删除,现在我删除了,它工作正常,现在 ID 与 FindAll() 一起来谢谢人 谢谢 - 你应该接受@Wilt 的回答。 ***.com/questions/33229741/…可以请你回答这个,回答页尾,最后评论。 【参考方案1】:您有一个基类 Service
并在您的 Email
类中扩展该类。您还在那里重新定义了id
列。这是不允许的。
您的问题可能与此有关。更多信息请查看the Doctrine2 documentation on attribute override。
我建议您从您的Email
实体中完全删除 id 列:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Email
*
* @ORM\Entity(repositoryClass="AppBundle\Entity\EmailRepository")
*/
class Email extends Service
/**
* @var string
*
* @ORM\Column(name="emailAddress", type="string", length=255)
*/
private $emailAddress;
/**
* Set emailAddress
*
* @param string $emailAddress
*
* @return Email
*/
public function setEmailAddress($emailAddress)
$this->emailAddress = $emailAddress;
return $this;
/**
* Get emailAddress
*
* @return string
*/
public function getEmailAddress()
return $this->emailAddress;
【讨论】:
【参考方案2】:您应该将mapped superclass 用于服务类。 它的字段将被映射,但出现在数据库中的唯一实体将是电子邮件。
<?php
/** @MappedSuperclass */
class Service
/** @Column(type="string") */
protected $serviceTitle;
// ... more fields and methods
/** @Entity */
class Email extends Service
/** @Id @Column(type="integer") */
private $id;
/** @Column(type="string") */
private $emailAddress;
// ... more fields and methods
【讨论】:
答案中有错字,但已修复,我认为现在有效。以上是关于为啥 symfony 学说 findall() 返回 id NULL的主要内容,如果未能解决你的问题,请参考以下文章
致命错误:调用未定义的方法 MyModule\Entity\MyEntity::findAll() 学说 orm 2 zf2