0
votes

I want to get two field values from a database table and combine them together and use that as the display text in the label. Also I want to make another field value in the table as the label id.

The AgeCat table stores the different age categories. The table definition is

age_cat_id ---> primary key
range_start
range_end

e.g:

age_cat_id=>1
range_start=>18
range_end=>24

What I want is to have the label text set as '"range_start" to "range_end"' e.g: '18 to 24'

and the label id to be the age_cat_id.

What is the proper way to format the label text to display as above? I have retrieved all the records of the AgeCat table, and stored them in a string array ($agecats) using find(all). I'm having trouble retrieving values from that array and then format it according to above way and set as the label text. Please suggest if there is a better way to do this.

The purpose of this is to display each record in the AgeCat table following the format of 'range_start to range_end' but having their value set as the corresponding age_cat_id. E.g: 18 to 24 is displayed using some form element but the value should be 1. It's like having a drop down list with custom strings, but each linked to a unique value. So once an option is selected, the value is passed, not the string.

If the label is not good enough, what form element is suitable for this?

1

1 Answers

2
votes

Virtual fields:cakephp documentation.

public $virtualFields = array(
'cat_date' => 'CONCAT(AgeCat.range_start, " to ", AgeCat.range_end)'
);

P.S.: according to Cakephp conventions the primary key should be named id.

Edit: take a look there: form helper

echo $this->Form->select('id', $cat_date)

In the controller something like this:

$cat_date=$this->Model->find('list',array('fields'=>array('id','cat_name')));

with $cat_date an array containing the desired values in the select.