2012-05-31 50 views
1

简单的Postgres表:布尔类型PostgreSQL的

CREATE TABLE public.test (
    id INTEGER NOT NULL, 
    val BOOLEAN NOT NULL, 
    CONSTRAINT test_pkey PRIMARY KEY(id) 
); 

做到这一点

Yii::app()->db->createCommand()->insert('test', array(
     'id'=>1, 
     'val'=>true, 
    )); 

Everithing还好吧:

Executing SQL: INSERT INTO "test" ("id", "val") VALUES (:id, :val). Bound with :id=1, :val=true 

这样

Yii::app()->db->createCommand()->insert('test', array(
     'id'=>1, 
     'val'=>false, 
    )); 

我得到错误

SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for type boolean: "" 
LINE 1: INSERT INTO "test" ("id", "val") VALUES ('1', '') 
^. The SQL statement executed was: INSERT INTO "test" ("id", "val") VALUES (:id, :val). Bound with :id=1, :val=false 

难道我错了吗?

+5

Php正在将布尔值转换为整数,在'false'值的情况下,转换为“”。看看https://bugs.php.net/bug.php?id=33876#1122477362 – sucotronic

+0

强调在PDO中投射类型,并且你的链接非常有用。非常感谢! –

+0

现在在Yii GitHub上有一个问题: http://github.com/yiisoft/yii/issues/779 –

回答

1

在您的'db'组件中,​​将emulatePrepare属性设置为false/null。将其设置为true通常会触发此错误。

Postgres支持预处理语句,因此不需要模拟。

+0

这解决了我的问题。谢谢。 – Kolyunya