Jump to:

3450 Posts in 1063 Topics by 738 members

Data Model Questions

SilverStripe Forums » Data Model Questions » DataList byIDs ensure value is an array?

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

Page: 1
Go to End
Author Topic: 418 Views
  • merrick_sd
    Avatar
    Community Member
    95 Posts

    DataList byIDs ensure value is an array? Link to this post

    I have a Text field called selecetdIndividuals on Page Table. eg: 18,19,20

    I want to use it to show the chosen Individuals which are in my Individuals table.

    in my Page.php if manually set the values it works.
    if try and make it dynamic its doesn't,, but i have echoed $selectedIndividuals and they display.

       public function SelectedStaff($selectedIndividuals) {
       
        if ($selectedIndividuals) {

        // comment out $idList = new Array($selectedIndividuals);
    //manually set
    $idList = array(18,19,20);
    $dl = DataList::create('Individual')->byIDs($idList);
        return $dl;
        } else {
    echo "non some values are null like this one";
    }
    }

    so what i mean is when i do $idList = $selectedIndividuals; i get an error

    [Warning] array_map(): Argument #2 should be an array
    Line 781 in /home/hmyapp/public_html/framework/model/DataList.php
    773
    774    /**
    775     * Filter this list to only contain the given Primary IDs
    776     *
    777     * @param array $ids Array of integers, will be automatically cast/escaped.
    778     * @return DataList
    779     */
    780    public function byIDs(array $ids) {
    781       $ids = array_map('intval', $ids); // sanitize
    782       $baseClass = ClassInfo::baseDataClass($this->dataClass);
    783       $this->where("\"$baseClass\".\"ID\" IN (" . implode(',', $ids) .")");
    784       
    785       return $this;
    786    }

  • merrick_sd
    Avatar
    Community Member
    95 Posts

    Re: DataList byIDs ensure value is an array? Link to this post

       this was the bit i needed
    $idList = explode(',',$selectedIndividuals);

    //function to display selected individuals on the frontend
    public function SelectedStaff($selectedIndividuals) {

       if ($selectedIndividuals) {   
        //turn the passed string 18,19,20 into an array
    $idList = explode(',',$selectedIndividuals);
    //get a readonly result
    $dl = Individual::get()->byIDs($idList);
        return $dl;
       }else {
       //no selected individuals passed
       return;
       }
    }

    418 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.