0

Is it possible to filter an entity and display only few columns in symfony2? I think I can do a custom query for this, but it seems a bit dirty and I am sure there is a better solution. For example I have my variable $createdBy below, and it contains few data that shouldnt be displayed in this parent entity such as password etc...

/**
 * @var Customer
 *
 * @ORM\ManyToOne(targetEntity="MyCompany\Bundle\CustomerBundle\Entity\Customer")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="created_by", referencedColumnName="id", nullable=false)
 * })
 */
protected $createdBy;

So I need to display my Customer entity, but only containing fields like id and name for example. EDIT : I already have an instance of Project, the entity with my createdBy field, and I want to grab my customer data 'formatted' for this entity and not returning too much fields like password ...

Thanks

CodeIsLife
  • 1,205
  • 8
  • 14
Nicolas D
  • 1,182
  • 18
  • 40
  • 1
    I'm not sure what the issue you're having is? Since you control the output you control what to display or not to display. – Jason Roman Feb 25 '16 at 00:47

1 Answers1

0

It sounds like expected behavior to me. The doctrine documentation seems to imply that eager fetching is only one level deep.

According to the docs:

Whenever you query for an entity that has persistent associations and these associations are mapped as EAGER, they will automatically be loaded together with the entity being queried and is thus immediately available to your application.

http://doctrine-orm.readthedocs.org/en/latest/reference/working-with-objects.html#by-eager-loading

The entity being queried has eager on createdBy so it will be populated.

to bypass you can create a method in your entity repository as following :

// join entities and load wanted fields
     public function findCustom()
        {
            return $this->getEntityManager()
                ->createQuery(
                    'SELECT p FROM AppBundle:Product p ORDER BY p.name ASC'
                )
                ->getResult();
        }

hope this helps you

try this and let me know if it works, you should fill the right repository name

'XXXXBundle:CustomerYYYY', 'c'

public function findUser($user_id){

 $qb = $this->_em->createQueryBuilder('c')
            ->select(array('c', 'cb.id', 'cb.name'))
            ->from('XXXXBundle:Customer', 'c')
            ->where('c.id <> :id')
            ->leftJoin('c.createdBy', 'cb')
            ->setParameter('id', $user_id)->getQuery();
if ($qb != null)
            return $qb->getOneOrNullResult();
        return null;
}
CodeIsLife
  • 1,205
  • 8
  • 14
  • your answer seems to be pertinent, but how do I call this findCustom method as I don't choose to load the associated entity. Lets say that I call my customer associated from a project loaded like that $project = $em->find('MyProject\Domain\Project', $id); how can I call your custom method in this load automatically to populate my $createdBy with less data? – Nicolas D Feb 25 '16 at 11:44
  • i've update the answer, please let me know if it's ok for you – CodeIsLife Feb 25 '16 at 12:05
  • i've tested your answer, but when I load my project entity, I still have my Cutomer attached to the project with the same structure. I think I will have to manually format all my entities with a foreign key linked to a customer which will be fastidious and boring... – Nicolas D Feb 25 '16 at 22:12
  • 1
    you can use doctine namedQuery for custom queries : https://github.com/doctrine/doctrine2/blob/master/tests/Doctrine/Tests/Models/CMS/CmsUser.php – CodeIsLife Feb 25 '16 at 22:38
  • it seems like a good thing, I will need more time to implement this, I come back once it is tested – Nicolas D Feb 26 '16 at 00:03