Reputation: 598
I use the auto-generated getters in a class table inheritance setup in a Symfony project. getId()
returns null, while every other getter works. Can you spot any problem? What should I search for? I imported the database entries manually, but I don't think that is the cause.
//src/Acme/WebzineBundle/Entity/Content.php
namespace Acme\WebzineBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Content
*
* @ORM\Table()
* @ORM\Entity
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="heading", type="integer")
* @ORM\DiscriminatorMap({
* 0 = "Review"
* })
*/
abstract class Content
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var \DateTime
*
* @ORM\Column(name="edited", type="date")
*/
private $edited;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Get edited
*
* @return \DateTime
*/
public function getEdited()
{
return $this->edited;
}
}
//src/Acme/WebzineBundle/Entity/Review.php
namespace Acme\WebzineBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Review articles
*
* @ORM\Table()
* @ORM\Entity
*/
class Review extends Content
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=127)
*/
private $title;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
}
The next foreign key constraint is on the table of the child entity:
CONSTRAINT `FK_7EEF84F0BF396750` FOREIGN KEY (`id`) REFERENCES `Content` (`id`)
ON DELETE CASCADE
//src/Acme/AdminBundle/Controller/MainController.php
namespace Acme\AdminBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class MainController extends Controller
{
public function indexAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery(
'SELECT post FROM AcmeWebzineBundle:Content post
ORDER BY post.edited DESC'
);
$query->setMaxResults(30);
$posts = $query->getResult();
$latest_post = $posts[0];
return $this->render('AcmeAdminBundle:Main:index.html.twig', array(
'posts' => $posts,
'id' => gettype($latest_post->getId()), // This returns null!
'edited' => $latest_post->getEdited(), // Any other getter works
'title' => $latest_post->getTitle(), // also from the child entity.
));
}
}
Upvotes: 5
Views: 4479
Reputation: 12306
You need to remove the id
property and getId()
method from the child class
//src/Acme/WebzineBundle/Entity/Review.php
namespace Acme\WebzineBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Review articles
*
* @ORM\Table()
* @ORM\Entity
*/
class Review extends Content
{
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=127)
*/
private $title;
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
}
You can not create object of Content
class.
And better use only unique properties and methods in Review
class, because others are inherited from abstract Content
class.
Upvotes: 8