ActiveRecord.php

浏览该文件的文档。
00001 <?php
00003 // FleaPHP Framework
00004 //
00005 // Copyright (c) 2005 - 2008 QeeYuan China Inc. (http://www.qeeyuan.com)
00006 //
00007 // 许可协议,请查看源代码中附带的 LICENSE.txt 文件,
00008 // 或者访问 http://www.fleaphp.org/ 获得详细信息。
00010 
00028 class FLEA_Db_ActiveRecord
00029 {
00035     var $_aggregation = array();
00036 
00042     var $_table;
00043 
00049     var $_idname;
00050 
00056     var $init = false;
00057 
00063     var $_mapping = false;
00064 
00072     function define()
00073     {
00074     }
00075 
00085     function FLEA_Db_ActiveRecord($conditions = null)
00086     {
00087         $this->init();
00088         $this->load($conditions);
00089     }
00090 
00096     function init()
00097     {
00098         if ($this->init) { return; }
00099         $this->init = true;
00100 
00101         $myclass = get_class($this);
00102         $options = call_user_func(array($myclass, 'define'));
00103         $tableClass = $options['tableClass'];
00104 
00105         $objid = "{$myclass}_tdg";
00106         if (FLEA::isRegistered($objid)) {
00107             $this->_table =& FLEA::registry($objid);
00108         } else {
00109             FLEA::loadClass($tableClass);
00110             $this->_table =& new $tableClass(array('skipCreateLinks' => true));
00111             FLEA::register($this->_table, $objid);
00112         }
00113 
00114         if (!empty($options['propertiesMapping'])) {
00115             $this->_mapping = array(
00116                 'p2f' => $options['propertiesMapping'],
00117                 'f2p' => array_flip($options['propertiesMapping']),
00118             );
00119             $this->_idname = $this->_mapping['f2p'][$this->_table->primaryKey];
00120         } else {
00121             $this->_mapping = array('p2f' => array(), 'f2p' => array());
00122             foreach ($this->_table->meta as $field) {
00123                 $this->_mapping['p2f'][$field['name']] = $field['name'];
00124                 $this->_mapping['f2p'][$field['name']] = $field['name'];
00125             }
00126             $this->_idname = $this->_table->primaryKey;
00127         }
00128 
00129         if (!isset($options['aggregation']) || !is_array($options['aggregation'])) {
00130             $options['aggregation'] = array();
00131         }
00132         foreach ($options['aggregation'] as $offset => $define) {
00133             if (!isset($define['mappingName'])) {
00134                 $define['mappingName'] = substr(strtolower($define['tableClass']), 0, 1) . substr($define['tableClass'], 1);
00135             }
00136             if ($define['mappingType'] == HAS_MANY || $define['mappingType'] == MANY_TO_MANY) {
00137                 $this->{$define['mappingName']} = array();
00138             } else {
00139                 $this->{$define['mappingName']} = null;
00140             }
00141 
00145             FLEA::loadClass($define['class']);
00146             $options = call_user_func(array($define['class'], 'define'));
00147 
00148             $link = array(
00149                 'tableClass' => $options['tableClass'],
00150                 'mappingName' => $define['mappingName'],
00151                 'foreignKey' => isset($define['foreignKey']) ? $define['foreignKey'] : null,
00152             );
00153 
00154             if ($define['mappingType'] == MANY_TO_MANY) {
00155                 $link['joinTable'] = isset($define['joinTable']) ? $define['joinTable'] : null;
00156                 $link['assocForeignKey'] = isset($define['assocForeignKey']) ? $define['assocForeignKey'] : null;
00157             }
00158 
00159             $this->_table->createLink($link, $define['mappingType']);
00160             $define['link'] =& $this->_table->getLink($link['mappingName']);
00161             $this->_aggregation[$offset] = $define;
00162         }
00163     }
00164 
00170     function load($conditions)
00171     {
00172         $row = $this->_table->find($conditions);
00173         if (is_array($row)) { $this->attach($row); }
00174     }
00175 
00179     function save()
00180     {
00181         $row =& $this->toArray();
00182         $this->_table->save($row);
00183     }
00184 
00188     function delete()
00189     {
00190         $this->_table->removeByPkv($this->getId());
00191     }
00192 
00198     function setId($id)
00199     {
00200         $this->{$this->_idname} = $id;
00201     }
00202 
00208     function getId()
00209     {
00210         return $this->{$this->_idname};
00211     }
00212 
00218     function toArray()
00219     {
00220         $arr = array();
00221         foreach ($this->_mapping['p2f'] as $prop => $field) {
00222             $arr[$field] = $this->{$prop};
00223         }
00224         return $arr;
00225     }
00226 
00232     function attach(& $row)
00233     {
00234         foreach ($this->_mapping['f2p'] as $field => $prop) {
00235             if (isset($row[$field])) {
00236                 $this->{$prop} = $row[$field];
00237             }
00238         }
00239 
00240         foreach ($this->_aggregation as $define) {
00241             $mn = $define['link']->mappingName;
00242             if (!isset($row[$mn])) { continue; }
00243             if ($define['link']->oneToOne) {
00244                 $this->{$mn} =& new $define['class']($row[$mn]);
00245             } else {
00246                 $this->{$mn}[] =& new $define['class']($row[$mn]);
00247             }
00248         }
00249     }
00250 
00251 }

Generated at Sat Feb 2 15:18:50 2008 for FleaPHP by  doxygen 1.5.3