<?php
if (!PHP5){
die('Helper_Model Must PHP5 Support!');
}
/**
* Helper_Model 是 FLEA_Db_TableDataGateway 的增强类
*
*/
class Helper_Model extends FLEA_Db_TableDataGateway{
//魔术方法 提供 按 字段名称 操作 数据记录的功能
function __call($funcname,$args){
//
//findBy
if (preg_match("/^findBy*/i",$funcname)){
$fieldName = preg_replace('/^findBy*/i','',$funcname);
return $this->findBy($fieldName,$args[0]);
}
//findAllBy
else if (preg_match("/^findAllBy*/i",$funcname)){
$fieldName = preg_replace('/^findAllBy*/i','',$funcname);
return $this->findAllBy($fieldName,$args[0]);
}
//removeBy
else if (preg_match("/^removeBy*/i",$funcname)){
$fieldName = preg_replace('/^removeBy*/i','',$funcname);
return $this->removeBy($fieldName,$args[0],$args[1]);
}
//updateBy
else if (preg_match("/^updateBy*/i",$funcname)){
$fieldName = preg_replace('/^updateBy*/i','',$funcname);
return $this->updateBy($fieldName,$args[0],$args[1]);
} else {
log_message(get_class($this)." 类无此方法: {$funcname}", 'error');
throw new FLEA_Exception(get_class($this) . " 没有这个方法: {$funcname}");
}
}
/**
* 判断字段是否存在
*
* @param string $field
* @return bool
*/
function fieldIsExist($field){
if (in_array(strtoupper($field),$this->fields)){
return true ;
}
log_message("表 {$this->qtableName} 无此字段: [{$field}]", 'debug');
dump("字段: [{$field}] 不存在","表 {$this->qtableName}");
}
/**
* 根据字段名称和值查找一条记录
*
* @param string $field
* @param string $value
* @return array
*/
function findBy($field,$value){
if ($this->fieldIsExist($field))
return $this->find(array($field => $value));
}
/**
* 根据字段名称和值查找所有记录
*
* @param string $field
* @param string $value
* @return array
*/
function findAllBy($field,$value){
if ($this->fieldIsExist($field))
return $this->findAll(array($field => $value));
}
/**
* 删除满足条件且符合指定字段的值的记录 成功返回删除的记录总数,失败返回 0
*
* @param string $field
* @param string $value
* @return int
*/
function removeBy($field,$value,$conditions=null){
if ($this->fieldIsExist($field)){
if(empty($conditions)|| !is_array($conditions))
$conditions = array();
$conditions[$field] = $value ;
return $this->removeByConditions(array($field => $value));
}
}
/**
* 更新满足条件且符合指定字段的值的记录 成功返回更新的记录总数,失败返回 false
*
* @param string $field
* @param string $value
* @param array $conditions
* @return int|boolean
*/
function updateBy($field,$value,$conditions){
if ($this->fieldIsExist($field)){
$row = array($field => $value);
return $this->updateByConditions($conditions, $row);
}
}
}
?>