Doctrine 2: selecting object fields including related fields

I have the following entity:

/**
 * @ORM\Table(name="Employee")
 * 
@ORM\Entity(repositoryClass="Project\BackendBundle\Entity\Repository\EmployeeRepository")
 */
class Employee
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="Division")
     * @ORM\JoinColumn(name="division_id", referencedColumnName="id")
     * @Expose
     **/
    private $division;

    /**
     * @ORM\ManyToOne(targetEntity="Position")
     * @ORM\JoinColumn(name="position_id", referencedColumnName="id")
     */
    private $position;

    .....
}

      

With my REST API, I want to create a filter called "fields" where I put a comma separated list of entity fields that I want to retrieve. Like I would like to be able to put related fields. So the request looks like this:

/api/employee?fields=id,division

      

I check these fields if they exist in entity fields or association map.

Each related field is added to the request as follows:

foreach ( $this->assoc as $key => $mapping ) {
    $queryBuilder
        ->addSelect(substr($key, 0, 1) . ' AS ' . $key)
        ->leftJoin(
            $mapping['targetEntity'],
            substr($key, 0, 1),
            \Doctrine\ORM\Query\Expr\Join::WITH,
            substr($key, 0, 1) . ' = u.' . $key
        );
}

      

From the above query, I get the following DQL:

SELECT u.id, d AS division FROM Project\BackendBundle\Entity\Employee u LEFT JOIN Project\BackendBundle\Entity\Division d WITH d = u.division

      

Everything is ok, I get the expected result ( var_dump()

):

array (size=1)
  0 => 
    array (size=2)
      'division' => 
        object(Project\BackendBundle\Entity\division)[645]
          private 'id' => int 20
          private 'name' => string 'division1' (length=9)
      'id' => int 890

      

Now if I add another related field to my requested fields:

/api/employee?fields=id,division,position

      

I am getting the following DQL:

SELECT u.id, d AS division, p AS position FROM Project\BackendBundle\Entity\Employee u LEFT JOIN Project\BackendBundle\Entity\Division d WITH d = u.division LEFT JOIN Project\BackendBundle\Entity\Position p WITH p= u.position

      

The results now look like this:

array (size=2)
  0 => 
    array (size=1)
      'division' => 
        object(Project\BackendBundle\Entity\Kategorija)[672]
          private 'id' => int 20
          private 'name' => string 'division1' (length=9)
  1 => 
    array (size=2)
      'position' => 
        object(Project\BackendBundle\Entity\Position)[629]
          private 'id' => int 15
          private 'name' => string 'Manager' (length=7)
      'id' => int 890

      

The problem is that now the result of one object lies in two arrays instead of one.

Expected Result:

array (size=1)
  0 => 
    array (size=3)
      'division' => 
        object(Project\BackendBundle\Entity\Kategorija)[672]
          private 'id' => int 20
          private 'name' => string 'division1' (length=9)
      'position' => 
        object(Project\BackendBundle\Entity\Position)[629]
          private 'id' => int 15
          private 'name' => string 'Manager' (length=7)
      'id' => int 890

      

What am I missing or something amiss?

EDIT

I realized I was getting the fields wrong. I started using PARTIAL functions.

As requested by /api/employee?fields=id,division

DQL it looks like this:

SELECT partial u.{id,division} FROM Project\BackendBundle\Entity\Employee u

      

And the result:

array (size=1)
      0 => 
        array (size=3)
          'division' => 
            object(Project\BackendBundle\Entity\Kategorija)[672]
              private 'id' => int 20
              private 'name' => string 'division1' (length=9)
          'position' => 
            object(Project\BackendBundle\Entity\Position)[629]
              private 'id' => int 15
              private 'name' => string 'Manager' (length=7)
          'id' => int 890

      

You can see that I am getting the requested entity fields + all related fields, regardless of the fact that I only requested one related field.

How to filter these related fields as well?

+3


source to share


1 answer


What command are you using to execute DQL? When you select "division" or "position" in a DQL expression, it will treat it as a complete object. If you choose split.id and position.id this should work fine.



0


source







All Articles