• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DataMapper - Counting related items

#11
[eluser]phobeous[/eluser]
Hi again and thanks for your "fast as light" answer. You're right, servicio is a collection, but I want to run the count only on the related servicios of expediente with id=1.

include_related_count('servicio/adjunto') fails. This is the generated query:

SELECT `expedientes`.*, (SELECT COUNT(*) AS count FROM (`adjuntos`) LEFT OUTER JOIN `expedientes` `expedientes_subquery` ON `expedientes_subquery`.`id` = `adjuntos`.`servicio_id` WHERE `expedientes_subquery`.id = `expedientes`.`id`) AS servicio/adjunto_count FROM (`expedientes`) WHERE `expedientes`.`id` = 1

Because of the alias 'servicio/adjunto_count', but even fixing the alias and executing the query directly on MySQL, the result is wrong. Table 'servicios' is not queried and it tries to relate adjuntos directly with expediente.

I just suggest that it would be nice and cool if, given a proper relationship configuration, DataMapper library could 'translate' this notation:
Code:
$file = new Expediente(1);
$adjuntos_count = $file->servicio->adjunto->count();

into this query:

SELECT COUNT(*) AS `numrows`
FROM (`adjuntos`)
LEFT OUTER JOIN `servicios` servicios ON `servicios`.`id` = `adjuntos`.`servicio_id`
WHERE `servicios`.`expediente_id` = 1

Furthermore, you give this as an option in your first answer to this thread. The relationship scenario for my suggestion is the same as
Code:
$continent->country->person->count();

But, please Harro, take my comment just as a suggestion. DataMapper works perfectly and it's a great library for which me (and thousand of people around the world) will be always pleased to you.

Cheers.

#12
[eluser]WanWizard[/eluser]
Suggestions are always good.

The issue here is in the implementation, Datamapper objects have no awareness as to where they are in a hierarchy, which makes it difficult to construct that join and add that where clause. That would require that the 'adjunto' object knows what it's parent object is...

I'll have to look into this. Could you create an issue for this on http://bitbucket.org/wanwizard/datamapper, with a summary of the issue and a link to this thread so it won't be forgotten?

#13
[eluser]phobeous[/eluser]
[quote author="WanWizard" date="1353667177"]Suggestions are always good.

The issue here is in the implementation, Datamapper objects have no awareness as to where they are in a hierarchy, which makes it difficult to construct that join and add that where clause. That would require that the 'adjunto' object knows what it's parent object is...

I'll have to look into this. Could you create an issue for this on http://bitbucket.org/wanwizard/datamapper, with a summary of the issue and a link to this thread so it won't be forgotten?[/quote]

No sooner said than done:

https://bitbucket.org/wanwizard/datamapp...ated-count


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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