How to exclude bidirectionnal relations using NativeQuery and ResultSetMapping?

148 views Asked by At

I have 2 entities, User and Customer, with a OneToOne bi-directionnal relation. User entity also have a bi-directionnal relation with Invoice entity :

class User implements UserInterface
{
    /**
     * @ORM\OneToOne(targetEntity="Evo\UserBundle\Entity\Customer", mappedBy="user", cascade={"persist", "remove"})
     * @Type("Evo\UserBundle\Entity\Customer")
     */
    protected $customer;

    /**
     * @ORM\OneToMany(targetEntity="Evo\BackendBundle\Entity\Invoice", mappedBy="user", cascade={"remove"})
     */
    protected $invoices;
}

class Customer
{
    /**
     * @ORM\OneToOne(targetEntity="Evo\UserBundle\Entity\User", inversedBy="customer")
     * @ORM\JoinColumn(name="account", nullable=false)
     */
    protected $user;
}

Now, I want to retrieve Users and their Customer relation, but nothing more, using a NativeQuery and a ResultSetMapping. I tried

$sql = "SELECT a.id, a.identifier, c.id AS customer_id, c.email, c.firstname, c.lastname
    FROM account a
    INNER JOIN customer c ON c.account = a.id
    WHERE c.email = '[email protected]'";

$rsm = new ResultSetMapping();
$rsm->addEntityResult('Evo\UserBundle\Entity\User', 'u');
$rsm->addFieldResult('u', 'id', 'id');
$rsm->addFieldResult('u', 'identifier', 'username');

$rsm->addJoinedEntityResult('Evo\UserBundle\Entity\Customer' , 'c', 'u', 'customer');
$rsm->addFieldResult('c', 'customer_id', 'id');
$rsm->addFieldResult('c', 'email', 'email');
$rsm->addFieldResult('c', 'firstname', 'firstname');
$rsm->addFieldResult('c', 'lastname', 'lastname');

$query = $this->_em->createNativeQuery($sql, $rsm);

$users = $query->getResult();

return $users;

But this returns the User, and all his bi-directionnal relations :

array(1) {
  [0]=>
  object(stdClass)#1370 (33) {
    ["__CLASS__"]=>
    string(26) "Evo\UserBundle\Entity\User"
    ["id"]=>
    string(5) "38425"
    ["invoices"]=>
    array(77) {
      [0]=>
      string(32) "Evo\BackendBundle\Entity\Invoice"
      [1]=>
      string(32) "Evo\BackendBundle\Entity\Invoice"
      [2]=>
      string(32) "Evo\BackendBundle\Entity\Invoice"
      [3]=>
      string(32) "Evo\BackendBundle\Entity\Invoice"
      [4]=>
      string(32) "Evo\BackendBundle\Entity\Invoice"
      [5]=>
      string(32) "Evo\BackendBundle\Entity\Invoice"
      [6]=>
      string(32) "Evo\BackendBundle\Entity\Invoice"
      [7]=>
      string(32) "Evo\BackendBundle\Entity\Invoice"
      [8]=>
      string(32) "Evo\BackendBundle\Entity\Invoice"
      [9]=>
      string(32) "Evo\BackendBundle\Entity\Invoice"
    }
    ["customer"]=>
    object(stdClass)#3083 (87) {
      ["__CLASS__"]=>
      string(30) "Evo\UserBundle\Entity\Customer"
      ["id"]=>
      string(5) "38553"
      ["user"]=>
      string(26) "Evo\UserBundle\Entity\User"
      ["email"]=>
      string(12) "[email protected]"
      ["lastname"]=>
      string(13) "RAVIER (TEST)"
      ["firstname"]=>
      string(7) "Jacques"
    }
    ["username"]=>
    string(12) "[email protected]"
  }
}

I want to get rid of these Invoice relations in the returned data. Something I should do with the ResultSetMapping ?

note : For a specific reason, I can't use classic DQL on this feature, I really need to use NativeQuery.

0

There are 0 answers