/** * @param \Enjoin\Record\Record $Record * @param array $collection * @param array|null $params * @return \Enjoin\Record\Record */ public static function update(Record $Record, array $collection, array $params = null) { if (isset($params['fields'])) { $collection = Extras::pick($collection, $params['fields']); } foreach ($collection as $field => $value) { $Record->{$field} = $value; } $flags = $Record->scope()->type === self::NON_PERSISTENT ? self::SOFT_SAVE : 0; return static::save($Record, $params, $flags); }
/** * @depends testMockDataB */ public function testModelFindAllEagerOneThenManyMeanGrouped() { $this->handleDebug(__FUNCTION__); $params = $this->getCompareParams(__FUNCTION__); $sql = Enjoin::get('Books')->findAll($params, Enjoin::SQL); $this->assertEquals($this->getCompareSql(__FUNCTION__), $sql); $r = Enjoin::get('Books')->findAll($params); $book = Extras::findWhere($r, ['id' => 1]); $article = Extras::findWhere($book->author->articles, ['id' => 3]); $this->assertEquals(1928, $article->year); }
/** * @param stdClass $parent * @param stdClass $child */ private function handleRelation(stdClass $parent, stdClass $child) { $where = ['relatedKey' => $child->key]; if ($child->as) { $where['as'] = $child->as; } $relation = Extras::findWhere($parent->Model->getDefinition()->getRelations(), $where); $missedErr = "Unable to find relation between '{$parent->Model->getUnique()}' " . "and '{$child->Model->getUnique()}', foreign key: '{$child->key}'."; $relation ?: Error::dropModelException($missedErr); $child->relation = $relation; $child->foreignKey = $relation->foreignKey; if ($relation->type === Extras::HAS_MANY) { $this->hasMany = true; } # Handle child `as`: if (!$child->as) { if ($relation->type === Extras::HAS_ONE || $relation->type === Extras::BELONGS_TO) { # On `hasOne`, `belongsTo`: $child->as = Inflector::singularize($child->Model->getTableName()); } else { # On `hasMany`: $child->as = Inflector::pluralize($child->Model->getTableName()); } $child->asProp = Inflector::camelize($child->as); } else { $child->asProp = $child->as; } # Handle child `prefix`: if (!$child->prefix) { $child->prefix = $parent->prefix ? $parent->prefix . Extras::GLUE_CHAR . $child->as : $child->as; } }
/** * @param array|object|null $collection * @param array|null $params * @return \Enjoin\Record\Record */ public function create($collection = null, array $params = null) { if (isset($params['fields'])) { $collection = Extras::pick($collection, $params['fields']); } $this->cache()->flush(); return $this->build($collection)->save(); }