/** * @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; } }