Preload needs protection if no parent set #211

Closed
Maigre opened this Issue Nov 16, 2012 · 0 comments

Projects

None yet

2 participants

@Maigre
Maigre commented Nov 16, 2012

Hi,
i'm playing with the recently added Preload function,
and i had some trouble with it.

Let say i do

R::preload($books,array('author'));

if one of the given beans $books has no parent author set
(consequence: $book->author_id is null)
it fail and throw an error (invalid SQL)

I found an easy fix for this:

in OODB.php, in the preload function, at line 896 i replaced

foreach($beans as $bean) {
$id = $bean->{$field.'_id'};
$ids[$id] = $id;
if (!isset($map[$id])) $map[$id] = array();
$map[$id][] = $bean;
}

by

foreach($beans as $bean) {
if ($id = $bean->{$field.'_id'}) {
$ids[$id] = $id;
if (!isset($map[$id])) $map[$id] = array();
$map[$id][] = $bean;
}
}

So i just test if an id is available adding it to the preload pool.

This function is very helpful by the way, i hope it will help improve it.

Question: Do you think it's possible to load a bean AND preload all parents in one SQL query ?
It would be awesome ;)

Thanks for your work !

Best Regards

Thomas

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment