lox / pheasant

A lightweight data mapper designed to take advantage of PHP 5.3+
http://getpheasant.com
MIT License
101 stars 21 forks source link

Double hydrating when using `includes()`? #130

Closed bjornpost closed 9 years ago

bjornpost commented 9 years ago

Just fiddled around with includes() again and noticed that it tries to hydrate objects twice. Most of the time it's just inefficient. But when using the DateTime Type, it throws an exception saying "DateTime::__construct() expects parameter 1 to be string, object given".

After some digging; I found that Schema::hydrate() is called twice for included DomainObjects. First, it's called from \Pheasant\Collection::hydrate() (src), second time it's called from Relationship::get(), (f.e. BelongsTo (src)).

Just calling return $cached; in BelongsTo.php fixes the issue, but I'm not sure about possible unwanted side effects.

/cc @Jud

Jud commented 9 years ago

@bjornpost that's how we fixed it and haven't had any side effects (ditto for HasOne).

https://github.com/judsonco/pheasant/blob/9db99cb4fdf568f9664425e1c4ab16fa16dce071/lib/Pheasant/Relationships/BelongsTo.php#L23

I haven't had time to think about how that effects the ability to use a different, hypothetical, cache backend like memcached, but it doesn't seem like it would effect it.