Ich bin noch ganz neu bei CakePHP und gehe damit gerade mein erstes Projekt an.
Ich habe eine habtm Beziehung mit einer Linking Table. Ich habe es jetzt so weit, dass die Daten angezeigt werden, aber ich kann Mutationen nicht in die Linking Table speichern.
Es geht um folgende Tabellen

Ich habe jetzt bei dem Product Model eine habtm Beziehung hinzugefügt.
- Code: Alles auswählen
- <?php
class Product extends AppModel {
var $name = 'Product';
var $hasAndBelongsToMany = array(
'Client' => array
(
'className' => 'Client',
'joinTable' => 'clients_products',
'foreignKey' => 'product_id',
'associationForeignKey' => 'client_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
var $validate = array(
'name' => array(
'rule' => 'notEmpty'
)
);
}
?>
Im Controller habe ich die Funktion add_client.
- Code: Alles auswählen
- <?php
class productsController extends AppController {
var $helpers = array ('Html','Form');
var $name = 'Products';
function index() {
$this->set('products', $this->Product->find('all'));
}
function add_client ($id = null) {
$this->set('clients', $this->Product->Client->find('list'));
if (!empty($this->data)) {
if ($this->Product->save($this->data)) {
$this->Session->setFlash('The client is attached to the product.');
$id = $this->Product->id;
$this->redirect(array('action' => 'add_client/'.$id.''));
}
}
$this->Product->id = $id;
$this->set('product', $this->Product->read());
}
}
?>
und zu guter Letzt noch der Code der view.
- Code: Alles auswählen
- <!-- File: /app/views/products/add_Client.ctp -->
<h1><?php echo $product['Product']['name'] ?></h1>
<h2>Add Client</h2>
<?php echo $form->create('Product'); ?>
<?php echo $form->input('Product.id', array('type'=>'hidden', 'value' => $product['Product']['id'])); ?>
<?php echo $form->input('clients'); ?>
<?php echo $form->end('Add Client'); ?>
<table>
<th>ID</th>
<th>Client</th>
<th>Action</th>
<?php foreach ($product['Client'] as $client): ?>
<tr>
<td><?php echo $client['id']; ?></td>
<td><?php echo $client['name']; ?></td>
</tr>
<?php endforeach; ?>
</table>
In der View habe ich eine Liste von allen Clients die an das Produkt angefügt sind. Ich kann Sie aus der Liste auswählen, aber wenn ich es in die DB schreiben will, kommt die Flashmeldung es sei geschehen, aber in der Tabelle findet man keine neue Einträge.
Wie gesagt bin ich neu bei CakePHP und es ist gut möglich, dass es ein kleiner Fehler ist, den ich nicht sehe. Ich bin für sämtliche Hinweise und Anregungen dankbar....