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.

Data Model Questions /

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

DataList byIDs ensure value is an array?


Go to End


2 Posts   1354 Views

Avatar
merrick_sd

Community Member, 99 Posts

15 November 2012 at 1:40am

Edited: 15/11/2012 1:45am

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 }

Avatar
merrick_sd

Community Member, 99 Posts

15 November 2012 at 2:58am

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