[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/active_record/src/ -> lmbARMetaInfo.class.php (source)

   1  <?php
   2  /*

   3   * Limb PHP Framework

   4   *

   5   * @link http://limb-project.com

   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)

   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html

   8   */
   9  
  10  /**

  11   * class lmbARMetaInfo.

  12   *

  13   * @package active_record

  14   * @version $Id: lmbARMetaInfo.class.php 5997 2007-06-18 12:27:21Z pachanga $

  15   */
  16  class lmbARMetaInfo
  17  {
  18    protected $db_table = null;
  19    protected $db_column_names = array();
  20    protected $get_method_fields = array();
  21    protected $set_method_fields = array();
  22    protected $cast_methods = array();
  23  
  24    function __construct($active_record, $conn = null)
  25    {
  26      if(!$table_name = $active_record->getTableName())
  27        $table_name = lmb_under_scores(get_class($active_record));
  28  
  29      $this->db_table = lmbToolkit :: instance()->createTableGateway($table_name, $conn);
  30      $this->db_column_names = $this->db_table->getColumnNames();
  31    }
  32  
  33    function getDbTable()
  34    {
  35      return $this->db_table;
  36    }
  37  
  38    function getDbColumnsNames()
  39    {
  40      return $this->db_column_names;
  41    }
  42  
  43    function hasColumn($name)
  44    {
  45      return isset($this->db_column_names[$name]);
  46    }
  47  
  48    function castDbValues($record)
  49    {
  50      $this->_loadCastMethods($record);
  51  
  52      $result = array();
  53      foreach($record->export() as $key => $value)
  54      {
  55        if(isset($this->cast_methods[$key]))
  56        {
  57          $method = $this->cast_methods[$key];
  58          if(method_exists($record, $method))
  59            $result[$key] = $record->$method($key);
  60          else
  61            $result[$key] = $value;
  62        }
  63        else
  64          $result[$key] = $value;
  65      }
  66  
  67      return $result;
  68    }
  69  
  70    protected function _loadCastMethods($record)
  71    {
  72      if(sizeof($this->cast_methods))
  73        return;
  74  
  75      static $accessors;
  76  
  77      if(!isset($accessors))
  78      {
  79        $typeinfo = new lmbDbTypeInfo();
  80        $accessors = $typeinfo->getColumnTypeGetters();
  81      }
  82  
  83      foreach($this->db_column_names as $name)
  84      {
  85        if($info = $this->db_table->getColumnInfo($name))
  86          $this->cast_methods[$name] = $accessors[$info->getType()];
  87        else
  88          $this->cast_methods[$name] = '';
  89      }
  90    }
  91  }
  92  
  93  ?>


Generated: Fri Aug 29 04:49:26 2008 Cross-referenced by PHPXref 0.7