related foreign key to own table

Notice: This thread is very old.
mcmatak
Member | 487
+
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)

David Matějka
Moderator | 6427
+
+1
-

Try this:

$someone->related('people')

if it won't work try this instead:

$someone->related('people', 'children_id')
mcmatak
Member | 487
+
0
-
{foreach $someone->related('people.children_id') as $someoneChild}

there is solution

mcmatak
Member | 487
+
0
-

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