/** * 魔法方法,实现对象属性的设置 * * @param string $prop_name * @param mixed $value */ function __set($prop_name, $value) { $meta = self::$_meta[$this->_class_name]; /* @var $meta QDB_ActiveRecord_Meta */ if (!isset($meta->props[$prop_name])) { throw new QDB_ActiveRecord_UndefinedPropException($this->_class_name, $prop_name); } $config = $meta->props[$prop_name]; if ($config['readonly']) { throw new QDB_ActiveRecord_SettingReadonlyPropException($this->_class_name, $prop_name); } if (!empty($config['setter'])) { // 如果指定了属性的 setter,则通过 setter 方法来修改属性值 $callback = $config['setter']; if (!is_array($callback[0])) { $this->{$callback[0]}($value); return; } else { call_user_func($callback[0], $this, $value); } // 修改属性的脏状态 $this->_changed_props[$prop_name] = $prop_name; return; } if ($config['assoc']) { // 在指定关联对象时,要进行类型检查 if ($config['assoc'] == QDB::HAS_ONE || $config['assoc'] == QDB::BELONGS_TO) { if ($value instanceof $config['assoc_class']) { $this->_props[$prop_name] = $value; } else { throw new QDB_ActiveRecord_SettingPropTypeMismatch($this->_class_name, $prop_name, $config['assoc_class'], gettype($value)); } } else { if (is_array($value)) { $this->_props[$prop_name] = QColl::createFromArray($value, $config['assoc_class']); } elseif ($value instanceof Iterator) { $this->_props[$prop_name] = $value; } else { throw new QDB_ActiveRecord_SettingPropTypeMismatch($this->_class_name, $prop_name, 'Iterator', gettype($value)); } } $this->_changed_props[$prop_name] = $prop_name; } elseif ($this->_props[$prop_name] !== $value) { $this->_props[$prop_name] = self::_typed($value, $config['ptype']); $this->_changed_props[$prop_name] = $prop_name; } }
/** * 魔法方法,实现对象属性的设置 * * @param string $prop_name * @param mixed $value */ function __set($prop_name, $value) { $meta = self::$_meta[$this->_class_name]; /* @var $meta QDB_ActiveRecord_Meta */ if (!isset($meta->props[$prop_name])) { throw new QDB_ActiveRecord_UndefinedPropException($this->_class_name, $prop_name); } $config = $meta->props[$prop_name]; if ($config['readonly']) { throw new QDB_ActiveRecord_ChangingReadonlyPropException($this->_class_name, $prop_name); } if (!empty($config['setter'])) { // 如果指定了属性的 setter,则通过 setter 方法来修改属性值 list($callback, $custom_parameters) = $config['setter']; if (!is_array($callback)) { $callback = array($this, $callback); $args = array($value, $prop_name, $custom_parameters, &$this->_props); } else { $args = array($this, $value, $prop_name, $custom_parameters, &$this->_props); } return call_user_func_array($callback, $args); } if ($config['assoc']) { // 在指定关联对象时,要进行类型检查 if ($config['assoc'] == QDB::HAS_ONE || $config['assoc'] == QDB::BELONGS_TO) { if ($value instanceof $config['assoc_class']) { $this->_props[$prop_name] = $value; if ($config['assoc'] == QDB::BELONGS_TO) { $assoc = $meta->assoc($prop_name); $this->_props[$assoc->source_key] = $value[$assoc->target_key]; $this->_changed_props[$assoc->source_key] = $assoc->source_key; } } else { throw new QDB_ActiveRecord_SettingPropTypeMismatchException($this->_class_name, $prop_name, $config['assoc_class'], gettype($value)); } } else { if (is_array($value)) { $this->_props[$prop_name] = QColl::createFromArray($value, $config['assoc_class']); } elseif ($value instanceof Iterator) { $this->_props[$prop_name] = $value; } else { throw new QDB_ActiveRecord_SettingPropTypeMismatchException($this->_class_name, $prop_name, 'Iterator', gettype($value)); } } $this->_changed_props[$prop_name] = $prop_name; } elseif ($this->_props[$prop_name] !== $value) { $this->_props[$prop_name] = self::_typed($value, $config['ptype']); $this->_changed_props[$prop_name] = $prop_name; } }
/** * @dataProvider arrProvider */ function testCreateFromArray($data) { $arr = array(); foreach ($data as $item) { $arr[] = new MyItem($item['index']); } $coll = QColl::createFromArray($arr, 'MyItem'); $this->assertEquals(count($data), count($coll)); $this->assertEquals($data, $coll->toArray()); }
/** * 以对象方式返回数据 * 如果设置了return_first为true,直接返回单个对象,否则返回对象集合 * 更多讨论,参看:http://qeephp.com/bbs/thread-7551-1-1.html * * @param string $class_name * @param boolean $return_first * @access public * @return mixed */ function fetchObject($class_name, $return_first = false) { $objs = array(); $is_ar = is_subclass_of($class_name, 'QDB_ActiveRecord_Abstract'); while ($row = $this->fetchRow()) { $obj = $is_ar ? new $class_name($row, QDB::FIELD, true) : new $class_name($row); if ($return_first) { return $obj; } $objs[] = $obj; } return QColl::createFromArray($objs, $class_name); }