/** * @param EloquentModel $model * @param Relation $relation * @return string */ protected function createMethodBody(EloquentModel $model, Relation $relation) { $reflectionObject = new \ReflectionObject($relation); $name = Str::camel($reflectionObject->getShortName()); $arguments = [$model->getNamespace()->getNamespace() . '\\' . Str::singular(Str::studly($relation->getTableName()))]; if ($relation instanceof BelongsToMany) { $defaultJoinTableName = $this->helper->getDefaultJoinTableName($model->getTableName(), $relation->getTableName()); $joinTableName = $relation->getJoinTable() === $defaultJoinTableName ? null : $relation->getJoinTable(); $arguments[] = $joinTableName; $arguments[] = $this->resolveArgument($relation->getForeignColumnName(), $this->helper->getDefaultForeignColumnName($model->getTableName())); $arguments[] = $this->resolveArgument($relation->getLocalColumnName(), $this->helper->getDefaultForeignColumnName($relation->getTableName())); } elseif ($relation instanceof HasMany) { $arguments[] = $this->resolveArgument($relation->getForeignColumnName(), $this->helper->getDefaultForeignColumnName($model->getTableName())); $arguments[] = $this->resolveArgument($relation->getLocalColumnName(), EmgHelper::DEFAULT_PRIMARY_KEY); } else { $arguments[] = $this->resolveArgument($relation->getForeignColumnName(), $this->helper->getDefaultForeignColumnName($relation->getTableName())); $arguments[] = $this->resolveArgument($relation->getLocalColumnName(), EmgHelper::DEFAULT_PRIMARY_KEY); } return sprintf('return $this->%s(%s);', $name, $this->prepareArguments($arguments)); }
/** * @param Relation $relation * @return string * @throws GeneratorException */ protected function createMethodBody(Relation $relation) { $reflectionObject = new \ReflectionObject($relation); $name = Str::camel($reflectionObject->getShortName()); $arguments = [Str::studly($relation->getTableName())]; if ($relation instanceof BelongsToMany) { $defaultJoinTableName = TitleHelper::getDefaultJoinTableName($this->tableName, $relation->getTableName()); $joinTableName = $relation->getJoinTable() === $defaultJoinTableName ? null : $relation->getJoinTable(); $arguments[] = $joinTableName; $arguments[] = $this->resolveArgument($relation->getForeignColumnName(), TitleHelper::getDefaultForeignColumnName($this->getTableName())); $arguments[] = $this->resolveArgument($relation->getLocalColumnName(), TitleHelper::getDefaultForeignColumnName($relation->getTableName())); } elseif ($relation instanceof HasMany) { $arguments[] = $this->resolveArgument($relation->getForeignColumnName(), TitleHelper::getDefaultForeignColumnName($this->getTableName())); $arguments[] = $this->resolveArgument($relation->getLocalColumnName(), TitleHelper::$defaultPrimaryKey); } else { $arguments[] = $this->resolveArgument($relation->getForeignColumnName(), TitleHelper::getDefaultForeignColumnName($relation->getTableName())); $arguments[] = $this->resolveArgument($relation->getLocalColumnName(), TitleHelper::$defaultPrimaryKey); } return sprintf('return $this->%s(%s);', $name, $this->prepareArguments($arguments)); }