Abid
Abid

Reputation: 74

Why symfony doctrine findall() return id NULL

i have service class:

  <?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;
    }
}

and email class which extends servic class:

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;
    }
}

i generate crud for email and my email controller index action showing list of emails, it have doctrine findall() function but problem is , this findall function return entity with ID->NULL

can any one tell me why its return 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

Upvotes: 0

Views: 1945

Answers (2)

Roubi
Roubi

Reputation: 2106

You should use a mapped superclass for class Service. Its fields will be mapped, but the only entity appearing in the database will be Email.

<?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
}

Upvotes: -1

Wilt
Wilt

Reputation: 44326

You have a base class Service and extend this class in your Email class. You also redefine the id column there. This is not allowed.

Your problem probably relates to this. Check the Doctrine2 documentation on attribute override for more information.

I suggest you remove the id column totally from your Email entity:

<?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;
    }
}

Upvotes: 2

Related Questions