Date index for May 2003


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [achievo] group, count, any one?



Hi,

e r wrote:

I need to use grouping with count function netween two nodes, is there any "nice" way to do that or I'll need to make a dirty hack for it?
10x,
Eldad Ran.

Now the easy way. This is a lot easier to understand and implement. However, it does a new query for each record that's being displayed.


class myCountAttribute extends atkAttribute
{
  function load($db, $rec, $mode="")
  {
    $childnode = &getNode("mymodule.childnode");
    if (is_object($childnode))
    {
      // $rec is the 'master' record, we can use it's id-field to
      // count records in another node.
      return $childnode->countDb("parent_id='".$rec["id"]."'",
                                                       "","",$mode);
    }
    else
    {
      // node not found.
      return 0;
    }
  }

  function store($db, $rec, $mode="")
  {
    // dummy impl, we don't have anything to store
  }
}

This approach has several advantages. First of all, it's a lot less code. Second, it's very easy to generalize this to a generic attribute
that you can use in any situation (by passing the nodename and where clause as parameters). And you don't use the (somewhat difficult) database abstraction layer. The last advantage is that you use $childnode to access the count. Any filters that might have been set in
$childnode will be applied.


Greetings,
Ivo



http://www.achievo.org/lists achievo.org - ©1999-2002 ibuildings.nl BV