Reflection on data Object Mapping mode of ZF framework

Source: Internet
Author: User

Having recently learned the ZF framework, the introduction to the Section Zend_db_table_row in the Handbook, "Zend_db_table::find () or:: FindRow () method can return a Zend_db_table_row object, The object is automatically mapped to this row of records in the table, fields and class properties are fully associative (field names are underlined, property names are named by the first lowercase camel, such as automatic "last_name", the property name "LastName" is manipulated), and any field is directly like the Action class property Such as:

$row = $table->fetchrow (' first_name = "Robin");//$table is a zend_db_table instance
echo $row->lastname;//assume output ' Bill '

And the modified operation is super convenient, directly to the property assigned to the Save () method can be called, such as:

$row->lastname = ' Gates '; $row->save ();

The data is automatically updated to the table. After a deep dive into the next source, roughly or logically a bit more, so their own rationale, here put a simple integration of the idea of the class, according to the data object mapping mode to think, can help to understand.

<?php/** * Data Object mapping mode *class datamapping{protected $id;    protected $data;    protected $db;    protected $change = false;        function __construct ($id) {$this->db = Factory::getdatabase ();        $res = $this->db->query ("select * from user where id = $id limit 1");        $this->data = $res->fetch_assoc ();    $this->id = $id; } function __get ($key)//Implement object manipulation properties to manipulate the table field value {if (Isset ($this->data[$key])) {return $t        his->data[$key];        }} function __set ($key, $value)//For the new field can be updated directly into the data table {$this->data[$key] = $value;     $this->change = true; } function __destruct ()//destructor implementation Save () logic {if ($this->change) {foreach ($this->da            Ta as $k + $v) {$fields [] = "$k = ' {$v} '"; } $this->db->query ("Update user set".) Implode (', ', $fields).     "Where id = {$this->id} limit 1");   }    }} 

  

Reflection on data Object Mapping mode of ZF framework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.