使用Doctrine保存行时,Doctrine_Connection_Mysql_Exception

时间:2011-08-07 16:07:45

标签: mysql orm doctrine php-5.3

我有一张简单的桌子。这是模特图片:

abstract class BaseImages extends Doctrine_Record
{
    public function setTableDefinition()
    {
        $this->setTableName('images');
        $this->hasColumn('id', 'integer', 4, array(
             'type' => 'integer',
             'length' => 4,
             'fixed' => false,
             'unsigned' => false,
             'primary' => true,
             'autoincrement' => true,
             ));
        $this->hasColumn('file', 'string', 45, array(
             'type' => 'string',
             'length' => 45,
             'fixed' => false,
             'unsigned' => false,
             'primary' => false,
             'notnull' => false,
             'autoincrement' => false,
             ));
        $this->hasColumn('order', 'integer', 4, array(
             'type' => 'integer',
             'length' => 4,
             'fixed' => false,
             'unsigned' => false,
             'primary' => false,
             'default' => '0',
             'notnull' => false,
             'autoincrement' => false,
             ));
    }

    public function setUp()
    {
        parent::setUp();
        $this->hasMany('AuthorsHasImages', array(
             'local' => 'id',
             'foreign' => 'images_id'));

        $this->hasMany('Banners', array(
             'local' => 'id',
             'foreign' => 'images_id'));

        $this->hasMany('BooksHasImages', array(
             'local' => 'id',
             'foreign' => 'images_id'));

        $this->hasMany('Collections', array(
             'local' => 'id',
             'foreign' => 'images_id'));

        $this->hasMany('IllustratorsHasImages', array(
             'local' => 'id',
             'foreign' => 'images_id'));

        $this->hasMany('MerchandisingHasImages', array(
             'local' => 'id',
             'foreign' => 'images_id'));
    }
}

当我使用时:

$image = new Images();
$image->file = $name;
$image->save();

我有以下错误:

  

致命错误:未捕获的异常'Doctrine_Connection_Mysql_Exception',消息为'SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法错误;查看与您的MySQL服务器版本对应的手册,以便在/ Users/maurogadaleta/Sites/org.blackiebooks的第1行'','order,file)VALUES('0','P8040001.JPG')附近使用正确的语法/phpinc/doctrine/Doctrine/Connection.php:1082堆栈跟踪:#0 /Users/maurogadaleta/Sites/org.blackiebooks/phpinc/doctrine/Doctrine/Connection/Statement.php(269):Doctrine_Connection-> rethrowException(Object (PDOException),Object(Doctrine_Connection_Statement))#1 /Users/maurogadaleta/Sites/org.blackiebooks/phpinc/doctrine/Doctrine/Connection.php(1042):Doctrine_Connection_Statement-> execute(Array)#2 / Users / maurogadaleta / Sites / org.blackiebooks / phpinc / doctrine / Doctrine / Connection.php(687):Doctrine_Connection-> exec('INSERT INTO ima ...',Array)#3 /Users/maurogadaleta/Sites/org.blackiebooks/phpinc /doctrine/Doctrine/Connection/UnitOfWork.php(647):第1082行/Users/maurogadaleta/Sites/org.blackiebooks/phpinc/doctrine/Doctrine/Connection.php中的Doctrine_

有谁知道为什么我会收到此错误以及我可以采取哪些措施来解决问题?

2 个答案:

答案 0 :(得分:3)

使用标识符引用;

$conn->setAttribute(Doctrine_Core::ATTR_QUOTE_IDENTIFIER, true);

http://www.doctrine-project.org/documentation/manual/1_2/hu/configuration#identifier-quoting

答案 1 :(得分:0)

似乎Doctrine没有引用列名,order是MySQL关键字。尝试重命名该列(或强制Doctrine引用它,如果可能的话)。