为什么DBAL返回SQL语法错误?

时间:2013-08-25 14:00:22

标签: php sql codeigniter symfony doctrine-orm

我正在使用Doctrine 2和CodeIgniter。

我在某些函数中查询会触发SQL错误。我无法找到原因

该功能如下:

private function _key_exists($key)
    {
        $this->CI->load->library('doctrine');
        $em = $this->CI->doctrine->em;
        echo "<br />Key : ".$key;

        $key_object = $em->getRepository( 'Entity\Key' )->findOneBy( array( 'key' => $key ) );

        if( $key_object )
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
        // return $this->db->where(config_item('rest_key_column'), $key)->count_all_results(config_item('rest_keys_table')) > 0;
    }

当Key ENtity为:

<?php

namespace Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Keys
 *
 * @ORM\Table(name="keys")
 * @ORM\Entity
 */
class Key
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", precision=0, scale=0, nullable=false, unique=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="key", type="string", length=40, precision=0, scale=0, nullable=false, unique=false)
     */
    private $key;

    /**
     * @var integer
     *
     * @ORM\Column(name="level", type="integer", precision=0, scale=0, nullable=false, unique=false)
     */
    private $level;

    /**
     * @var boolean
     *
     * @ORM\Column(name="ignore_limits", type="boolean", precision=0, scale=0, nullable=false, unique=false)
     */
    private $ignoreLimits;

    /**
     * @var boolean
     *
     * @ORM\Column(name="is_private_key", type="boolean", precision=0, scale=0, nullable=false, unique=false)
     */
    private $isPrivateKey;

    /**
     * @var string
     *
     * @ORM\Column(name="ip_addresses", type="text", precision=0, scale=0, nullable=true, unique=false)
     */
    private $ipAddresses;

    /**
     * @var integer
     *
     * @ORM\Column(name="date_created", type="integer", precision=0, scale=0, nullable=false, unique=false)
     */
    private $dateCreated;

/* Getters and Setters ... */

}

错误信息是:

  

Doctrine \ DBAL \ DBALException:执行时发生异常   &#39; SELECT t0.id AS id1,t0.key AS key2,t0.level AS level3,   t0.ignore_limits AS ignore_limits4,t0.is_private_key AS   is_private_key5,t0.ip_addresses AS ip_addresses6,t0.date_created AS   date_created7 FROM keys t0 WHERE t0.key =?限制1&#39;与params   {&#34; 1&#34;:&#34; 0fedfa4d50653317df76a4dba79f9f07cd7a8273&#34;}:SQLSTATE [42000]:   语法错误或访问冲突:1064 SQL中有错误   句法;查看与MySQL服务器版本对应的手册   正确的语法使用near&#39;键t0 WHERE t0.key =   &#39; 0fedfa4d50653317df76a4dba79f9f07cd7a8273&#39;限制1&#39;在第1行   E:\程序\ WAMP \ WWW \ MYAPP \程序\库\原则\ DBAL \ DBALException.php   第47行

我确实在输出中看到了Key : c242c67787bd0b9a9b11a54fc942fde50f099235

非常感谢你的帮助

2 个答案:

答案 0 :(得分:2)

这个特定查询失败的原因是因为'key'是mysql中的保留字。

FROM keys t0 WHERE t0.key =?限制1'

http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

所以我猜你在改变之前没有做到这一点?

将属性重命名为value。 $ key-&gt;值反正比$ key-&gt;键更好。

答案 1 :(得分:0)

引用表和列名称需要使用定义中的刻度明确地完成,例如:

/**
 * @ORM\Column(name="`key`", type="string", length=255, nullable=false)
 */
protected $key;

http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/basic-mapping.html#quoting-reserved-words