A point of PHP: mysqli_result::fetch_object() and ORM

来源:互联网 发布:知乎数据库设计 编辑:程序博客网 时间:2024/06/04 17:45

After using Hibernet in Java, I like the convenience  brought by it.

But in PHP:

mysqli_result::fetch_object() is a good and great function. It can complete Relation's turing into Object.

For example:

A table named Machine in database. It consists of "MACHINE_ID" and "MACHINE_NAME". 

$query = "selete * from method where METHOD_NAME'".$method_name."'";

$result = $mysqli->query($query);

if($obj = $result->fetch_object()){
   $this->machine->machine_id = $obj->MACHINE_ID;
   $this->machine->machine_name = $obj->MACHINE_NAME;
   return true;
  }
  else
   return false;
  }

 

As naming "machine_id" and "machine_name" of Machine object's properties, we have to write two lines:

   $this->machine->machine_id = $obj->MACHINE_ID;
   $this->machine->machine_name = $obj->MACHINE_NAME;

 

but if we name "MACHINE_ID" and "MACHINE_NAME" in Method object(Method.class), the two lines will disappear.

 

Another way, we change table "machine"'s fields into "machine_id" and "machine_name". That means make them to be same as Machine object's properties. Then the two lines will also disappear.

 

Machine object means abstrated conception class named Machine.class.php corresponding to table "machine" in DB.

 

I like and recommend the "Another way" or just do as the original codes. Never use the way in red colored as capitalization always means constant.

原创粉丝点击