Welcome, Guest
Username: Password: Remember me

TOPIC: [FIXED] addGroupOrder not working properly?

addGroupOrder not working properly? 12 May 2015 20:01 #12976

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 310
  • Thank you received: 131
  • Karma: 48
Hi,

I have searched the forum if there was already a topic on this, but I could not find it.....
In my project, I fork a backend model because I want to have some data available from another table.
But, in this forked model the column from the other table is a group concat.

I use it like so in my copy/paste prepareQuery function:
//addselect
$this->addSelect('GROUP_CONCAT(_deelnemer_.naam SEPARATOR \' en \') AS `_deelnemer_naam`');
//addjoin
$this->addJoin('`#__trailrides_deelnemers` AS _deelnemer_ ON _deelnemer_.team = a.id', 'LEFT');
//goup on id for the group concat
$this->addGroupOrder('a.id');

The $this->addGroupOrder('a.id'); did not seem to work at all.

Later on in the code there is this part
		//GROUP ORDER : Prioritary order for groups in lists
		foreach($this->getState('query.groupOrder', array()) as $groupOrder)
			$query->order($groupOrder);
I think this should be
		//GROUP ORDER : Prioritary order for groups in lists
		foreach($this->getState('query.groupOrder', array()) as $groupOrder)
			$query->group($groupOrder);

At least in my project the data is shown correctly if I change that line.

Can you confirm this is an issue or is there something wrong with my code.

For completeness, here is the entire (almost totally original) prepareQuery function in my forked model
	protected function prepareQuery(&$query)
	{
		$acl = TrailridesHelper::getActions();

		//FROM : Main table
		$query->from('#__trailrides_teams AS a');



		//IMPORTANT REQUIRED FIELDS
		$this->addSelect(	'a.id');


		switch($this->getState('context', 'all'))
		{
			case 'teams.default':

				//BASE FIELDS
				$this->addSelect(	'a.betaald,'
								.	'a.factuur_verstuurd,'
								.	'a.klassering,'
								.	'a.naam,'
								.	'a.nummer,'
								.	'a.rit');

				//SELECT
				$this->addSelect('_rit_.naam AS `_rit_naam`');
				//Add concatenated info from deelnemers
				$this->addSelect('GROUP_CONCAT(_deelnemer_.naam SEPARATOR \' en \') AS `_deelnemer_naam`');

				//JOIN
				$this->addJoin('`#__trailrides_ritten` AS _rit_ ON _rit_.id = a.rit', 'LEFT');
				$this->addJoin('`#__trailrides_deelnemers` AS _deelnemer_ ON _deelnemer_.team = a.id', 'LEFT');
				
				//GROUPBY for the group concat
				$this->addGroupOrder('a.id');

				break;

			case 'teams.modal':

				//BASE FIELDS
				$this->addSelect(	'a.naam');


				break;
			case 'all':
				//SELECT : raw complete query without joins
				$this->addSelect('a.*');

				// Disable the pagination
				$this->setState('list.limit', null);
				$this->setState('list.start', null);
				break;
		}

		//FILTER - Access for : Root table


		//WHERE - FILTER : Rit
		if((int)$this->getState('filter.rit') > 0)
			$this->addWhere("a.rit = " . (int)$this->getState('filter.rit'));

		//WHERE - FILTER : Klassering
		if($this->getState('filter.klassering') !== null)
			$this->addWhere("a.klassering = " . $this->_db->Quote($this->getState('filter.klassering')));

		//WHERE - FILTER : Betaald
		if($this->getState('filter.betaald') !== null)
			$this->addWhere("a.betaald = " . (int)$this->getState('filter.betaald'));

		//WHERE - FILTER : Factuur verstuurd
		if($this->getState('filter.factuur_verstuurd') !== null)
			$this->addWhere("a.factuur_verstuurd = " . (int)$this->getState('filter.factuur_verstuurd'));

		//Populate only uniques strings to the query
		//SELECT
		foreach($this->getState('query.select', array()) as $select)
			$query->select($select);

		//JOIN LEFT
		foreach($this->getState('query.join.left', array()) as $join)
			$query->join('LEFT', $join);

		//JOIN INNER
		foreach($this->getState('query.join.inner', array()) as $join)
			$query->join('INNER', $join);

		//JOIN OUTER
		foreach($this->getState('query.join.outer', array()) as $join)
			$query->join('OUTER', $join);


		//WHERE
		foreach($this->getState('query.where', array()) as $where)
			$query->where($where);

		//GROUP ORDER : Prioritary order for groups in lists
		foreach($this->getState('query.groupOrder', array()) as $groupOrder)
			$query->group($groupOrder);

		//ORDER
		foreach($this->getState('query.order', array()) as $order)
			$query->order($order);

		//ORDER
		$orderCol = $this->getState('list.ordering');
		$orderDir = $this->getState('list.direction', 'asc');

		if ($orderCol)
			$query->order($orderCol . ' ' . $orderDir);
	}	
Last Edit: 12 May 2015 20:02 by Romkabouter. Reason: spelling
The administrator has disabled public write access.

addGroupOrder not working properly? 17 May 2015 10:32 #13021

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 987
  • Karma: 140
Since 2.7, you can use :
$model->addGroupBy()


Note: addGroupOrder() is used the group items together in the lists, but is not an agregating function. It is not the real SQL GROUPBY statement. Only a prioritary order.
Coding is now a piece of cake
The administrator has disabled public write access.
The following user(s) said Thank You: MorganL

addGroupOrder not working properly? 18 May 2015 14:17 #13070

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 310
  • Thank you received: 131
  • Karma: 48
Hi admin, I know that addGroupOrder() is not an aggregation function.
But addGroupBy did also not add a SQL GROUP BY statement.

I have checked and in the new version, this works correctly B)
The administrator has disabled public write access.
Time to create page: 0.060 seconds

Get Started