为什么symfony doctrine findall()返回id为NULL

时间:2015-10-19 13:07:44

标签: php symfony doctrine-orm doctrine

我有服务类:

  <?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和我的电子邮件控制器索引操作显示电子邮件列表,它有doctrine findall()函数,但问题是,这个findall函数返回实体ID-&gt; 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,
    ));
}

enter image description here

2 个答案:

答案 0 :(得分:2)

您有一个基类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;
    }
}

答案 1 :(得分:-1)

您应该使用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
}