phpcakephpcontrollercomponentsformhelper

cakePHP build a select box from custom array


So we have a variety of search pages each with different search criteria, so I decided to write a component which will get parameters passed to it from the controller, collect the neccesary data and return an array which I could then set to be used within my view file to populate drop down boxes to filter the criteria.

I have managed to get everything write up to where I must use the cakePHP helper to build a dynamical select box. I am convinced that I am doing something wrong and if there is an easier way to do this and still keep it somewhat dynamic please assist where you can:

// COMPONENT METHOD:
 public function filterQueries($parameters) {

    // Get defaults from the user:
    $criteria = $parameters["custom"];
    $defaults = $parameters["defaults"];

    // Validate the defaults the user may want and assign them to the return array:
    if($defaults != "false") {
        foreach($defaults as $key => $value) {
            if(array_key_exists($value, $this->defaults)) {
                $this->returnArray["defaults"][$value] = $this->defaults[$value];
            }
        }
    }

    // Get all data for the custom requested form fields:
    if($criteria != false) {
        foreach($criteria as $model => $arguments) {
            $fields = $arguments["fields"];
            $conditions = $arguments["conditions"];
            $recursive = $arguments["recursive"];
            if(!in_array($model,$this->uses)) {
                $useModel = ClassRegistry::init($model);
            } else {
                $useModel = $this->$$model;
            }
            $array = $useModel->find("all",array("conditions" => $conditions, "fields" => $fields, "recursive" => $recursive));
            $this->returnArray["custom"][$model] = $array;
        }
    }

    return $this->returnArray;
}

The above function will get an array which breaks down either custom searches or defaults (not included above but it all works, it returns the array exactly as I would have expected it.

 // The code within my action to get the content from above:
// Load the Filters component to search data:
        $search = $this->Components->load("Filter");

        // Tell search what you want:
        $searchBoxes = array(
            "defaults" => array("statuses", "survey_type"),
            "custom" => array(
              "User" => array(
                  "fields" => array("User.id","User.first_name", "User.last_name"),
                  "conditions" => array("User.user_group_id" => "4f847c63-1840-446e-88be-3e4d29566cf0"),
                  "recursive" => -1
              )  
            )
        );

       $filterResults = $search->filterQueries($searchBoxes);
       $this->set("filters",$filterResults);

So now I get this multi-associative array within my view file (all still fine), but I want to now build example a drop down list of the users based on the array created above, but the outcome is nothing like what I expected:

echo $this->Form->input('user_id',
                        array(
                            "type" => "select",
                            "options" => $filters["custom"]["User"]
                        )
                     );

The HTML output is broken and displays it like this:

<option value="last_name">Doe</option>
<option value="first_name">Jihn</option>
<optgroup label="User"> </optgroup>
<optgroup label="1"> </optgroup>
<option value="last_name">Marilyn</option>
<option value="first_name">Monroe</option>

I acknowledge that I do not have a lot of cake experience but cannot understand how to just get the results to :

 <option value='USERID'>NAME</option> // Yes I know the names and surnames must be concatinated still

Any advise help or guidance on how to do it, and do it the right way, would greatly be appreciated :)

VARDUMP ON $filters['custom']['users']

  array
   0 => 
     array
       'User' => 
         array
            'id' => string '4f84840e-cda8-4704-8fdf-210729566cf0' (length=36)
            'first_name' => string 'Name' (length=4)
            'last_name' => string 'Surname' (length=11)
    1 => 
      array
        'User' => 
          array
            'id' => string '4f8488cb-53e0-4f72-af73-3de229566cf0' (length=36)
            'first_name' => string 'Name' (length=6)
            'last_name' => string 'Surname' (length=6)

Solution

  • You can enhance your output by doing as follows:

    1) for combining two fields of a table, you can use "virtualfields" in the model, as follows: For example if you have the user model, you can define as follows:

     public $virtualFields = array(
      'full_name' => 'CONCAT(first_name, " ",last_name)'
     );
    

    So now the "full_name" field will be got whenever you call the find method of the User model.

    2) For getting the data from the table for a select box, you can use the find('list') method. For example for the User model if you need the id,full_name (last and first name combined using the virtual fields) of the table,it can be done as follows :

    $this->User->find('list',array('fields'=>array('id','full_name'),'conditions'=>$conditions))
    

    I hope this helps..