guillaumeoriol / serquant

Serquant library
4 stars 1 forks source link

'join' not performed by Doctrine in the application while performed by doctrine-cli #3

Open guillaumeoriol opened 13 years ago

guillaumeoriol commented 13 years ago

An unidirectional many-to-many association is set up between Person and Role. To retrieve people, the following DQL is built: select e from \Domain\Entity\Person e

When performed by doctrine-cli, the following SQL queries are issued: SELECT p0_.id AS id0,... FROM people p0_ SELECT t0.id AS id1, t0.name AS name2 FROM roles t0 INNER JOIN people_roles ON t0.id = people_roles.role_id WHERE people_roles.person_id = 1

When performed by the Doctrine persister, only the first SQL query (on 'people') is issued.

guillaumeoriol commented 13 years ago

Reading Doctrine documentation (section 3.5.2. Association proxies), I found this explanation:

Whenever you query for an object that has a single-valued association to another object that is configured LAZY, without joining that association in the same query, Doctrine puts proxy objects in place where normally the associated object would be. Just like other proxies it will transparently initialize itself on first access.

guillaumeoriol commented 13 years ago

For this issue, I have written two entities: CmsUser and CmsAccount, and a one-to-one (unidirectional) association between CmsUser and CmsAccount.

I want to execute the following DQL: select e from \Domain\Entity\CmsUser e

When I run that query from Doctrine command line tool, I get a proxy object for the account (which I consider normal). But when I run the same query from my test suite, I receive a failed assertion when testing if the account is a proxy:

    $account = $user->getAccount();
    $this->assertInstanceOf('\Doctrine\ORM\Proxy\Proxy', $account);

Here are the entity definitions.

/**
 * @Entity
 * @Table(name="cms_users")
 */
class CmsUser
{
    /**
     * @Id @Column(type="integer")
     * @GeneratedValue
     */
    public $id;

    /**
     * @OneToOne(targetEntity="CmsAccount", cascade={"persist", "remove"}, orphanRemoval=true)
     * @JoinColumn(name="account_id", referencedColumnName="id")
     */
    public $account;

    // ...
}

And

/**
 * @Entity
 * @Table(name="cms_accounts")
 */
class CmsAccount
{
    /**
     * @Column(type="integer")
     * @Id @GeneratedValue
     */
    public $id;

    /**
     * @Column(length=50)
     */
    public $accountNumber;

    // ...
}