Hello,
I have another question.
I'm trying to edit a "Part" model which HABTM to "Model" model. I
tried using $form->input('ModelsPart.model_id',
array('multiple'=>'checkbox')), yet it the system doesn't know which
model already belongs to that part.
What I'm trying to do is that the system can tell whether that
particular "part" that I'm trying to edit already belongs to certain
models.
So far, I have this in my controller:
function edit($id = null)
{
if (!$id && empty($this->data)) {
$this->flash(__('Invalid Part', true),
array('action'=>'index'));
exit();
}
if (!empty($this->data)) {
if ($this->Part->save($this->data)) {
$this->flash(__('The Part has been saved.',
true),
array('action'=>'index'));
exit();
} else {
}
}
if (empty($this->data)) {
$this->data = $this->Part->read(null, $id);
}
$models = $this->Part->Model->find('list');
$this->set('selectedModels', $this->ModelsPart->find('all',
array('conditions'=>array('ModelsPart.part_id'=>$id))));
$this->set(compact('models'));
}
and my view looks like:
<div class="parts form">
<?php echo $form->create('Part');?>
<fieldset>
<legend><?php __('Edit Part
\''.$this->data['Part']
['name'].'\'');?></legend>
<?php
echo $form->input('id');
echo $form->input('name');
echo $form->input('part_number');
echo $form->input('part_nokia');
echo $form->input('location');
echo $form->input('stock');
echo $form->input('price');
$n = count ($selectedModels);
?>
<div class="input">
<?php
foreach ($models as $field => $value) {
for ($i = 0; $i < $n; $i++) {
if ($field ==
$selectedModels[$i]['ModelsPart']['model_id']) {
echo '<div
class="checkbox"><input type="checkbox"
name="data[ModelsPart][model_id][]" value="'.$field.'"
checked><label>'.$value.'</label></div>';
continue;
}
}
echo '<div class="checkbox"><input
type="checkbox"
name="data[ModelsPart][model_id][]" value="'.$field.'"><label>'.
$value.'</label></div>';
}
?>
</div>
<?
echo $form->hidden('modified',
array("value" => date('Y-m-d
H:i:s')));
?>
</fieldset>
<?php echo $form->end('Submit');?>
</div>
But that doesn't work properly. Does Cake have an easier solution for
this? Or do I have to use this type of loops to check whether model is
selected or not?
Thanks for your help.
Reza Muhammad
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---