Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Archive /

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo

Getting a distinct result using the dataobject


Go to End


4 Posts   3771 Views

Avatar
Mednezz

Community Member, 23 Posts

15 July 2007 at 12:53am

Edited: 15/07/2007 7:59am

Hey there,

I'm trying to create a dropdown form field, but would like to retrieve the available options from the database with only the available occurences. Normally i would use distinct in my query, but now i would like to use the dataobject to retrieve the data on a nice way.

Is this possible?

Now i've got it like this:
$spec = DataObject::get("VacaturePage", "", "", "", "")->column('Branche');
print_r(array_unique($spec));

but there should be a nicer way i think.
tnx in advance!

now i've got it like this :

$aType = array(0 => 'All types');
$aType = ArrayLib::cleanArray(ArrayLib::associative_push($aType,ArrayLib::valuekey(array_values(array_unique(DataObject::get("VacaturePage")->column('Type_vacature'))))));
$searchType = new DropdownField('type','',$aType);

I added 2 functions to the ArrayLib

cleanArray to clean out empty values in the array and associative_push so i can use a default value.

It may be nice for the distribution ;)

Avatar
Sam

Administrator, 690 Posts

15 July 2007 at 9:53am

instance_get has a groupBy option.

Perhaps you want to use singleton, and instance_get

singleton('VacaturePage')->instance_get(..)

See the API docs for more info.

Before we can judge whether it's appropriate to include your ArrayLib functions in the core, can you perhaps explain how the functions work, and give some other examples of where they might be useful?

We want to make sure that the core libraries contain a relatively small number of methods that let people do what they need to do - we don't want it to end up like the PHP function list! ;-)

Avatar
Mednezz

Community Member, 23 Posts

15 July 2007 at 8:48pm

Well that doesn't differ that much i think from my method. So you suggest i could use "group by Branche" inside the filter parameter?

about the functions...

First i've added the associative_push. Since a dropdown formfield needs a associative array for it's options and values you can retrieve the options from the database as described above. But sometimes one wants to have a make your choice option as the first option inside the dropdown formfield. So the normal function array_push won't work inside that associative array i had to create a workarround, the associative_push. Here's the code :

       /**
	 * Concatenate two associative arrays
	 *
	 * @param $arr array
	 * @param $tmp array
	 * @return array
	 */
	static function associative_push($arr, $tmp) 
	{
	    if (is_array($tmp)) {
		    foreach ($tmp as $key => $value) {
		      $arr[$key] = $value;
		    }
		    return $arr;
	  	}
	    return false;
	}
 

Second function is cleanArray(). When you got an empty row inside your array which you want to use in your dropdown formfield, you'll get an empty option. Not so nice. Using this function will get rid of that. It also handles the array recursive.

	/**
	 * Get rid of empty rows inside the array - recursive
	 *
	 * @param $arr array
	 * @return array
	 */
	static function cleanArray($arr) {
	   foreach ($arr as $index => $value) {
	        if(is_array($arr[$index])) $arr[$index] = cleanArray($arr[$index]);
	        if (empty($value)) unset($arr[$index]);
	   }
	   return $arr;
	}

Avatar
xmedeko

Community Member, 94 Posts

4 August 2007 at 11:17am

Sam: I cannot find GroupBy option in instance_get.

SiteTree::makelinksunique() method uses GROUP BY in this way:

[code php]
$badURLs = "'" . implode("', '", DB::query("SELECT URLSegment, count(*) FROM SiteTree GROUP BY URLSegment HAVING count(*) > 1")->column()) . "'";
$pages = DataObject::get("SiteTree", "URLSegment IN ($badURLs)");