Hello,

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 make the filter called "fields", where I put 
the comma separated list of entity fields, that I want to retrieve. Also I 
would like to be able to put associated fields there. So the request looks 
like this:

/api/employee?fields=id,division

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

Every associated field is added to the query like this:
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 request above 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 fine, 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 one more associated field to my requested fields:
/api/employee?fields=id,division,position

I get 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 looks 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 entity lies in two arrays instead 
of one. 

The expected result was:

*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 I am missing or doing wrong?


-- 
You received this message because you are subscribed to the Google Groups 
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to