问题:使用Yii的AR类.save(),报以下错
CDbCommand 无法执行 SQL 语句: SQLSTATE[HY000]: General error: 1400 OCIStmtExecute: ORA-01400: cannot insert NULL into ("TEST"."QUICK_LINK_INFO"."LINK_ID") (ext\pdo_oci\oci_statement.c:146). The SQL statement executed was: INSERT INTO QUICK_LINK_INFO (link_name, link_href, link_icon, link_target, admin_login) VALUES (:yp0, :yp1, :yp2, :yp3, :yp4) RETURNING link_id INTO :RETURN_ID
其中,link_id是主键,在数据库里设为PK了。是唯一主键。
解决方法:
1、扒到yii框架的/db/oci/COciCommandBuilder.php
2、见createInsertCommand
3、为quick_link_info建一个seq,名字为QUICK_LINK_INFO_SEQ。然后改代码
/**
* Creates an INSERT command.
* @param mixed $table the table schema ({@link CDbTableSchema}) or the table name (string).
* @param array $data data to be inserted (column name=>column value). If a key is not a valid column name, the corresponding value will be ignored.
* @return CDbCommand insert command
*/
public function createInsertCommand($table,$data) {
$this->ensureTable($table);
$fields=array();
$values=array();
$placeholders=array();
$i=0;
foreach($data as $name=>$value) {
//&& ($value!==null || $column->allowNull)
$column=$table->getColumn($name);
//liuxq
if($column->dbType === "NUMBER($column->size)" ||
$column->dbType === "CHAR($column->size)" ||
$column->dbType === "VARCHAR2($column->size)" ||
$column->dbType === "NVARCHAR2($column->size)"
) {
$fields[]=$column->rawName;//列名
if($value instanceof CDbExpression)
$placeholders[]=$value->expression;
else {
$placeholders[]=self::PARAM_PREFIX.$i;
$values[self::PARAM_PREFIX.$i]=$column->typecast($value);
$i++;
}
}
}
if(is_string($table->primaryKey)) {
//liuxq 2013-1-5 修改以下代码
$fields[] = $table->primaryKey;
$placeholders[] = $table->rawName.'_SEQ.nextval';
$sql="INSERT INTO {$table->rawName} (".implode(', ',$fields).') VALUES ('.implode(', ',$placeholders).')';
$command=$this->getDbConnection()->createCommand($sql);
//$command->bindParam(':RETURN_ID', $this->returnID, PDO::PARAM_INT, 12);
$table->sequenceName = $table->rawName.'_SEQ.nextval';//'RETURN_ID';
}
else
{
$sql="INSERT INTO {$table->rawName} (".implode(', ',$fields).'".") VALUES ('.implode(', ',$placeholders).')';
$command=$this->getDbConnection()->createCommand($sql);
}
foreach($values as $name=>$value)
$command->bindValue($name,$value);
return $command;
}
4、问题解决。如果大家有更好的方法,请告诉我呀 ~~
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)