Ejemplo n.º 1
0
 public function __construct($mdlName, $projection = null)
 {
     $this->model = is_model($mdlName) ? $mdlName : MODEL($mdlName);
     if ($projection !== null) {
         $this->p($projection);
     }
 }
Ejemplo n.º 2
0
 public function __construct()
 {
     if ($this->modelName === "") {
         $this->modelName = substr($this->getName(), 8);
     }
     $this->model = MODEL($this->modelName);
 }
Ejemplo n.º 3
0
 public static function build(Sabel_Db_Model $source, Sabel_Db_Join_Structure $structure, $rows)
 {
     $objects = $structure->getJoinObjects();
     $structure = $structure->getStructure();
     $tables = array();
     foreach ($structure as $joinTables) {
         $tables = array_merge($tables, $joinTables);
     }
     $results = array();
     $selfObj = MODEL($source->getName());
     foreach ($rows as $row) {
         $models = self::createModels($row, $tables, $objects);
         foreach ($tables as $tblName) {
             if (!isset($structure[$tblName])) {
                 continue;
             }
             foreach ($structure[$tblName] as $parent) {
                 $name = convert_to_modelname($parent);
                 $models[$tblName]->__set($name, $models[$parent]);
             }
         }
         $self = clone $selfObj;
         $self->setProperties($row);
         $tblName = $source->getTableName();
         foreach ($structure[$tblName] as $parent) {
             $name = convert_to_modelname($parent);
             $self->__set($name, $models[$parent]);
         }
         $results[] = $self;
     }
     return $results;
 }
Ejemplo n.º 4
0
 public function __construct($model)
 {
     if (is_string($model)) {
         $model = MODEL($model);
     } elseif (!is_model($model)) {
         $message = __METHOD__ . "() argument must be a string or an instance of model.";
         throw new Sabel_Exception_InvalidArgument($message);
     }
     $this->model = $model;
     $this->tblName = $model->getTableName();
     $this->structure = Sabel_Db_Join_Structure::getInstance();
 }
Ejemplo n.º 5
0
 public function setModel($model)
 {
     if (is_string($model)) {
         $this->model = MODEL($model);
     } elseif ($model instanceof Sabel_Db_Model) {
         $this->model = $model;
     } else {
         $message = __METHOD__ . "() argument must be a string or an instance of model.";
         throw new Sabel_Exception_Runtime();
     }
     $this->values = $this->model->toArray();
 }
Ejemplo n.º 6
0
 public function __construct($config = array())
 {
     $this->config = $config;
     if (isset($this->config["namespace"])) {
         $this->namespace = $this->config["namespace"];
     } else {
         $this->namespace = self::DEFUALT_NAMESPACE;
     }
     if (isset($config["model"])) {
         $this->model = MODEL($config["model"]);
     } else {
         $this->model = MODEL("SblPreference");
     }
 }
Ejemplo n.º 7
0
 public function add($object, $alias = "", $joinKey = array())
 {
     if (is_string($object)) {
         $object = new Sabel_Db_Join_Object(MODEL($object), $alias, $joinKey);
     } elseif (is_model($object)) {
         $object = new Sabel_Db_Join_Object($object, $alias, $joinKey);
     }
     $structure = Sabel_Db_Join_Structure::getInstance();
     $structure->addJoinObject($this->getName(), $object);
     $object->setChildName($this->getName());
     $this->objects[] = $object;
     if (empty($joinKey)) {
         $name = $object->getModel()->getTableName();
         $object->setJoinKey(create_join_key($this->model, $name));
     }
     return $this;
 }
Ejemplo n.º 8
0
 public function __construct($model, $pageKey = "page")
 {
     if (is_string($model)) {
         $model = MODEL($model);
     }
     if (is_model($model)) {
         $model->autoReinit(false);
     } elseif ($model instanceof Sabel_Db_Join) {
         $model->getModel()->autoReinit(false);
         $this->isJoin = true;
     } else {
         $message = __METHOD__ . "() invalid instance.";
         throw new Sabel_Exception_Runtime($message);
     }
     $this->model = $model;
     $this->attributes["pageKey"] = $pageKey;
 }
Ejemplo n.º 9
0
 public function __construct($model)
 {
     if (is_string($model)) {
         $model = MODEL($model);
     }
     if (is_model($model)) {
         $model->autoReinit(false);
         $this->method = "select";
     } elseif ($model instanceof Sabel_Db_Join) {
         $model->getModel()->autoReinit(false);
         $this->method = "join";
         $this->isJoin = true;
     } else {
         throw new Exception("Paginate::__construct() invalid instance.");
     }
     $this->model = $model;
 }
Ejemplo n.º 10
0
 public function fetchModel($mdlName, $conditions)
 {
     if (is_array($conditions)) {
         $_model = MODEL($mdlName);
         foreach ($conditions as $column => $value) {
             $_model->setCondition($column, $value);
         }
         $model = $_model->selectOne();
     } else {
         $model = MODEL($mdlName, $conditions);
     }
     if ($model->isSelected()) {
         return $model;
     } else {
         $this->notFound();
         return false;
     }
 }
Ejemplo n.º 11
0
 public function testFloatCast()
 {
     $ts = MODEL("TestSchema");
     $ts->floatcol = 1;
     $this->assertTrue(is_float($ts->floatcol));
     $this->assertEquals(1, $ts->floatcol);
     $ts->floatcol = "1";
     $this->assertTrue(is_float($ts->floatcol));
     $this->assertEquals(1, $ts->floatcol);
     $ts->floatcol = "0.123";
     $this->assertTrue(is_float($ts->floatcol));
     $this->assertEquals(0.123, $ts->floatcol);
     $ts->floatcol = "0.123.456";
     $this->assertFalse(is_float($ts->floatcol));
     $this->assertEquals("0.123.456", $ts->floatcol);
     $ts->floatcol = true;
     $this->assertFalse(is_float($ts->floatcol));
     $this->assertEquals(true, $ts->floatcol);
 }
Ejemplo n.º 12
0
 public function createModel(&$row)
 {
     $name = $this->tblName;
     static $models = array();
     if (isset($models[$name])) {
         $model = clone $models[$name];
     } else {
         $model = MODEL(convert_to_modelname($name));
         $models[$name] = clone $model;
     }
     if ($this->hasAlias()) {
         $name = strtolower($this->aliasName);
     }
     $props = array();
     foreach ($this->columns as $column) {
         $hash = Sabel_Db_Join_ColumnHash::getHash("{$name}.{$column}");
         if (array_key_exists($hash, $row)) {
             $props[$column] = $row[$hash];
             unset($row[$hash]);
         }
     }
     $model->setProperties($props);
     return $model;
 }
Ejemplo n.º 13
0
 protected function export_fixture()
 {
     foreach ($this->arguments as $mdlName) {
         $lines = array();
         $code = array("<?php" . PHP_EOL);
         $code[] = "class Fixture_{$mdlName} extends Sabel_Test_Fixture";
         $code[] = "{";
         $code[] = "  public function upFixture()";
         $code[] = "  {";
         $models = MODEL($mdlName)->select();
         foreach ($models as $model) {
             $code[] = '    $this->insert(' . $this->createLine($model->toArray()) . ');';
         }
         $code[] = "  }" . PHP_EOL;
         $code[] = "  public function downFixture()";
         $code[] = "  {";
         $code[] = '    $this->deleteAll();';
         $code[] = "  }";
         $code[] = "}";
         $path = FIXTURE_DIR . DS . $mdlName . ".php";
         file_put_contents($path, implode(PHP_EOL, $code));
         $this->success("export {$mdlName} Records to '" . substr($path, strlen(RUN_BASE) + 1) . "'");
     }
 }
Ejemplo n.º 14
0
 public function __wakeup()
 {
     l("[form] unserialize form object", SBL_LOG_DEBUG);
     $values = $this->model;
     $this->model = MODEL($this->mdlName);
     if ($this->isSelected) {
         $this->model->setProperties($values);
         $this->model->setUpdateValues($this->updateValues);
     } else {
         $this->model->setValues($values);
     }
     $this->columns = $this->model->getColumns();
 }
Ejemplo n.º 15
0
 public function testInit()
 {
     Sabel_Db_Config::add("default", Test_DB_TestConfig::getIbaseConfig());
     MODEL("SblKvs")->delete();
 }
Ejemplo n.º 16
0
 protected function insertJoinTableData()
 {
     $data = array();
     $data[] = array("id" => 1, "value" => "grandparents1");
     $data[] = array("id" => 2, "value" => "grandparents2");
     $gp = MODEL("Grandparents");
     foreach ($data as $values) {
         $gp->insert($values);
     }
     $data = array();
     $data[] = array("id" => 1, "grandparents_id" => 2, "value" => "parents1");
     $data[] = array("id" => 2, "grandparents_id" => 1, "value" => "parents2");
     $p = MODEL("Parents");
     foreach ($data as $values) {
         $p->insert($values);
     }
     $data = array();
     $data[] = array("id" => 1, "parents_id" => 2, "value" => "children1");
     $data[] = array("id" => 2, "parents_id" => 1, "value" => "children2");
     $c = MODEL("Children");
     foreach ($data as $values) {
         $c->insert($values);
     }
     $data = array();
     $data[] = array("id" => 1, "children_id" => 2, "value" => "grandchildren1");
     $data[] = array("id" => 2, "children_id" => 1, "value" => "grandchildren2");
     $gc = MODEL("Grandchildren");
     foreach ($data as $values) {
         $gc->insert($values);
     }
 }
Ejemplo n.º 17
0
 public function testUseNamespace()
 {
     $pref = Sabel_Preference::create(new __Preference_Database_Config_Namespace());
     $pref->setInt("test", 1);
     $this->assertEquals(1, MODEL("SblPreference")->getCount("namespace", "myapp"));
 }
Ejemplo n.º 18
0
 protected function clear($mdlName)
 {
     MODEL($mdlName)->delete();
 }
Ejemplo n.º 19
0
 /**
  * @param array $rows
  *
  * @return Sabel_Db_Model[]
  */
 protected function toModels(array $rows)
 {
     $results = array();
     $source = MODEL($this->modelName);
     foreach ($rows as $row) {
         $model = clone $source;
         $model->setProperties($row);
         $results[] = $model;
     }
     return $results;
 }
Ejemplo n.º 20
0
 private function __construct($mdlName)
 {
     $this->model = MODEL($mdlName);
 }
Ejemplo n.º 21
0
 protected function getSessionModel($sessionId)
 {
     if (!isset($this->models[$sessionId])) {
         $this->models[$sessionId] = MODEL($this->mdlName, $sessionId);
     }
     return $this->models[$sessionId];
 }