我正在使用propel als ORM和Sqlite als本地数据库。配置似乎有效(可以创建简单的项目,例如新用户)。但我插入相关项目时遇到麻烦(例如:无法创建与新用户相关的新订单):
$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
我总是得到以下异常:
/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
我的数据库schema.xml
如下所示:
<?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>
我已经删除了datetime列,所以这不是错误,但可能是某种外键问题。
修改
我试图找到错误并调试我生成的Base/Order.php
。
PDO准备语句$con->prepare($sql);
将以下字符串抛出异常:
INSERT INTO order(id,user_id)VALUES(:p0,:p1)
异常包含消息near "order": syntax error"
,如果我尝试在sqlite控制台中运行一个非常相似的语句,我得到:
SQL&GT; INSERT INTO order(id,user_id)VALUES(0,1) [2015-02-01 11:45:39] [1] [SQLITE_ERROR] SQL错误或缺少数据库(接近“订单”:语法错误)
我不知道如何“选择”正确的数据库或者为什么这个查询无效,因为它可以插入到用户表中并且两者都在同一个数据库中,...