Ticket #1359 (closed defect: invalid)

Opened 19 months ago

Last modified 18 months ago

isset does not work on the referenced side of one-to-one relations

Reported by: pstradomski Owned by: romanb
Priority: major Milestone:
Component: Relations Version: 1.0.0
Severity: Keywords:
Cc: pstradomski@… Has Test: no
Status: Pending Core Response Has Patch: no

Description (last modified by pstradomski) (diff)

User: id (primary key), other fields
Email: id (PK), userId (FK), email(string); defined named relation user (foreignType=one, foreignAlias=email)
//in the database there's user with id = 6, and an email with id=1 and userid=6
$u = Doctrine::getTable('User')->find(6);

var_dump(isset($u->email));     // returns false
var_dump($u->email->toArray()); // shows that the record exists and is not a newly created one, because it has email column filled.

The code was exported from 1.0 branch yesterday.

Change History

Changed 19 months ago by pstradomski

  • description modified (diff)

Changed 19 months ago by pstradomski

  • cc pstradomski@… added

Changed 19 months ago by romanb

  • status changed from new to closed
  • resolution set to invalid

I dont see anything wrong here. If you only load the User, without it's email address, the email field will not be set (isset() = false). In the subsequent line you trigger a lazy-load of the email object through accessing it, also correct. You really do not want isset() to hit the database. Thats unintuitive and can be a performance nightmare.

You can do this instead:

$u = Doctrine_Query::create()
        ->from('User u')
        ->leftJoin('u.email')
        ->where('u.id = ?', 6)
        ->execute()
        ->getFirst();
var_dump(isset($u->email)); // TRUE if the user has an email, FALSE otherwise

Remember, in order to know if the user has an email (in the database), you have to hit the db either way. This way its 1 query, with lazy loading its 2.

If i misunderstood you feel free to reopen and clarify the issue.

Changed 18 months ago by anonymous

  • milestone New deleted

Milestone New deleted

Note: See TracTickets for help on using tickets.