How to see values of multiple checkbox on hasmany

For a hasMany relationship I cant get the required field to display as a label, instead I get the id value as the label

$availabilityForTutors = $this->Tutors->AvailabilityForTutors->find(‘list’, [
‘conditions’=>array(‘AvailabilityForTutors.tutor_id’=> $id),‘limit’ => 200]);

echo $this->Form->input(‘availabilityForTutors’, [‘multiple’ => ‘checkbox’,‘label’=>$availabilityForTutors->weekday]);

http://stackoverflow.com/questions/27234782/cakephp3-many-to-many-relations

Set the displayField method to the name of the column you want to use as the label. This can be done within the Table class of the association.

public function initialize(array $config) {
         parent::initialize($config);

         $this->table('groups');
         $this->displayField('name');  //  Change this value
         $this->primaryKey('id');
}

You can also set the key value fields in the finders options array as in the code below:

$availabilityForTutors = $this->Tutors->AvailabilityForTutors->find('list', ['keyField' => 'column acting as key', 'valueField' => 'column acting as value', 'conditions'=>array('AvailabilityForTutors.tutor_id'=> $id),'limit' => 200]);

You can read more about the findList options here

1 Like