"Error: SQLSTATE[42S22]: Column not found: 1054" when using a custom primary key in CakePHP 2.x

"Error: SQLSTATE[42S22]: Column not found: 1054" when using a custom primary key in 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.propertytype_id" => "desc"),"conditions" => array("Propertytype.propertytype_status" => "0")));   
$this->set("propertytype",$Propertytype);

查看

<?php echo $this->Form->input("propertytype",array("class"=>"form-control","data-style"=>"btn-white",
                    "data-live-search"=>"true","label"=>false,"data-size"=>"5","required"=>"required","options"=>$propertytype));
?>  

我没有使用 Propertytype.id 作为主键。我将其重命名为 Propertytype.Propertytype_id。但是当我 运行 查询时,它要求 Propertytype.id

Database Error
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Propertytype.id' in 'field list'

如何将其更改为 Propertytype.property_id 作为选项值?

您必须在 class 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")
));

你忘记引号了。