How to use entity classes in php

怪我咯
Release: 2023-03-11 20:56:02
Original
3987 people have browsed it

php is a weakly typed language. Normally, the type of variables is not defined. But if you are a JAVA or .NET developer, you will not be able to adapt to PHP. Or when you want to write an ORM framework similar to hibernate, without the concept of entity classes, it is not easy to control. Let me briefly talk about how to implement the concept of entity classes in PHP.

First build a basic Model class

<?php
class BaseModel{
	private $_tableName;
	
	public function construct($tableName=""){
		$this->_tableName=$tableName;
	}
	
	public function getTableName(){
		return $this->_tableName;
	}
	
	public function getFieldsArray(){
		try {			
			$obj=json_decode(json_encode($this),true);  //此处可能会影响效率,但是为了去除类中的private属性,目前是这么做的
			$fieldsArray=array();
			foreach ($obj as $k=>$v){
				$fieldsArray[]=$k;
			}
			return $fieldsArray;
		} catch (Exception $e) {
			throw new Exception($e,3, $previous);
		}
	}
	
	
	public function find($condition=null){
		try {
			$sql="select ".implode(",",$this->getFieldsArray())." from ".$this->_tableName." ";
			if($condition){
				$sql.=" where ".$condition;
			}else {
				$obj=json_decode(json_encode($this),true);
				$fieldsArray=array();
				foreach ($obj as $k=>$v){
					if($v!=null && $v!=""){
						$fieldsArray[]=$k."=&#39;".$v."&#39;";
					}
				}
				if(count($fieldsArray)>0){
					$sql.=" where ".implode(" and ", $fieldsArray);
				}
			}
			return $sql;	
		} catch (Exception $e) {
			throw new Exception($e,3, $previous);
		}
	}
}
?>
Copy after login

Then let’s build a class that corresponds to the table in the database and will be used in the project

<?php
class MemberModel extends BaseModel{
	public $m_ID;
	public $m_Account;
	public $m_Pwd;
	public $m_TEL;
	public $m_UserID;
	public $m_ChannelID;
	public $m_Status;
	public $m_CreateTime;
	public $m_UpdateTime;
	
}
?>
Copy after login

The following is how the entity class is Went to use it

First time looking at controller

	public function actionSelectMember(){
		try {
			$member=new MemberModel("T_Member");
			$member->m_Account=GetValue::getParam("Account");
			$member->m_Pwd=GetValue::getParam("Pwd");
			$result=MemberService::selectMember($member);
			if($result){
				Yii::app()->session["MemberID"]=$result["m_ID"];
				echo IMReturnStr::success();
			}else {
				echo IMReturnStr::GetInfo(false,"用户名或者密码错误");
			}
		} catch (Exception $e) {
			echo IMReturnStr::failure();
		}
	}
Copy after login

service

<p style="margin-bottom: 7px;">	public static function selectMember(MemberModel $member){<br/>		try {<br/>			return MemberDao::selectMember($member);<br/>		} catch (Exception $e) {<br/>			throw new Exception($e,4);<br/>		}<br/>	}</p>
Copy after login

dao

	public static function selectMember(MemberModel $member){    //这里就是为什么要写类型了,写了类型可以拿到定义的类中的方法,否则虽然也可以直接写,但是没有自动提示,如果用的方法比较多,就很蛋疼了。
		try {
			$sql=$member->find();
			return YIISqlOper::queryRow($sql);
		} catch (Exception $e) {
			throw new Exception($e,4);
		}
	}	
Copy after login

The above is the detailed content of How to use entity classes in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!