Hi
I've a question (or five) about HABTM relationships in Cake
1.2.3.8166.
I had a very simple setup with a HABTM relationship:
A KPI can be displayed on many charts
A Chart can display many KPIs
So I've a HABTM relationship and a join table called charts_kpis. This
all worked very well until I started to complicate matters by adding
attributes to my charts_kpis table.
Each KPI can have a different colour and width when displayed on
different charts. The colours are held in a lookup table called
kpi_colours. I created (baked) a model for my charts_kpis join table
and added in the relationship with kpi_colours:
class ChartsKpi extends AppModel {
var $name = 'ChartsKpi';
var $belongsTo = array(
'Chart' => array(
'className' => 'Chart',
'foreignKey' => 'chart_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Kpi' => array(
'className' => 'Kpi',
'foreignKey' => 'kpi_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'KpiColour' => array(
'className' => 'KpiColour',
'foreignKey' => 'kpi_colour_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}
I also updated my kpi_colours model to associate it with charts_kpis:
class KpiColour extends AppModel {
var $name = 'KpiColour';
var $hasMany = array('KpiLevel', 'ChartsKpi');
}
I think this is all OK up til here as I've tried to follow examples
elsewhere in the forum. Where my brain begins to breakdown is how to
implement this in the view and controller.
When I had a simple HABTM relationship I used this form helper method
to generate a set of checkboxes in the Chart add.ctp view. This
allowed the user to check which KPIs they wanted to appear on a chart:
echo $form->input('Kpi', array('type' => 'select',
'multiple' => 'checkbox',
'label' => 'Select the KPIs to appear in this
Chart',
'div' => 'checkboxdiv'));
This worked like a dream. Now what I want to do is display the
checkbox followed by a select drop down so the user can check a KPI
and select the colour they want the KPI to be on the chart. To do this
I've had to manually build the checkboxes followed by a select
element for the colours:
foreach ($kpis as $kpi)
{
echo "<div class=\"checkboxdiv\">";
echo "<label for=\"KpiKpi" . $kpi['Kpi']['id'] .
"\">" . $kpi['Kpi']['title'] . "</label>";
echo '<input type="checkbox" id="KpiKpi' . $kpi
['Kpi']['id'] . '" ' .
'value="' . $kpi['Kpi']['id'] . '" ' .
'name="data[Kpi][Kpi][]" />';
echo $form->select('ChartsKpi.' . $kpi['Kpi']
['id'] . '.kpi_colour_id', $kpiColours;);
echo "</div>";
}
Now that looks a right mess; but as far as the checkboxes go it works
OK and it produces a select list for each KPI that looks like this:
<select id="ChartsKpi24KpiColourId" name="data[ChartsKpi][24]
[kpi_colour_id]">
where the 24 is the $kpi['Kpi']['id'].
My first question is; have I gone about this the right way from the
form point of view?
If the form constructs are OK then my second question is; how do I
save the kpi_colour_id data along with the HABTM data when that gets
saved?
In my controller I had a simple:
if (!empty($this->data)) {
$this->Chart->create();
if ($this->Chart->save($this->data)) {
Which saved the HABTM just fine; but doesn't same the associated
kpi_colour_id. I tried updating the controller to do a saveAll; but
that doesn't work for me either.
So, just to reiterate:
1. Have I gone about this the right way?
2. How do I get the data structures right so that I can save the
associated attributes?
Thanks
David
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---