0
votes

For better understanding, I will simply put the context.

I have a User entity who has a BankAccount relation.

Here, my User class:

/**
 * User
 *
 * @ORM\Table(name="fos_user")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
 */
class User extends BaseUser
{

    // [...]


    /**
     * @ORM\OneToOne(targetEntity="AppBundle\Entity\BankAccount", mappedBy="customer")
     */
    private $bankAccount;

    // [...]
}

And my bank Account class:

/**
 * BankAccount
 *
 * @ORM\Table(name="bank_account")
 * @ORM\Entity
 * @DoctrineAssert\UniqueEntity({"iban"})
 * @DoctrineAssert\UniqueEntity({"mandateRef"})
 * @ORM\HasLifecycleCallbacks
 */
class BankAccount
{
    // [...]

    /**
     * @var User
     *
     * @ORM\OneToOne(targetEntity="AppBundle\Entity\User", inversedBy="bankAccount")
     * @ORM\JoinColumn(name="customer_id", referencedColumnName="id", onDelete="CASCADE")
     */
    private $customer;

    // [...]
}

I want to create a filter form who will be a basic entity type for user with some orderBy statement. I will so use the query_builder options:

->add('customer', null, array(
    'query_builder' => function (UserRepository $er) {
        return $er->createQueryBuilder('u')
            ->select('u')
            ->orderBy('u.firstname, u.lastname, u.username')
        ;
    },
    'property' => 'fullnameWithUsername',
))

This will generate a lot a BankAccount selects made by doctrine, for each user:

enter image description here

If I deactivate the query builder, no more additional select but we have a left join on user select:

enter image description here

So the solution could be to manually add the join statement on my query builder as described here but I think it's quite ugly and by the way, I absolutely don't need the user's bank account for this form.

So my question is: How can I tell Doctrine to not always try to get user's bank account relation?

Note: I already try all fetch options on both entities, not working.

Thanks for your help!

1

1 Answers

0
votes

If you change that OneToOne relationship to be a OneToMany (even though it really isn't), Doctrine will stop loading the bank account entity.

It's dirty, but far as I know there's no other way to stop it happening.