stdtech / dbo
PHP 5.3 简单的 ORM
v1.0
2013-04-24 21:06 UTC
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-09-23 11:12:35 UTC
README
PHP 5.3 简单的 ORM
使用示例
创建表结构
$T["pack"] = array( "table" => "pack", "key" => "id", "fields" => array( "id" => "int", "name" => "char", "date_created" => "date", "desired_size" => "int", "pages_min" => "int", "pages_max" => "int", "theme_id" => "int", "is_full" => "int" ) );
初始化数据库连接并“喂入”数据库
$db = new DBObject($config); $db->init_tables($T);
这样是从数据库中“取出”的一行——关联数组
$row = array( "id" => 1, "name" => "Alex", "date_created" => "2014-03-03 11:12:33", "desired_size" => 15, ... );
从“pack”表中通过主键(列“id”)获取行
$rows = $db->get('pack', 3);
将返回
array( 0 => array( "id" => 3, "name" => "Mike", "date_created" => "2014-03-03 11:12:33", "desired_size" => 15, ... ) )
从“pack”表中通过主键获取列“name”等于“Alex”的行
$filter = array( "name" => "Alex" ); $db->get('pack', $filter)
将返回
array( 0 => array( "id" => 1, "name" => "Alex", "date_created" => "2014-03-03 11:12:33", "desired_size" => 15, ... ), ... )
// 从“pack”表中通过主键获取列“name”大于“Alex”的行
$filter = array( "name" => ">Alex" // символ '>' в начале строки, допускаются <, <=, >, >=, =, != (по-умолчанию =) ); $db->get('pack', $filter)
将返回
array( 0 => array( "id" => 1, "name" => "Alex", "date_created" => "2014-03-03 11:12:33", "desired_size" => 15, ... ), ... )
更新表pack,其中id=3,并将列设置为来自 $row 的值(如果 $row 中省略了某些列,则表中的数据将设置为 NULL)
$db->set('pack', $row, 3);
更新表pack,其中id=3,并仅设置来自 $row 的列($row 中缺失的列将保持不变)
$db->set('pack', $row, 3, false);
更新表pack,其中记录与 $filter 中的条件匹配
$db->set('pack', $row, $filter);
添加新记录
$db->set('pack', $row);
过滤器的结构
$filter = array( "name" => ">Alex", "col_name_1" => "scalar" "col_name_2" => array("scalar_1", "scalar_2") );
所有列通过 AND 联合,而一个列内部通过 OR 联合,即在上述示例中将是
WHERE name > "Alex" AND col_name_1 = "scalar" AND (col_name_2 = "scalar_1" OR col_name_2 = "scalar_2")