CodeIgniter Forums
Datamapper (Find rows NOT in my join table) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Datamapper (Find rows NOT in my join table) (/showthread.php?tid=16533)



Datamapper (Find rows NOT in my join table) - El Forum - 03-09-2009

[eluser]Iverson[/eluser]
Say I have users can have multiple projects. How can I get the rows of projects that AREN'T related to a particular user?


Datamapper (Find rows NOT in my join table) - El Forum - 03-10-2009

[eluser]tdktank59[/eluser]
well 1 option is to do something like this

Code:
$u = new User();
$u->where('id',$id);
$u->projects()->get();

foreach ($u->projects->all as $projects)
{
$project_ids = array($projects->id);
}

$p = new Project();
$p->where_not_in('id',$project_ids)->get();

see where im going with this?


Datamapper (Find rows NOT in my join table) - El Forum - 03-11-2009

[eluser]Iverson[/eluser]
[quote author="tdktank59" date="1236738273"]well 1 option is to do something like this

Code:
$u = new User();
$u->where('id',$id);
$u->projects()->get();

foreach ($u->projects->all as $projects)
{
$project_ids = array($projects->id);
}

$p = new Project();
$p->where_not_in('id',$project_ids)->get();

see where im going with this?[/quote]

Thanks! I just needed to a jumpstart Smile Only thing I needed to change from your code was to make $project_ids an array instead of the object's id. This might be a good addition to Datamapper as I can see others using this just as much as relationships.

Code:
$u = new User();
$u->where('id',$id);
$u->projects()->get();

foreach ($u->projects->all as $projects)
{
$project_ids[] = $projects->id;
}

$p = new Project();
$p->where_not_in('id',$project_ids)->get();