• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DataMapper: count on more than one related object

For an already retrieved object, is there a "built-in" function to count the number of related objects from multiple relationships?

DataMapper has a nice function to count a single relationship (http://datamapper.wanwizard.eu/pages/cou...ng.Related), but I'd like to count on more than one relationship.

For example:

User (1:M) Profiles (M:1) Converters

Each User can have multiple Profiles for a single Converter. After retrieving a Profile, I want to know how many Profiles the User has for that same Converter.

So far, my solution is:

// get Profile by ID
$p = new Profile(99);
// get the related User and Converter
$u = $p->user->get();
$c = $p->converter->get();
// how many Profiles does this User have for this Converter?
$count = $p->result_count();

Is there a simpler/more elegant/concise way to accomplish this?

Not really simpler or more elegant, but probably faster since it's only two queries:

$p = new Profile();

// get the profile and related user and converter info

// how many Profiles does this User have for this Converter?
$count = $p->where_related_user('id', $p->user_id)->where_related_converter('id', $p->converter_id)->count();

Thank you -- I'll take the speed improvement. And, I'm feeling good that I'm getting the hang of DataMapper :-)

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.