diff --git a/lib/Doctrine/Export/Sqlite.php b/lib/Doctrine/Export/Sqlite.php index 7f9f617bb..955d71112 100644 --- a/lib/Doctrine/Export/Sqlite.php +++ b/lib/Doctrine/Export/Sqlite.php @@ -159,14 +159,20 @@ class Doctrine_Export_Sqlite extends Doctrine_Export } // sqlite doesn't support foreign key declaration but it parses those anyway + + $fk = array(); if (isset($options['foreignKeys']) && ! empty($options['foreignKeys'])) { - foreach($options['foreignKeys'] as $definition) { - $queryFields .= ', ' . $this->getForeignKeyDeclaration($definition); + foreach ($options['foreignKeys'] as $definition) { + //$queryFields .= ', ' . $this->getForeignKeyDeclaration($definition); + + if (isset($definition['onDelete'])) { + $fk[] = $definition; + } } } if (isset($options['indexes']) && ! empty($options['indexes'])) { - foreach($options['indexes'] as $index => $definition) { + foreach ($options['indexes'] as $index => $definition) { $queryFields .= ', ' . $this->getIndexDeclaration($index, $definition); } } @@ -174,7 +180,38 @@ class Doctrine_Export_Sqlite extends Doctrine_Export $name = $this->conn->quoteIdentifier($name, true); $query = 'CREATE TABLE ' . $name . ' (' . $queryFields . ')'; - return $this->conn->exec($query); + try { + if ( ! empty($fk)) { + $this->conn->beginTransaction(); + } + $ret = $this->conn->exec($query); + + if ( ! empty($fk)) { + foreach ($fk as $definition) { + + $query = 'CREATE TRIGGER doctrine_' . $name . '_cscd_delete ' + . 'AFTER DELETE ON ' . $name . ' FOR EACH STATEMENT ' + . 'BEGIN ' + . 'DELETE FROM ' . $definition['foreignTable'] . ' WHERE '; + + $local = (array) $definition['local']; + foreach((array) $definition['foreign'] as $k => $field) { + $query .= $field . ' = old.' . $local[$k] . ';'; + } + + $query .= 'END;'; + + $this->conn->exec($query); + } + + $this->conn->commit(); + } + } catch(Doctrine_Exception $e) { + + $this->conn->rollback(); + + throw $e; + } } /** * create sequence diff --git a/lib/Doctrine/Table.php b/lib/Doctrine/Table.php index c133874b3..203edca0a 100644 --- a/lib/Doctrine/Table.php +++ b/lib/Doctrine/Table.php @@ -1121,9 +1121,10 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable if ( ! is_array($key)) { $key = array($key); } + foreach ($key as $k) { if ( ! isset($this->data[$k])) { - throw new Doctrine_Exception("Primary key value for $k wasn't found"); + throw new Doctrine_Table_Exception("Primary key value for $k wasn't found"); } $id[] = $this->data[$k]; } diff --git a/tests/Export/SqliteTestCase.php b/tests/Export/SqliteTestCase.php index 3c13664bc..a3b4baaf8 100644 --- a/tests/Export/SqliteTestCase.php +++ b/tests/Export/SqliteTestCase.php @@ -1,4 +1,35 @@ . + */ + +/** + * Doctrine_Export_Sqlite_TestCase + * + * @package Doctrine + * @author Konsta Vesterinen + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @category Object Relational Mapping + * @link www.phpdoctrine.com + * @since 1.0 + * @version $Revision$ + */ class Doctrine_Export_Sqlite_TestCase extends Doctrine_UnitTestCase { public function testCreateDatabaseDoesNotExecuteSql() { try { @@ -91,11 +122,15 @@ class Doctrine_Export_Sqlite_TestCase extends Doctrine_UnitTestCase { $this->assertEqual($this->adapter->pop(), 'CREATE TABLE sometable (id INTEGER UNSIGNED PRIMARY KEY AUTOINCREMENT, name VARCHAR(4), INDEX myindex (id ASC, name DESC))'); } - public function testExportSupportsForeignKeys() + public function testExportSupportsEmulationOfCascadingDeletes() { $r = new ForeignKeyTest; - $this->assertEqual($this->adapter->pop(), 'CREATE TABLE foreign_key_test (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(2147483647), code INTEGER, content VARCHAR(4000), parent_id INTEGER, FOREIGN KEY parent_id REFERENCES foreign_key_test(id), FOREIGN KEY id REFERENCES foreign_key_test(parent_id) ON UPDATE RESTRICT ON DELETE CASCADE)'); + $this->assertEqual($this->adapter->pop(), 'COMMIT'); + $this->assertEqual($this->adapter->pop(), 'CREATE TRIGGER doctrine_foreign_key_test_cscd_delete AFTER DELETE ON foreign_key_test BEGIN DELETE FROM foreign_key_test WHERE parent_id = old.id;END;'); + $this->assertEqual($this->adapter->pop(), 'CREATE TABLE foreign_key_test (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(2147483647), code INTEGER, content VARCHAR(4000), parent_id INTEGER)'); + $this->assertEqual($this->adapter->pop(), 'BEGIN TRANSACTION'); } + } ?>