1 官网:http://docs.iphalcon.cn/reference/models.html#finding-records
2 具体操作实例
数据:
mysql> select * from robots; +----+------------+------------+------+ | id | name | type | year | +----+------------+------------+------+ | 1 | Robotina | mechanical | 1972 | | 2 | Astro Boy | mechanical | 1952 | | 3 | Terminator | cyborg | 2029 | +----+------------+------------+------+
数组方式使用:
$robot = Robots::findFirst(3); //查看前三条数据(也可以和find一样,添加条件查询)
$robots = Robots::find([
"type = 'virtual'",
"order" => "name", //按照name排序 'order'=>' id asc '
"limit" => 100, //限制显示100
'conditions'=>'area_id in ({aids:array})', //'conditions' => 'area IN ({area:array}) AND device_type = :device_type:',
'bind' => ['area' => $ids,'device_type'=>$register_device_type], //'bind'=>['aids'=>$limit_A],
'columns'=>'id,did,device_name,device_install_address,defense_status,device_type,status', //要查询的结果
] );
参数:
参数 | 描述 | 举例 |
---|---|---|
conditions | 查询操作的搜索条件。用于提取只有那些满足指定条件的记录。默认情况下 Phalcon\Mvc\Model 假定第一个参数就是查询条件。 | "conditions" => "name LIKE 'steve%'" |
columns | 只返回指定的字段,而不是模型所有的字段。 当用这个选项时,返回的是一个不完整的对象。 | "columns" => "id, name" |
bind | 绑定与选项一起使用,通过替换占位符以及转义字段值从而增加安全性。 | "bind" => ["status" => "A", "type"=> "some-time"] |
bindTypes | 当绑定参数时,可以使用这个参数为绑定参数定义额外的类型限制从而更加增强安全性。 | "bindTypes" =>[Column::BIND_PARAM_STR,Column::BIND_PARAM_INT] |
order | 用于结果排序。使用一个或者多个字段,逗号分隔。 | "order" => "name DESC, status" |
limit | 限制查询结果的数量在一定范围内。 | "limit" => 10 |
offset | Offset the results of the query by a certain amount | "offset" => 5 |
group | 从多条记录中获取数据并且根据一个或多个字段对结果进行分组。 | "group" => "name, status" |
for_update | 通过这个选项, Phalcon\Mvc\Model 读取最新的可用数据,并且为读到的每条记录设置独占锁。 | "for_update" => true |
shared_lock | 通过这个选项, Phalcon\Mvc\Model 读取最新的可用数据,并且为读到的每条记录设置共享锁。 | "shared_lock" => true |
cache | 缓存结果集,减少了连续访问数据库。 | "cache" => ["lifetime" => 3600,"key" => "my-find-key"] |
hydration | Sets the hydration strategy to represent each returned record in the result | "hydration" =>Resultset::HYDRATE_OBJECTS |
使用面向对象方式:
$build= new \Phalcon\Mvc\Model\Query\Builder();
主要就是数据库查询 要多熟悉和掌握,很重要,很基本