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,

There are several ways to do it. The performance-wise best way (but not easy) is to create a new attribute that makes the count for the second node be done in the same query that loads the record for the first node.
It requires some magic, but you'll end up being able to do something like this:


$this->add(new myCountAttribute("childcount"));

to any node you want to add the count to.

Implementing this coundattribute can be done like this:

class myCountAttribute extends atkAttribute
{
  function myCountAttribute($name, $flags=0)
  {
    // instantiate base class with some example default flags.
    $this->atkAttribute($name, $flags|AF_HIDE_EDIT|AF_HIDE_ADD);
  }

function addToQuery(&$query, $table="", $aliasprefix="", $rec="",
$level, $mode)
{
// since we're hide_edit and hide_add, we only do things in
// adminmode query
if ($mode=="admin")
{
$query->addField("count(childtable.id)",


                                        "", "", "counter");

      $query->addJoin("childtable", "childtable",
                             "childtable.parent_id = id", true);
      $query->addGroupBy("id");
    }
  }

  function db2value($record)
  {
    // magic: the value seems to be stored by mysql or atk in the record
    //  array with this very strange name..
    return $record["countercount(childtable.id)"];

    // (if it's not named like this, try to discover its name by using
    // var_dump($record))
  }
}

Ok. This is the hard way. I'll post a second reply with a more easy approach.

Greetings,
Ivo



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