Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC:

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

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);
	}	

Please Log in or Create an account to join the conversation.

Last edit: by Romkabouter. Reason: spelling

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

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 986
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 following user(s) said Thank You: MorganL

Please Log in or Create an account to join the conversation.

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

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)

Please Log in or Create an account to join the conversation.

  • Page:
  • 1
Time to create page: 0.095 seconds

Real time saver and great Component Builder tool ! I have been developing with J-Cook Pro Component Builder for several months now and can say with all honesty that this product/service is second to none. The product is feature rich and is being improved and added to all the time. Do yourself a favor if you need to build a Joomla! Component then you can do no wrong in trying the product. You will save on time and effort while being able to deliver your project on time. J-Cook pro does the hard work for you you then have the freedom to fully customise the end result for your own needs. 

One word: Awesome.
Edwardcox (JED)
         

Get Started