php - Error inserting a record into two Tables with a FK relationship using Phalcon -
i have 2 tables in mysql database, created this:
create table table1 ( id int auto_increment, name varchar(10), primary key(id) ) engine=innodb
and
create table table2 ( id_fk int, stuff varchar(30), primary key (`id_fk`), constraint fk_id foreign key(id_fk) references table1(id) on delete cascade ) engine=innodb
(these not original tables. point table2 has foreign key referencing primary key in table 1)
now in code, add entries both of tables within 1 transaction. i'm doing this:
$table1 = new table1(); $table1->settransaction($transaction); $table1->name($name); if (!$table->create()) { foreach ($table1->getmessages() $message) { $this->flash->error($message); } $transaction->rollback("can't save table1"); } else { $table2 = new table2(); $table2->settransaction($transaction); $table2->setid($table1->getid()); $table2->setstuff($stuff); if (!$table2->create()) { foreach ($table2->getmessages() $message) { $this->flash->error($message); } $transaction->rollback("can't save password"); } $transaction->commit();
the table1 create() successful, however, when attempt table2 create, errors out constraint violation stating value of field "id" not exist on referenced table.how can use phalcon transaction save data 2 referenced tables in single transaction. thanks.
Comments
Post a Comment