TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
These forums are read-only and for archival purposes only!
Please join our new forums at discourse.kohanaframework.org
ORM nesting problem
  • Hi,

    My problem is:

    A is an ORM model that "has_many" B (defined with "_has_many" property) B and C are related with "has_many" (defined with "_has_many" property 'through')

    if I do $A->B->find_all() it works if I do $B->C->find_all() it works if I do $C->B->find_all() it works

    if I do $A->B->C->find_all() it loose the relation between A and B and returns only records having B primary key NULL

    what I'm doing wrong ?

  • You have one $A loaded, which has_many B's.

    Now, before being able to select a C through B, the B must be loaded. Otherwise it has no id to connect to the C... (B is not a loaded Model but a MySQL_ResultSet or something like that)

    $A->B->where('id', '=', 1)->find()->C->find_all()

  • that would work if only one B needs to be loaded, in my case A has many Bs each of them has manys C and this query: $A->B->where('id', '=', 1)->find_all()->C->find_all()

    won't work.

    Suggestions ?

  • You can't retrieve them all in one find_all statement. However you could make a for each over $A->B->find_all() to get the C's.

    foreach ($A->B->where('something','=', true)->find_all() as $B) {
      $B->C->find_all();
    }
  • yes, bu in that case I wouldn't have a single iterator to go through all the Cs... So all the other functions I have that are supposed to work with an iterator would fails...

    is there a way to merge two DB results iterators of the same type of Objects ?

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion