在fuelphp中查找id不是主键的记录
Find a record where id is not the primary key in fuelphp
如果 table 的主键不是 id
列,有没有办法在 fuelphp 中使用 ORM 查找记录?
例如,listing_id
是我的主键。我无法通过以下方式获得所需的结果:
$listing_id = Input::get('listing_id');
$entry = Model_ExampleData::find($listing_id);
受保护的静态 $_primary_key
By default this is set to array('id'), if you use another column name or multiple primary keys you need to set this property.
class Model_Article extends Orm\Model
{
protected static $_primary_key = array('aid');
}
The primary key must be a real primary key: unique and unchanging. Don't use it for other purposes (like a foreign key in a one-one relation) as well, that won't work as the PK can't be changed. The Orm won't check this, and while it might seem to work at first glance: you'll get into trouble.
It is not required for the PK to be auto_increment (though preferred) and you can specify the PK yourself, but only the first time. Once it's set, it's set.
Docs .
为此你有神奇的方法前缀:find_by_
通过添加此前缀,您可以告诉 FuelPHP 使用模型中的任意字段查找记录而无需修改模型配置,并且拆分字段名称可以实现多个条件通过 _and_ 他们之间。
Model_ExampleData::find_by_FIELD($value)
Model_ExampleData::find_by_FIELD1_and_FIELD2($value1, $value2)
Model_ExampleData::find_all_by_FIELD($value)
Model_ExampleData::find_all_by_FIELD1_and_FIELD2($value1, $value2)
Model_ExampleData::count_by_FIELD($value1)
如果 table 的主键不是 id
列,有没有办法在 fuelphp 中使用 ORM 查找记录?
例如,listing_id
是我的主键。我无法通过以下方式获得所需的结果:
$listing_id = Input::get('listing_id');
$entry = Model_ExampleData::find($listing_id);
受保护的静态 $_primary_key
By default this is set to array('id'), if you use another column name or multiple primary keys you need to set this property.
class Model_Article extends Orm\Model
{
protected static $_primary_key = array('aid');
}
The primary key must be a real primary key: unique and unchanging. Don't use it for other purposes (like a foreign key in a one-one relation) as well, that won't work as the PK can't be changed. The Orm won't check this, and while it might seem to work at first glance: you'll get into trouble. It is not required for the PK to be auto_increment (though preferred) and you can specify the PK yourself, but only the first time. Once it's set, it's set.
Docs .
为此你有神奇的方法前缀:find_by_
通过添加此前缀,您可以告诉 FuelPHP 使用模型中的任意字段查找记录而无需修改模型配置,并且拆分字段名称可以实现多个条件通过 _and_ 他们之间。
Model_ExampleData::find_by_FIELD($value)
Model_ExampleData::find_by_FIELD1_and_FIELD2($value1, $value2)
Model_ExampleData::find_all_by_FIELD($value)
Model_ExampleData::find_all_by_FIELD1_and_FIELD2($value1, $value2)
Model_ExampleData::count_by_FIELD($value1)