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?
EDIT
I figured out that I am getting the fields wrong way. I started using PARTIAL functionality.
On request /api/employee?fields=id,division
the DQL looks like this:
SELECT partial u.{id,division} FROM Project\BackendBundle\Entity\Employee u
And the result is:
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 get the requested entity fields + all associated fields, no matter that I requested only one associated field.
How to get those associated fields filtered too?
What command are you using to execute the DQL? When you select 'division' or 'position' in a DQL statement, it's going to treat that as a full object. If you select division.id and position.id that should work just fine.