&引用;错误:SQLSTATE[42S22]:未找到列:1054“;在CakePHP 2.x中使用自定义主键时
我试图用以下代码在CakePHP 2.x中构建一个下拉列表: 控制器&引用;错误:SQLSTATE[42S22]:未找到列:1054“;在CakePHP 2.x中使用自定义主键时,cakephp,orm,cakephp-2.x,Cakephp,Orm,Cakephp 2.x,我试图用以下代码在CakePHP 2.x中构建一个下拉列表: 控制器 $Propertytype=$this->Propertytype->find("list",array("fields" => array("Propertytype.propertytype_id,Propertytype.propertytype_name,Propertytype.propertytype_id"),"order" => array("Propertytype.propertyt
$Propertytype=$this->Propertytype->find("list",array("fields" => array("Propertytype.propertytype_id,Propertytype.propertytype_name,Propertytype.propertytype_id"),"order" => array("Propertytype.propertytype_id" => "desc"),"conditions" => array("Propertytype.propertytype_status" => "0")));
$this->set("propertytype",$Propertytype);
看法
如何将其更改为
Propertytype.property\u id
作为选项值?您必须在类Propertytype
中定义$primaryKey
:
class Propertytype extends AppModel
{
public $primaryKey = 'property_id';
}
您还必须使用正确的语法编写查询:
$Propertytype=$this->Propertytype->find("list",array(
"fields" => array(
"Propertytype.propertytype_id",
"Propertytype.propertytype_name"
),
"order" => array("Propertytype.propertytype_id" => "desc"),
"conditions" => array("Propertytype.propertytype_status" => "0")
));
您忘记了引号。请参阅此链接。我没有使用Propertytype.id列时出现相同错误。我将其重命名为Propertytype.Propertytype\u id。但如果我运行该查询,它会询问Propertytype.id。
class Propertytype extends AppModel
{
public $primaryKey = 'property_id';
}
$Propertytype=$this->Propertytype->find("list",array(
"fields" => array(
"Propertytype.propertytype_id",
"Propertytype.propertytype_name"
),
"order" => array("Propertytype.propertytype_id" => "desc"),
"conditions" => array("Propertytype.propertytype_status" => "0")
));