related foreign key to own table

mcmatak
Member | 496
+
0
-

how to go through childrens specified in same table?

for example:

table
people (id, name, children_id), children_id is foreign key to people id
1, František Horák, NULL
2, Anička Horáková, 1
3, Pepíček Horák, 1

$query = $this->database->table('people');
$query->select('people.*');
$query->where('children_id IS NULL');
foreach($query as $someone) {
    echo $someone->name . "\n";

    foreach($someone->related('children') as $someoneChild) {
        echo " has child: " . $someoneChild->name . "\n";
    }
}

problem:

No reference found for $people->related(children).

Last edited by mcmatak (2015-04-15 15:23)

Comments

mcmatak:
{foreach $someone->related('people.children_id') as $someoneChild}

there is solution

4 years ago
David Matějka
Moderator | 5936
+
+1
-

Try this:

$someone->related('people')

if it won't work try this instead:

$someone->related('people', 'children_id')

Comments

mcmatak:

thanks, i had it after i wrote this post :) what about sorting this relations?

4 years ago