Esempio n. 1
0
 public function testMethodColumnWithColumnKeyNotNull()
 {
     $this->assertSame(['foo' => 'bar'], ArrayUtils::column(['foo' => 'bar'], null));
     $result = ArrayUtils::column([['name' => 'foo'], ['id' => 10, 'name' => 'bar'], ['foo' => 'baz']], 'name', 'id');
     $expected = ['foo', 10 => 'bar'];
     $this->assertSame($expected, $result);
 }
Esempio n. 2
0
 /**
  * @param $name
  * @return null|\Sloths\Db\Model\AbstractModel
  */
 public function getBelongsTo($name)
 {
     $schema = $this->getBelongsToSchema($name);
     $foreignKeyColumn = $schema->getForeignKey();
     $model = $schema->getModel();
     $primaryKeyColumn = $model->getPrimaryKey();
     $tableName = $model->getTableName();
     if ($parentCollection = $this->getParentCollection()) {
         $modelClassName = $schema->getModelClassName();
         $foreignKeyIds = $parentCollection->column($foreignKeyColumn);
         $foreignKeyIds = array_unique($foreignKeyIds);
         $foreignKeyIds = array_diff($foreignKeyIds, [null]);
         $select = $model->table()->select()->where($tableName . '.' . $primaryKeyColumn . ' IN (' . implode(', ', $foreignKeyIds) . ')');
         $rows = $select->all();
         $pairs = ArrayUtils::column($rows, null, $primaryKeyColumn);
         foreach ($pairs as &$item) {
             $item = new $modelClassName($item);
         }
         foreach ($parentCollection as $model) {
             $foreignKeyValue = $model->get($foreignKeyColumn);
             if (isset($pairs[$foreignKeyValue])) {
                 $model->relationData[$name] = $pairs[$foreignKeyValue];
             } else {
                 $model->relationData[$name] = null;
             }
         }
         $selfForeignKeyValue = $this->get($foreignKeyColumn);
         return isset($pairs[$selfForeignKeyValue]) ? $pairs[$selfForeignKeyValue] : null;
     } else {
         return $model->first($tableName . '.' . $primaryKeyColumn . ' = ' . $this->get($foreignKeyColumn));
     }
 }
Esempio n. 3
0
 /**
  * @param $name
  * @return null|\Sloths\Db\Model\AbstractModel
  */
 public function getHasOne($name)
 {
     $schema = $this->getHasOneSchema($name);
     $foreignKeyColumn = $schema->getForeignKey();
     $model = $schema->getModel();
     $tableName = $model->getTableName();
     if ($parentCollection = $this->getParentCollection()) {
         $modelClassName = $schema->getModelClassName();
         $ids = $parentCollection->ids();
         $select = $model->table()->select()->where($tableName . '.' . $foreignKeyColumn . ' IN (' . implode(', ', $ids) . ')');
         $rows = $select->all();
         $pairs = ArrayUtils::column($rows, null, $foreignKeyColumn);
         foreach ($pairs as &$item) {
             $item = new $modelClassName($item);
         }
         foreach ($parentCollection as $model) {
             $id = $model->id();
             if (isset($pairs[$id])) {
                 $model->relationData[$name] = $pairs[$id];
             } else {
                 $model->relationData[$name] = null;
             }
         }
         $selfId = $this->id();
         return isset($pairs[$selfId]) ? $pairs[$selfId] : null;
     } else {
         return $model->first($tableName . '.' . $foreignKeyColumn . ' = ' . $this->id());
     }
 }
Esempio n. 4
0
 /**
  * @param string $name
  * @param string $columnKey
  * @return array
  */
 public function column($name, $columnKey = null)
 {
     return ArrayUtils::column($this->toArray(), $name, $columnKey);
 }
Esempio n. 5
0
 /**
  * @param $name
  * @return mixed
  * @throws \LogicException
  * @throws \InvalidArgumentException
  */
 public function loadColumn($name)
 {
     if (!$this->hasColumn($name)) {
         throw new \InvalidArgumentException('Model has no column ' . $name);
     }
     if (!$this->exists()) {
         throw new \LogicException('Cannot load column from non existing record');
     }
     if ($parentCollection = $this->getParentCollection()) {
         $primaryKeyColumn = $this->getPrimaryKey();
         $ids = $parentCollection->ids();
         $select = $this->table()->select([$primaryKeyColumn, $name]);
         $select->where($primaryKeyColumn . ' IN (' . implode(', ', $ids) . ')');
         $rows = $select->all();
         $pairs = ArrayUtils::column($rows, $name, $primaryKeyColumn);
         foreach ($parentCollection as $model) {
             $id = $model->id();
             if (isset($pairs[$id])) {
                 $model->data[$name] = $pairs[$id];
             } else {
                 $model->data[$name] = null;
             }
         }
         return $pairs[$this->id()];
     } else {
         $select = $this->table()->select($name);
         $select->where($this->getPrimaryKey() . ' = ' . $this->id());
         $row = $select->first();
         $value = $row[$name];
         $this->data[$name] = $value;
         return $value;
     }
 }