Skip to content

Instantly share code, notes, and snippets.

@tugrul
Created December 21, 2018 19:48
Show Gist options
  • Select an option

  • Save tugrul/2ea4c107021a18af48c7d36bcccec13e to your computer and use it in GitHub Desktop.

Select an option

Save tugrul/2ea4c107021a18af48c7d36bcccec13e to your computer and use it in GitHub Desktop.
doctrine result proxy entity
<?php
namespace ForumBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Class LastThread
* @package ForumBundle\Entity
*/
class LastThread
{
/**
* @var Thread
* @ORM\OneToOne(targetEntity="ForumBundle\Entity\Thread")
*/
private $thread;
/**
* @var Post
* @ORM\OneToOne(targetEntity="ForumBundle\Entity\Post")
*/
private $post;
/**
* @return Thread
*/
public function getThread(): Thread
{
return $this->thread;
}
/**
* @param Thread $thread
*/
public function setThread(Thread $thread): void
{
$this->thread = $thread;
}
/**
* @return Post
*/
public function getPost(): Post
{
return $this->post;
}
/**
* @param Post $post
*/
public function setPost(Post $post): void
{
$this->post = $post;
}
}
<?php
namespace ForumBundle\Repository;
use Doctrine\DBAL\Query\QueryBuilder;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query\ResultSetMappingBuilder;
use ForumBundle\Entity\{Subforum,Post,Thread,LastThread};
/**
* Class ThreadRepository
*
* @package ForumBundle\Repository
*/
class ThreadRepository extends EntityRepository
{
public function getUpdatedListBySubforum(Subforum $subforum)
{
$em = $this->getEntityManager();
$rsm = new ResultSetMappingBuilder($em);
$rsm->addRootEntityFromClassMetadata(LastThread::class, 'm');
$rsm->addJoinedEntityFromClassMetadata(Thread::class, 't', 'm',
'thread');
$rsm->addJoinedEntityFromClassMetadata(Post::class, 'p', 'm',
'post', ['id' => 'post_id', 'create_date' => 'post_create_date']);
$query = 'select ' . $rsm->generateSelectClause(['t' => 'WFT', 'p' => 'WFP']) . ' from forum_thread WFT inner join (select thread_id, max(id) as last_post_id from forum_post group by thread_id) LPI on WFT.id = LPI.thread_id inner join forum_post WFP on WFP.id = LPI.last_post_id where WFT.subforum_id = :subforumId';
return $em->createNativeQuery($query, $rsm)
->setParameter(':subforumId', $subforum->getId())
->getResult();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment