Hallo,
ich habe ein Problem mit CakePHP3 und dem speichern von neuen Entity und deren Associations.
Ich bekomme einfach die Associations nicht gespeichert, obwohl ich mMn alles nach der Anleitung in den Docs mache. Hier der Code:
Der Controller
Hiere meine TableModels:
Die Datenbank schaut so aus:
Auf der Clientseite erstelle ich ein Formular, welches so aussieht:
Ich habe vorher nur bei Abfragen mit Associations gearbeitet und dachte durch das hinzufügen der Flags in der save-Methode in Verbindung mit den Angaben in den Models, müsste es doch eigentlich funktionieren.
Das Script wird ohne Fehlermeldungen ausgeführt. Im Debug erscheint nur dass Articles befüllt wird. Die andere Tabelle wird ignoriert.
Hat jemand eine Idee wie es funktioniert?
ich habe ein Problem mit CakePHP3 und dem speichern von neuen Entity und deren Associations.
Ich bekomme einfach die Associations nicht gespeichert, obwohl ich mMn alles nach der Anleitung in den Docs mache. Hier der Code:
Der Controller
PHP-Code:
$articles = TableRegistry::get('Articles');
$article = $articles->newEntity($this->request->data);
if ($this->request->is('post')) {
$valid = $articles->validate($article, [
'associated' => ['Topics']
]);
if ( $valid ) {
$articles->save($article, [
'validate' => false,
'associated' => ['Topics']
]);
}
}
PHP-Code:
class ArticlesTable extends Table {
public function initialize(array $config) {
$this->primaryKey('article_id');
$this->belongsTo ( 'Topics', [
'targetForeignKey' => 'topic_id'
]);
}
}
class TopicsTable extends Table {
public function initialize(array $config) {
$this->primaryKey('topic_id');
$this->hasMany ( 'Articles', [
'targetForeignKey' => 'article_id'
]);
}
Code:
CREATE TABLE `articles` ( `article_id` int(11) NOT NULL AUTO_INCREMENT, `topic_id` int(11) DEFAULT NULL, PRIMARY KEY (`article_id`) ) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=latin1; CREATE TABLE `topics` ( `topic_id` int(11) NOT NULL AUTO_INCREMENT, `topic_title` varchar(45) DEFAULT NULL, PRIMARY KEY (`topic_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
HTML-Code:
<input type="text" id="articles-heading" maxlength="45" required="required" name="Articles[heading]"> <input type="text" id="articles-topics-topic-title" list="topics" name="Articles[Topics][topic_title]">
Das Script wird ohne Fehlermeldungen ausgeführt. Im Debug erscheint nur dass Articles befüllt wird. Die andere Tabelle wird ignoriert.
Hat jemand eine Idee wie es funktioniert?
Kommentar