Beispiel #1
0
 public function getModelInstance()
 {
     if (!$this->setup) {
         $this->runSetup();
         $this->setup = true;
     }
     return InjectionContainer::resolve(Nibii::getClassName($this->options['model'], $this->type));
 }
Beispiel #2
0
 public function __construct(DriverAdapter $adapter)
 {
     $this->table = Nibii::getModelTable($this);
     $this->adapter = $adapter;
     foreach ($this->behaviours as $behaviour) {
         $behaviourInstance = $this->getComponentInstance($behaviour);
         $behaviourInstance->setModel($this);
     }
 }
 public function runSetup()
 {
     $model = InjectionContainer::resolve(Nibii::getClassName($this->options['model'], self::BELONGS_TO));
     if ($this->options['foreign_key'] == null) {
         $this->options['foreign_key'] = $model->getDescription()->getPrimaryKey()[0];
     }
     if ($this->options['local_key'] == null) {
         $this->options['local_key'] = Text::singularize($model->getTable()) . '_id';
     }
 }
Beispiel #4
0
 public function getJunctionClassName($classA, $classB)
 {
     $classBParts = explode('\\', substr(nibii\Nibii::getClassName($classB), 1));
     $classAParts = explode('\\', $classA);
     $joinerParts = [];
     foreach ($classAParts as $i => $part) {
         if ($part == $classBParts[$i]) {
             $joinerParts[] = $part;
         } else {
             break;
         }
     }
     $class = [end($classAParts), end($classBParts)];
     sort($class);
     $joinerParts[] = implode('', $class);
     return implode('\\', $joinerParts);
 }
 public function runSetup()
 {
     if (isset($this->options['through'])) {
         $junctionModelName = $this->options['through'];
     } else {
         $junctionModelName = Nibii::joinModels($this->setupName, $this->options['model']);
     }
     $this->options['junction_model'] = $junctionModelName;
     $foreignModel = Nibii::load($this->options['model']);
     if ($this->options['foreign_key'] == null) {
         $this->options['foreign_key'] = $foreignModel->getDescription()->getPrimaryKey()[0];
     }
     if ($this->options['local_key'] == null) {
         $this->options['local_key'] = $this->setupPrimaryKey[0];
     }
     if (!isset($this->options['junction_local_key'])) {
         $this->options['junction_local_key'] = Text::singularize($this->setupTable) . '_id';
     }
     if (!isset($this->options['junction_foreign_key'])) {
         $this->options['junction_foreign_key'] = Text::singularize($foreignModel->getTable()) . '_id';
     }
 }
Beispiel #6
0
 /**
  * 
  * @param RecordWrapper $model
  */
 public function __construct($model)
 {
     $this->table = $model->getTable();
     $this->name = Nibii::getModelName((new \ReflectionClass($model))->getName());
     $relationships = $model->getRelationships();
     $adapter = DriverAdapter::getDefaultInstance();
     $schema = Db::getDriver()->describeTable($this->table)[$this->table];
     $this->autoPrimaryKey = $schema['auto_increment'];
     foreach ($schema['columns'] as $field => $details) {
         $this->fields[$field] = ['type' => $adapter->mapDataTypes($details['type']), 'required' => !$details['nulls'], 'default' => $details['default'], 'name' => $field];
         if (isset($details['default'])) {
             $this->fields[$field]['default'] = $details['default'];
         }
         if (isset($details['length'])) {
             $this->fields[$field]['length'] = $details['length'];
         }
     }
     $this->appendConstraints($schema['primary_key'], $this->primaryKey, true);
     $this->appendConstraints($schema['unique_keys'], $this->uniqueKeys);
     foreach ($relationships as $type => $relations) {
         $this->createRelationships($type, $relations);
     }
 }
Beispiel #7
0
 public function testTableResolution()
 {
     $users = Nibii::load(Users::class);
     $description = $users->getDescription();
     $this->assertInstanceOf('\\ntentan\\nibii\\ModelDescription', $description);
 }
Beispiel #8
0
 public static function load($name)
 {
     return nibii\Nibii::load($name);
 }