dimanche 1 février 2015

propel and sqlite not able to insert related items

I am using propel als ORM and Sqlite als local DB. The configuration seems to work (it's possible to create simple items as example a new user). But I have troubles to insert related items (as example: it's not possible to create a new order, which is related to a new user):



$user = new User();
$user->setFirstName('Chris');
$user->setLastName('somename');
$user->setEmail('example@test.com');
$user->setPassword(password_hash("1234", PASSWORD_DEFAULT));
$user->save(); //works fine

$order = new Order();
$order->setUser($user);
$order->save(); //throws PDOException


I always get the following exception:



/usr/bin/php -S localhost:8080 -t /Users/chris/Documents/slim
exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 1 near "order": syntax error' in /Users/chris/Documents/slim/vendor/propel/propel/src/Propel/Runtime/Connection/PdoConnection.php:143
Stack trace:
#0 /Users/chris/Documents/slim/vendor/propel/propel/src/Propel/Runtime/Connection/PdoConnection.php(143): PDO->prepare('INSERT INTO ord...', Array)
#1 /Users/chris/Documents/slim/vendor/propel/propel/src/Propel/Runtime/Connection/StatementWrapper.php(75): Propel\Runtime\Connection\PdoConnection->prepare('INSERT INTO ord...', NULL)
#2 /Users/chris/Documents/slim/vendor/propel/propel/src/Propel/Runtime/Connection/ConnectionWrapper.php(383): Propel\Runtime\Connection\StatementWrapper->prepare(NULL)
#3 /Users/chris/Documents/slim/domain/Base/Order.php(748): Propel\Runtime\Connection\ConnectionWrapper->prepare('INSERT INTO ord...')
#4 /Users/chris/Documents/slim/domain/Base/Order.php(680): Base\Order->doInsert(Object(Propel\Runtime\Connection\ConnectionWrapper))
#5 /Users/chris/Documents/slim/domain/Base/Order.php(632): Base\Order->doSave(Object(Propel\Runtime\Connection\ConnectionWrapper))
#6 [internal function]: Base\Order->Base\{closure}()
#7 /Users/chris/Documents/slim/vendor/propel/propel/src/Propel/Runtime/Connection/TransactionTrait.php(35): call_user_func(Object(Closure))
#8 /Users/chris/Documents/slim/domain/Base/Order.php(645): Propel\Runtime\Connection\ConnectionWrapper->transaction(Object(Closure))
#9 /Users/chris/Documents/slim/index.php(88): Base\Order->save()
...
Next exception 'Propel\Runtime\Exception\PropelException' with message 'Unable to execute INSERT statement [INSERT INTO order (id, user_id) VALUES (:p0, :p1)]' in /Users/chris/Documents/slim/domain/Base/Order.php:765


My db schema.xml looks like this:



<?xml version="1.0" encoding="UTF-8"?>
<database name="shop" defaultIdMethod="native">
<table name="user" phpName="User">
<column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true"/>
<column name="firstname" type="varchar" size="255" required="true" />
<column name="lastname" type="varchar" size="255" required="true" />
<column name="email" type="varchar" size="255" required="true" />
<column name="password" type="varchar" size="255" required="true"/>
<!-- values: customer, employee -->
<column name="role" type="varchar" size="255" defaultValue="customer" required="true"/>
</table>
<table name="product" phpName="Product">
<column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true"/>
<column name="name" type="varchar" size="128" required="true"/>
<column name="img" type="varchar" size="128" required="true"/>
<column name="unit" type="varchar" size="10" required="true"/>
<column name="description" type="varchar" size="255" required="false"/>
<column name="unit_price" type="float" required="true"/>
</table>
<table name="order" phpName="Order">
<column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true" />
<column name="user_id" type="integer" required="true" />
<column name="datetime" type="timestamp" defaultExpr="CURRENT_TIMESTAMP" />
<foreign-key foreignTable="user">
<reference local="user_id" foreign="id" />
</foreign-key>
</table>
<table name="orderposition" phpName="OrderPosition">
<column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true"/>
<column name="order_id" type="integer" required="true"/>
<column name="product_id" type="integer" required="true"/>
<column name="quantity" type="integer" required="true"/>
<foreign-key foreignTable="order">
<reference local="order_id" foreign="id" />
</foreign-key>
<foreign-key foreignTable="product">
<reference local="product_id" foreign="id" />
</foreign-key>
</table>
</database>


I already removed the datetime column so this is not the error, but it might be some sort of foreign key problem.


Aucun commentaire:

Enregistrer un commentaire