CodeIgniter Forums
DataMapper: Find with No Related Objects - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: DataMapper: Find with No Related Objects (/showthread.php?tid=55979)



DataMapper: Find with No Related Objects - El Forum - 11-19-2012

[eluser]jonahdahlquist[/eluser]
Hello, world!

My CodeIgniter/DataMapper site needs to display a list of categories. Each category has a many-to-many relationship with itself, for multiple parents/children. I want to initially select only the categories with with no parents, or in other words, with no objects connected through a specified relationship.

I've run through a couple of possibilities (like including the related object count in the select and adding it to the where clause, which doesn't work because WHERE is evaluated before generating the column values in SQL), but to no avail.

Advice? Thank you.


DataMapper: Find with No Related Objects - El Forum - 11-19-2012

[eluser]WanWizard[/eluser]
Have you looked at http://datamapper.wanwizard.eu/pages/troubleshooting.html#Relationships.NotRelated ?


DataMapper: Find with No Related Objects - El Forum - 11-21-2012

[eluser]jonahdahlquist[/eluser]
Not quite, I don't want to check if it's not related to a specific object, I wanted get ones that aren't related to any objects at all. I did resolve it by doing this:

Code:
$c = new Category();

$top_level_categories = $c
    ->where_related_parents('id IS NULL')
    ->get();

If it's an integer auto-increment column, no values will actually be NULL, but if there are no related rows in the join, it will come out as NULL.