Hi,
I am fairly new to Cake and currently are trying to get some things to
work with Cake 1.2. Generally, everything looks nice and
straight-forward, so I have to say, I really like Cake.
Now, my problem:
I have two models (Product and Group), where each product can belong to
one or more groups, so it is modelled as a HABTM relation.
// MODELS:
// Product:
class Product extends AppModel {
var $name = 'Product';
var $hasAndBelongsToMany = array(
'Group' => array(
'className' => 'Group',
'joinTable' => 'groups_products',
'foreignKey' => 'product_id',
'associationForeignKey' => 'group_id'
)
);
}
// Group:
class Group extends AppModel {
var $name = 'Group';
var $hasAndBelongsToMany = array(
'Product' => array(
'className' => 'Product',
'joinTable' => 'groups_products',
'foreignKey' => 'group_id',
'associationForeignKey' => 'product_id',
'order' => 'Product.bot ASC'
)
);
}
If I use scaffolding for the products controller, I get an edit action
that is working out-of-the-box, it displays a select box for groups,
the currently related groups pre-selected.
Now i wanted to write my own edit action for products, and tried this
as a start:
// Controller:
class ProductsController extends AppController {
var $name = 'Products';
var $scaffold;
var $helpers = array('Html', 'Form');
var $uses = array('Product','Group');
function edit($id) {
$this->Product->id = $id;
$this->data = $this->Product->read();
$fields = $this->generateFieldNames();
$this->set('fieldnames', $fields);
$this->set('data',$this->data);
}
}
// View edit.thtml
echo $form->create('Product');
echo $form->inputs($fieldnames);
echo $form->end();
This generates the same fields as when using scaffolding, but the
related groups are NOT preselected (all other inputs have the correct
values set for the given ID).
Then, I tried to generate the fields "by hand", so my view looks like
this:
// View edit.thtml
echo $form->create('Product');
echo $form->input('bot');
echo $form->input('Group/Group');
echo $form->end();
It generates an text input for field 'bot' and populates it with
correct data. It then creates it with another text input, which is
empty.
How can I generate a multiple select field, that contains the correct
group names from groups-table and preselects the right values?
Btw, if I output $data in the view, it contains the correct
information, for the selected product and the associated group(s).
Any hints? Do you need more information?
TIA, Marco
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" 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
-~----------~----~----~----~------~----~------~--~---