Ejemplo n.º 1
0
 public function __construct($settings = false, $connectionName = 'default')
 {
     $this->_connectionName = $connectionName;
     if ($settings) {
         parent::__construct($settings, $connectionName);
     }
 }
Ejemplo n.º 2
0
 public static function getInstance($model)
 {
     $initArgs = func_get_args();
     array_shift($initArgs);
     if (!isset(self::$_instance)) {
         throw new \Exception('\\Simpletools\\Mvc\\Model: There are no settings provided.');
     }
     if (!isset($model) or $model == '') {
         throw new \Exception('\\Simpletools\\Mvc\\Model: Model name required.');
     }
     //namespacing
     //$model = str_replace('/','\\',$model);
     $namespace = self::$_instance->_activeRoutingNamespace;
     $orgModel = $model;
     $n = substr($model, 0, 1);
     if ($n == '\\' or $n == '/') {
         $model = trim(str_replace('/', '\\', $model), '\\');
         $_path = explode('\\', $model);
         $model = array_pop($_path);
         $namespace = implode('\\', $_path);
     }
     $class = !$namespace ? $model . 'Model' : $namespace . "\\" . $model . 'Model';
     if (!isset(self::$_instance->objects[$class])) {
         if (!class_exists($class)) {
             $p = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . '/' . $model . 'Model.php';
             $p = realpath(self::$_instance->_appDir . '/models/' . $p);
             if ($p === false) {
                 $p = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . '/' . $model . '.php';
                 $p = realpath(self::$_instance->_appDir . '/models/' . $p);
                 if ($p === false) {
                     throw new \Exception('SimpleModel: Couldn\'t find ' . $model . ' model in ' . self::$_instance->_appDir . '/models/');
                 }
             }
             require $p;
         }
         $obj = new $class();
         if ($obj instanceof \Simpletools\Db\Mysql\Model) {
             $connectionName = $obj->getConnectionName();
             unset($obj);
             $obj = \Simpletools\Db\Mysql\Client::getInstance($connectionName);
             self::$_instance->objects[$class] = $obj->getInstanceOfModel($model, $initArgs, $namespace);
         } else {
             if (is_callable(array($obj, 'init'))) {
                 call_user_func_array(array($obj, 'init'), $initArgs);
             }
             self::$_instance->objects[$class] = $obj;
         }
         if (is_callable(array(self::$_instance->objects[$class], 'setActiveRoutingNamespace'))) {
             self::$_instance->objects[$class]->setActiveRoutingNamespace(self::$_instance->_activeRoutingNamespace);
         }
     }
     return self::$_instance->objects[$class];
 }