[置顶] 增强型的FLEA_Db_TableDataGateway,提供按字段名称操作的功能

11 篇文章 0 订阅
8 篇文章 0 订阅

<?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);
		}			
	}
}
?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值