Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Getting a distinct result using the dataobject

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

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

Page: 1
Go to End
Author Topic: 2525 Views
  • Mednezz
    Avatar
    Community Member
    23 Posts

    Getting a distinct result using the dataobject Link to this post

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

  • Sam
    Avatar
    Administrator
    679 Posts

    Re: Getting a distinct result using the dataobject Link to this post

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

  • Mednezz
    Avatar
    Community Member
    23 Posts

    Re: Getting a distinct result using the dataobject Link to this post

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

  • xmedeko
    Avatar
    Community Member
    94 Posts

    Re: Getting a distinct result using the dataobject Link to this post

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

    2525 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.