mein Code zum erstellen und updaten von Kapiteln sieht zurzeit so aus:
- Code: Alles auswählen
function admin_add() {
if (!empty($this->data)) {
$this->data['Cat']['slug'] = $this->stringToSlug($this->data['Cat']['name']);
if ($this->Cat->save($this->data)) {
$this->Session->setFlash('Das Kapitel wurde erfolgreich erstellt');
$this->redirect(array('controller' => 'cats','action' => 'admin_index'));
}
}
$parents[0] = "Kein Oberkapitel";
$catlist = $this->Cat->generatetreelist(NULL,NULL,NULL," - ");
if($catlist) {
foreach ($catlist as $key=>$value)
$parents[$key] = $value;
}
$this->set(compact('parents'));
}
function admin_edit($id = NULL) {
if (!empty($this->data)) {
$this->data['Cat']['slug'] = $this->stringToSlug($this->data['Cat']['name']);
if($this->Cat->save($this->data)) {
$this->Session->setFlash('Das Kapitel wurde erfolgreich bearbeitet');
$this->redirect(array('controller' => 'cats','action' => 'admin_index'));
}
}
if($id==null) $this->redirect(array('controller' => 'cats','action' => 'admin_index'));
$this->data = $this->Cat->read(null, $id);
$parents[0] = "Kein Oberkapitel";
$catlist = $this->Cat->generatetreelist(NULL,NULL,NULL," - ");
if($catlist) {
foreach ($catlist as $key=>$value)
$parents[$key] = $value;
}
$this->set(compact('parents'));
}
Die Views sehen beide so aus(nur anderer Text):
- Code: Alles auswählen
<h2>Kapitel ändern</h2>
<?=$this->Form->create('Cat')?>
<fieldset>
<legend>Kapitel hinzufügen</legend>
<p>
<?=$this->Form->input('name', array('label' => 'Name'))?>
</p>
<p>
<?=$this->Form->input('description', array('type' => 'textarea','label' => 'Beschreibung'))?>
</p>
<p>
<?=$this->Form->input('parent_id', array('label' => 'Oberkapitel'))?>
</p>
</fieldset>
<?=$this->Form->end("Kapitel updaten")?>
Wenn ich nun eine neue Kategorie erstelle und als Oberkapitel "Kein Oberkapitel" wähle, trägt er 0 korrekt bei parent_id in die Datenbank ein. Beim Updaten macht er jedoch aus der "0" jedes Mal "NULL", obwohl vorher value="0" im Select-Feld markiert wurde. Woran kann das liegen?