Doctrine2.1: Search by DisccriminatorColumn in an Unknown Field Exception

I tried to find this error, but the fact that I did not find anything leads me to believe that I am doing something stupid. I will include the appropriate code below, but I mainly use multiple table inheritance (or Class Inheritance ) and try to use the Doctrine ORM findBy () to query based on the discriminator column, resulting in the following ORMException: "Unrecognized field: type".

Here is the code that throws the exception:

// $this->em is an instance of \Doctrine\ORM\EntityManager $repository = $this->em->getRepository('JoeCommentBundle:Thread'); return $repository->findOneBy(array( 'type' => $this->type, 'related_id' => $id )); 

Here is the appropriate code for the abstract entity "base":

 <?php namespace Joe\Bundle\CommentBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Gedmo\Mapping\Annotation as Gedmo; use Doctrine\Common\Collections\ArrayCollection; /** * @ORM\Entity * @ORM\Table(name="comment_threads") * @ORM\InheritanceType("JOINED") * @ORM\DiscriminatorColumn(name="type", type="string") * @ORM\DiscriminatorMap( {"story" = "Joe\Bundle\StoryBundle\Entity\StoryThread"} ) */ abstract class Thread { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @ORM\Column(name="related_id", type="integer") */ protected $relatedId; /** MORE FIELDS BELOW.... **/ 

And finally, here is the code for a specific thread object:

 <?php namespace Joe\Bundle\StoryBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Joe\Bundle\CommentBundle\Entity\Thread as AbstractThread; /** * @ORM\Entity * @ORM\Table(name="story_comment_threads") */ class StoryThread extends AbstractThread { /** * @ORM\OneToOne(targetEntity="Story") * @ORM\JoinColumn(name="story_id", referencedColumnName="id") */ protected $story; } 

I double checked my schema and the type column definitely exists, so I'm not sure what could be causing this. Any ideas? Thanks.

+4
source share
2 answers

Rob, when you request the actual use of the parent object and try to filter the discriminator value. Instead, work on the repository with respect to the child you want to retrieve. The doctrine will do the rest for you. Therefore, in your case, you want to get a repository for StoryThread .

 $repository = $this->em->getRepository('JoeCommentBundle:StoryThread'); return repository->find($id); 
+14
source

You cannot use the discriminator column as a standard property of an object.

Instead, you can do the following:

 $dql = 'SELECT e FROM JoeCommentBundle:Thread e WHERE e.related_id = :related_id AND e INSTANCE OF :type'; $query = $em->createQuery($dql); $query->setParameters(array( 'type' => $this->type, 'related_id' => $id )); $record = $query->getSingleResult(); 
+10
source

Source: https://habr.com/ru/post/1411964/


All Articles