From d239e67f2b88a96544efab4263480b9e814faede Mon Sep 17 00:00:00 2001 From: dedensaka Date: Tue, 28 Feb 2017 00:22:31 +0700 Subject: [PATCH] [FIX] both relation and relation process --- .../OrmManager/BothRelations/BothRelation.php | 138 ++--------- .../OrmManager/BothRelations/ManyToMany.php | 122 ++++++++-- .../BothRelations/MorphManytoMany.php | 213 +++++++++++------ .../BothRelations/MorphOneToMany.php | 12 +- .../BothRelations/MorphOneToOne.php | 222 +++++++++++++----- .../OrmManager/BothRelations/OneToOne.php | 92 ++++++-- .../OrmManager/Consoles/ModelBothConnect.php | 25 +- .../OrmManager/Consoles/ModelConnect.php | 14 +- .../OrmManager/Relations/BelongsTo.php | 2 - .../OrmManager/Relations/BelongsToMany.php | 99 +++----- .../OrmManager/Relations/HasManyThrough.php | 22 +- src/Flipbox/OrmManager/Relations/HasOne.php | 24 +- src/Flipbox/OrmManager/Relations/MorphOne.php | 14 +- src/Flipbox/OrmManager/Relations/MorphTo.php | 43 ++-- .../OrmManager/Relations/MorphToMany.php | 168 +------------ .../OrmManager/Relations/MorphedByMany.php | 163 ++++++++++++- src/Flipbox/OrmManager/Relations/Relation.php | 63 ++--- .../OrmManager/Stubs/belongsToMany.stub | 2 +- 18 files changed, 810 insertions(+), 628 deletions(-) diff --git a/src/Flipbox/OrmManager/BothRelations/BothRelation.php b/src/Flipbox/OrmManager/BothRelations/BothRelation.php index f5032bd..a78f818 100644 --- a/src/Flipbox/OrmManager/BothRelations/BothRelation.php +++ b/src/Flipbox/OrmManager/BothRelations/BothRelation.php @@ -5,145 +5,51 @@ use Illuminate\Console\Command; use Flipbox\OrmManager\ModelManager; use Illuminate\Database\Eloquent\Model; -use Flipbox\OrmManager\DatabaseConnection; +use Flipbox\OrmManager\Relations\Relation; -abstract class BothRelation +abstract class BothRelation extends Relation { /** - * laravel Command - * - * @var Command - */ - protected $command; - - /** - * model manager - * - * @var ModelManager - */ - protected $manager; - - /** - * database connection - * - * @var DatabaseConnection - */ - protected $database; - - /** - * model that want to connect to - * - * @var Model - */ - protected $model; - - /** - * model that will connect with - * - * @var Model - */ - protected $toModel; - - /** - * options + * to models to connect * * @var array */ - protected $options = []; + protected $toModels; /** - * Create a new Both instance. + * Create a new Model instance. * * @param Command $command + * @param ModelManager $manager * @param Model $model - * @param Model $toModel + * @param mixed $toModel + * @param array $options * @return void */ - public function __construct(Command $command, ModelManager $manager, Model $model, Model $toModel) + public function __construct(Command $command, + ModelManager $manager, + Model $model, + $toModel=null, + array $options=[]) { - $this->command = $command; - $this->manager = $manager; - $this->database = new DatabaseConnection; - $this->model = $model; - $this->toModel = $toModel; - - $this->preparation(); + parent::__construct($command, $manager, $model, $toModel, $options); - if ($this->database->isConnected()) { - $this->repositionModelByKeys(); - } + $this->options = array_merge($this->defaultOptions, $this->options); } /** - * preparation oprations - * - * @return void - */ - protected function preparation() {} - - /** - * reposition model by relations key - * - * @return void - */ - protected function repositionModelByKeys() - { - if (! $this->isPositionModelValid()) { - $this->exchangeModelPosition(); - - if (! $this->isPositionModelValid()) { - $this->askWhereForeignKeyTable(); - } - } - } - - /** - * exchange position model - * - * @param - * @return void - */ - protected function exchangeModelPosition() - { - $model = $this->model; - $this->model = $this->toModel; - $this->toModel = $model; - } - - /** - * check is position model is valid - * - * @return bool - */ - protected function isPositionModelValid() - { - return true; - } - - /** - * ask which table where foreign key filed exists + * show captions process * + * @param Model $model + * @param Model $toModel * @return void */ - protected function askWhereForeignKeyTable() {} - - /** - * get model fileds - * - * @param string $table - * @return array - */ - protected function getFields($table) - { - $fileds = $this->database->getTableFields($table); - - return $fileds->pluck('name')->toArray(); - } + protected function showCaptionProcess(Model $model, Model $toModel=null) {} /** - * build relations model to model + * get stub method file * - * @return void + * @return string */ - abstract public function buildRelations(); + protected function getStub() {} } diff --git a/src/Flipbox/OrmManager/BothRelations/ManyToMany.php b/src/Flipbox/OrmManager/BothRelations/ManyToMany.php index c626d3a..08954c6 100644 --- a/src/Flipbox/OrmManager/BothRelations/ManyToMany.php +++ b/src/Flipbox/OrmManager/BothRelations/ManyToMany.php @@ -2,59 +2,133 @@ namespace Flipbox\OrmManager\BothRelations; -use ReflectionClass; use Illuminate\Support\Str; -class ManyToMany extends Both +class ManyToMany extends BothRelation { /** - * reposition model by relations key + * set default options * + * @param array $options * @return void */ - protected function repositionModelByKeys() + protected function setDefaultOptions(array $options=[]) { $tables = [ - Str::singular($this->model->getTable()), - Str::singular($this->toModel->getTable()) + 'model' => $this->model->getTable(), + 'toModel' => $this->toModel->getTable() ]; asort($tables, SORT_REGULAR); + $pivotTable = implode('_', array_map([Str::class, 'singular'], $tables)); + + $this->defaultOptions = [ + 'pivot_table' => $pivotTable, + 'foreign_key' => $this->model->getForeignKey(), + 'related_key' => $this->toModel->getForeignKey(), + 'relation' => $this->toModel->getTable() + ]; + } - $pivotTable = implode('_', $tables); + /** + * styling text + * + * @return void + */ + protected function stylingText() + { + $modelTable = $this->model->getTable(); + $toModelTable = $this->toModel->getTable(); + $pivotTable = $this->defaultOptions['pivot_table']; + $foreignKey = $this->defaultOptions['foreign_key']; + $relatedKey = $this->defaultOptions['related_key']; - if (! $this->database->isTableExists($pivotTable)) { + $this->text = [ + 'table' => "[".$this->command->paintString($modelTable ,'green')."]", + 'to_table' => "[".$this->command->paintString($toModelTable ,'green')."]", + 'pivot_table' => "[".$this->command->paintString($pivotTable ,'green')."]", + 'foreign_key' => "[".$this->command->paintString($foreignKey ,'green')."]", + 'related_key' => "[".$this->command->paintString($relatedKey ,'green')."]", + 'pivot_text' => $this->command->paintString('pivot table', 'brown'), + 'foreign_text' => $this->command->paintString('foreign key', 'brown'), + 'related_text' => $this->command->paintString('related key', 'brown'), + ]; + } + + /** + * get connected db relation options + * + * @return void + */ + protected function setConnectedRelationOptions() + { + $pivotTable = $this->defaultOptions['pivot_table']; + $foreignKey = $this->defaultOptions['foreign_key']; + $relatedKey = $this->defaultOptions['related_key']; + + if (! $this->db->isTableExists($pivotTable)) { + $question = "Can't find table {$this->text['pivot_table']} in the database as {$this->text['pivot_text']}, choice one!"; $pivotTable = $this->options['pivot_table'] = $this->command->choice( - "Can't find table {$pivotTable} in the database as pivot table, choice one!", - $this->database->getTables() - ); + $question, $this->getTables()); + + $this->text['pivot_table'] = "[".$this->command->paintString($pivotTable, 'green')."]"; } - $foreignKey1 = Str::singular(strtolower($this->model->getTable())).'_'.$this->model->getKeyName(); - if (! $this->database->isFieldExists($pivotTable, $foreignKey1)) { - $this->options['foreign_key_1'] = $this->command->choice( - "Can't find field {$foreignKey1} in the table {$pivotTable} as foreign key of table {$table1}, choice one!", - $this->database->getTableFields($pivotTable) - ); + if (! $this->db->isFieldExists($pivotTable, $foreignKey)) { + $question = "Can't find field {$this->text['foreign_key']} in the table {$this->text['pivot_table']} as {$this->text['foreign_text']} of table {$this->text['table']}, choice one!"; + $this->options['foreign_key'] = $this->command->choice($question, $this->getFields($pivotTable)); } - $foreignKey2 = Str::singular(strtolower($this->toModel->getTable())).'_'.$this->toModel->getKeyName(); - if (! $this->database->isFieldExists($pivotTable, $foreignKey2)) { - $this->options['foreign_key_2'] = $this->command->choice( - "Can't find field {$foreignKey2} in the table {$pivotTable} as foreign key of table {$table2}, choice one!", - $this->database->getTableFields($pivotTable) - ); + if (! $this->db->isFieldExists($pivotTable, $relatedKey)) { + $question = "Can't find field {$this->text['related_key']} in the table {$this->text['pivot_table']} as {$this->text['related_text']} of table {$this->text['to_table']}, choice one!"; + $this->options['related_key'] = $this->command->choice($question, $this->getFields($pivotTable)); } } /** - * build relations model to model + * get relation options rules + * + * @return array + */ + protected function getRelationOptionsRules() + { + return [ + "There should be table {$this->text['pivot_table']} in the database as {$this->text['pivot_text']}", + "There should be field {$this->text['foreign_key']} in table {$this->text['pivot_table']} as {$this->text['foreign_text']} of table {$this->text['table']}", + "There should be field {$this->text['related_key']} in table {$this->text['pivot_table']} as {$this->text['related_key']} of table {$this->text['to_table']}" + ]; + } + + /** + * ask to use custome options + * + * @return void + */ + protected function askToUseCustomeOptions() + { + $question = "The {$this->text['pivot_text']} in the database will be?"; + $this->options['pivot_table'] = $this->command->ask($question, $this->defaultOptions['pivot_table']); + $this->text['pivot_table'] = "[".$this->command->paintString($this->options['pivot_table'], 'green')."]"; + + $question = "The {$this->text['foreign_text']} of table {$this->text['table']} in the table {$this->text['pivot_table']}, will be?"; + $this->options['foreign_key'] = $this->command->ask($question, $this->defaultOptions['foreign_key']); + + $question = "The {$this->text['related_text']} of table {$this->text['to_table']} in the table {$this->text['pivot_table']}, will be?"; + $this->options['related_key'] = $this->command->ask($question, $this->defaultOptions['related_key']); + } + + /** + * build relations between models * * @return void */ public function buildRelations() { $this->command->buildMethod($this->model, 'belongsToMany', $this->toModel, $this->options); + + $this->options['foreign_key'] = $this->options['related_key']; + $this->options['related_key'] = $this->options['foreign_key']; + $this->options['relation'] = $this->model->getTable(); $this->command->buildMethod($this->toModel, 'belongsToMany', $this->model, $this->options); } } diff --git a/src/Flipbox/OrmManager/BothRelations/MorphManytoMany.php b/src/Flipbox/OrmManager/BothRelations/MorphManytoMany.php index ae2d2b0..fb1c22a 100644 --- a/src/Flipbox/OrmManager/BothRelations/MorphManytoMany.php +++ b/src/Flipbox/OrmManager/BothRelations/MorphManytoMany.php @@ -4,137 +4,196 @@ use ReflectionClass; use Illuminate\Support\Str; +use Illuminate\Console\Command; +use Flipbox\OrmManager\ModelManager; +use Illuminate\Database\Eloquent\Model; -class MorphManytoMany extends Both +class MorphManytoMany extends BothRelation { /** - * name + * method suffix * * @var string */ - protected $name; - - /** - * table - * - * @var string - */ - protected $table; - - /** - * to models - * - * @var array - */ - protected $toModels = []; - + protected $nameSuffix = 'able'; + /** - * preparation oprations + * Create a new Model instance. * - * @param + * @param Command $command + * @param ModelManager $manager + * @param Model $model + * @param mixed $toModel + * @param array $options * @return void */ - protected function preparation() + public function __construct(Command $command, + ModelManager $manager, + Model $model, + $toModel=null, + array $options=[]) { - $refModel = new ReflectionClass($this->model); - $name = strtolower($refModel->getShortName().'able'); - - $this->options['name'] = $this->name = $this->command->ask('What relation name do you use?', $name); - $this->options['table'] = $this->table = Str::plural($this->name); - $this->options['foreign_key'] = $this->name.'_id'; - $this->options['related_key'] = strtolower($refModel->getShortName()).'_id'; + $this->command = $command; + $this->manager = $manager; + $this->db = $this->manager->db; + $this->model = $model; + + if (is_array($toModel)) { + foreach ($toModel as $model) { + $this->toModels[] = $model; + } + } else { + $this->toModels = [$toModel]; + } - $this->offerAddRelationModels(); + $this->setDefaultOptions($options); + $this->stylingText(); + $this->setRelationOptions($options); + $this->options = array_merge($this->defaultOptions, $this->options); } - + /** - * reposition model by relations key + * set default options * + * @param array $options * @return void */ - protected function repositionModelByKeys() + protected function setDefaultOptions(array $options=[]) { - if (! $this->isPositionModelValid()) { - $this->askWhereForeignKeyTable(); + $this->text['relation_name_text'] = $this->command->paintString('relation name', 'brown'); + + if (! isset($options['name'])) { + $refModel = new ReflectionClass($this->model); + $name = $this->getRelationName(strtolower($refModel->getShortName())); + $name = $this->command->ask("What {$this->text['relation_name_text']} do you use?", $name); + } else { + $name = $options['name']; } + + $this->defaultOptions = [ + 'name' => $name, + 'pivot_table' => Str::plural($name), + 'foreign_key' => $name.'_id', + 'related_key' => $this->model->getForeignKey() + ]; } /** - * check is position model is valid + * styling text * - * @return bool + * @return void */ - protected function isPositionModelValid() + protected function stylingText() { - return $this->database->isTableExists($this->table) - AND $this->database->isFieldExists($this->table, $this->options['foreign_key']) - AND $this->database->isFieldExists($this->table, $this->options['related_key']); + $this->text = array_merge($this->text, [ + 'name' => "[".$this->command->paintString($this->defaultOptions['name'], 'green')."]", + 'pivot_table' => "[".$this->command->paintString($this->defaultOptions['pivot_table'], 'green')."]", + 'foreign_key' => "[".$this->command->paintString($this->defaultOptions['foreign_key'], 'green')."]", + 'related_key' => "[".$this->command->paintString($this->defaultOptions['related_key'], 'green')."]", + 'pivot_text' => $this->command->paintString('pivot table', 'brown'), + 'foreign_text' => $this->command->paintString('foreign key', 'brown'), + 'related_text' => $this->command->paintString('related key', 'brown'), + ]); } - + /** - * offer to add more models + * get relation name form table name * - * @return void + * @param string $tableName + * @return string */ - protected function offerAddRelationModels() + protected function getRelationName($tableName) { - $models = $this->manager->getModels()->pluck('name'); - - $refToModel = new ReflectionClass($this->toModel); - $toModelName = $refToModel->getShortName(); - $toModelKey = array_search($toModelName, $models->toArray()); - - $refModel = new ReflectionClass($this->model); - $modelName = $refModel->getShortName(); - - if ($this->command->confirm("Do you want to add more relations model that connecting with {$modelName}? no if you want to connect {$toModelName} only")) { - $addModelKeys = $this->command->choice('You can add multiple model with sparated comma, choice models', $models->except($toModelKey)->toArray(), null, 1, true); + $name = Str::singular($tableName); - foreach ($addModelKeys as $model) { - $this->toModels[] = $this->manager->makeClass($model); - } - } + return $name.$this->nameSuffix; } /** - * ask which table where foreign key filed exists + * set connected db relation options * * @return void */ - protected function askWhereForeignKeyTable() + protected function setConnectedRelationOptions() { - $this->options['table'] = $this->table = $this->command->choice( - "Can't find table {$this->table}, what are you using?", - $this->database->getTables() - ); + $foreignKey = $this->defaultOptions['foreign_key']; + + if (! $this->db->isTableExists($table = $this->defaultOptions['pivot_table'])) { + $table = $this->options['pivot_table'] = $this->command->choice( + "Can't find table {$this->text['pivot_table']} as {$this->text['pivot_text']}, what are you using?", + $this->getTables() + ); - if (! $this->database->isFieldExists($this->table, $foreignKey = $this->options['foreign_key'])) { + $this->text['pivot_table'] = "[".$this->command->paintString($table, 'green')."]"; + $name = Str::singular($table); + $foreignKey = $name.'_id'; + } + + if (! $this->db->isFieldExists($table, $foreignKey)) { $this->options['foreign_key'] = $this->command->choice( - "Can't find field {$foreignKey} in the table {$this->table} as foreign key of table {$this->model->getTable()}, choice one!", - $this->database->getTableFields($this->table) + "Can't find field {$this->text['foreign_key']} in the table {$this->text['pivot_table']} as {$this->text['foreign_text']}, choice one!", + $this->getFields($table) ); } - if (! $this->database->isFieldExists($this->table, $relatedKey = $this->options['related_key'])) { + if (! $this->db->isFieldExists($table, $this->defaultOptions['related_key'])) { $this->options['related_key'] = $this->command->choice( - "Can't find field {$relatedKey} in the table {$this->table} as related key of table {$this->model->getTable()}, choice one!", - $this->database->getTableFields($this->table) + "Can't find field {$this->text['related_key']} in the table {$this->text['pivot_table']} as {$this->text['related_text']}, choice one!", + $this->getFields($table) ); } } /** - * build relations model to model + * get relation options rules + * + * @return array + */ + protected function getRelationOptionsRules() + { + return [ + "There should be table {$this->text['pivot_table']} as {$this->text['pivot_text']} of both relation", + "There should be field {$this->text['foreign_key']} in the table {$this->text['pivot_table']} as {$this->text['foreign_text']}", + "There should be field {$this->text['related_key']} in the table {$this->text['pivot_table']} as {$this->text['related_text']}", + ]; + } + + /** + * ask to use custome options * * @return void */ - public function buildRelations() + protected function askToUseCustomeOptions() { - $this->toModels[] = $this->toModel; + $this->options['pivot_table'] = $this->command->ask( + "The {$this->text['pivot_text']} of both relation will be?", + $this->defaultOptions['pivot_table'] + ); + + $name = Str::singular($this->options['pivot_table']); + $foreignKey = $name.'_id'; + + $this->options['foreign_key'] = $this->command->ask( + "The {$this->text['foreign_text']} in the table {$this->text['pivot_table']} will be?", + $foreignKey + ); + + $this->options['related_key'] = $this->command->ask( + "The {$this->text['related_text']} in the table {$this->text['pivot_table']} will be?", + $this->defaultOptions['related_key'] + ); + } - foreach ($this->toModels as $model) { - $this->command->buildMethod($model, 'morphToMany', $this->model, $this->options); - $this->command->buildMethod($this->model, 'morphedByMany', $model, $this->options); + /** + * build relations between models + * + * @return void + */ + public function buildRelations() + { + foreach ($this->toModels as $key => $toModel) { + $this->command->buildMethod($this->model, 'morphedByMany', $toModel, $this->options); + $this->command->buildMethod($toModel, 'morphToMany', $this->model, $this->options); } } } diff --git a/src/Flipbox/OrmManager/BothRelations/MorphOneToMany.php b/src/Flipbox/OrmManager/BothRelations/MorphOneToMany.php index 9327981..75322cf 100644 --- a/src/Flipbox/OrmManager/BothRelations/MorphOneToMany.php +++ b/src/Flipbox/OrmManager/BothRelations/MorphOneToMany.php @@ -5,18 +5,18 @@ class MorphOneToMany extends MorphOneToOne { /** - * build relations model to model + * build relations between models * * @return void */ public function buildRelations() { - $this->command->buildMethod($this->model, 'morphTo', null, $this->options); + $this->command->buildMethod($this->model, 'morphTo', $this->toModel, $this->options); - $this->toModels[] = $this->toModel; - - foreach ($this->toModels as $model) { - $this->command->buildMethod($model, 'morphMany', $this->model, $this->options); + foreach ($this->toModels as $key => $toModel) { + $options = $this->options; + $options['primary_key'] = $this->options['primary_key'][$key]; + $this->command->buildMethod($toModel, 'morphMany', $this->model, $options); } } } diff --git a/src/Flipbox/OrmManager/BothRelations/MorphOneToOne.php b/src/Flipbox/OrmManager/BothRelations/MorphOneToOne.php index a6f2278..bf8d428 100644 --- a/src/Flipbox/OrmManager/BothRelations/MorphOneToOne.php +++ b/src/Flipbox/OrmManager/BothRelations/MorphOneToOne.php @@ -3,110 +3,224 @@ namespace Flipbox\OrmManager\BothRelations; use ReflectionClass; -use Illuminate\Support\Arr; +use Illuminate\Console\Command; +use Flipbox\OrmManager\ModelManager; use Illuminate\Database\Eloquent\Model; -class MorphOneToOne extends Both +class MorphOneToOne extends BothRelation { /** - * name + * method suffix * * @var string */ - protected $name; + protected $nameSuffix = 'able'; /** - * to models + * type suffix * - * @var array + * @var string */ - protected $toModels = []; + protected $typeSuffix = '_type'; /** - * preparation oprations + * id suffix + * + * @var string + */ + protected $idSuffix = '_id'; + + /** + * Create a new Model instance. * - * @param + * @param Command $command + * @param ModelManager $manager + * @param Model $model + * @param mixed $toModel + * @param array $options * @return void */ - protected function preparation() + public function __construct(Command $command, + ModelManager $manager, + Model $model, + $toModel=null, + array $options=[]) { - $refModel = new ReflectionClass($this->model); - $name = strtolower($refModel->getShortName().'able'); - $this->options['name'] = $this->name = $this->command->ask('What relation name do you use?', $name); + $this->command = $command; + $this->manager = $manager; + $this->db = $this->manager->db; + $this->model = $model; + + if (is_array($toModel)) { + foreach ($toModel as $model) { + $this->toModels[] = $model; + } + } else { + $this->toModels = [$toModel]; + } - $this->offerAddRelationModels(); + $this->setDefaultOptions($options); + $this->stylingText(); + $this->setRelationOptions($options); + $this->options = array_merge($this->defaultOptions, $this->options); } /** - * offer to add more models + * set default options * + * @param $options * @return void */ - protected function offerAddRelationModels() + protected function setDefaultOptions(array $options=[]) { - $models = $this->manager->getModels()->pluck('name'); + $this->text['relation_name_text'] = $this->command->paintString('relation name', 'brown'); - $refToModel = new ReflectionClass($this->toModel); - $toModelName = $refToModel->getShortName(); - $toModelKey = array_search($toModelName, $models->toArray()); - $refModel = new ReflectionClass($this->model); - $modelName = $refModel->getShortName(); + $name = $this->getRelationName(strtolower($refModel->getShortName())); + $name = $this->command->ask("What {$this->text['relation_name_text']} do you use?", $name); - if ($this->command->confirm("Do you want to add more relations model that connecting with {$modelName}? no if you want to connect {$toModelName} only")) { - $addModelKeys = $this->command->choice('You can add multiple model with sparated comma, choice models', $models->except($toModelKey)->toArray(), null, 1, true); + $this->defaultOptions = [ + 'name' => $name, + 'type' => $this->getTypeName($name), + 'id' => $this->getIdName($name), + ]; - foreach ($addModelKeys as $model) { - $this->toModels[] = $this->manager->makeClass($model); - } + foreach ($this->toModels as $key => $toModel) { + $this->defaultOptions['primary_key'][$key] = $toModel->getKeyName(); } } /** - * check is position model is valid + * styling text * - * @return bool + * @return void */ - protected function isPositionModelValid() + protected function stylingText() { - return $this->database->isFieldExists($this->model->getTable(), $this->name.'_type') - AND $this->database->isFieldExists($this->model->getTable(), $this->name.'_id'); + $this->text = array_merge($this->text, [ + 'table' => "[".$this->command->paintString($this->model->getTable(), 'green')."]", + 'name' => "[".$this->command->paintString($this->defaultOptions['name'], 'green')."]", + 'type' => "[".$this->command->paintString($this->defaultOptions['type'], 'green')."]", + 'id' => "[".$this->command->paintString($this->defaultOptions['id'], 'green')."]", + 'related_type_text' => $this->command->paintString('related type', 'brown'), + 'related_id_text' => $this->command->paintString('related id', 'brown'), + 'primary_text' => $this->command->paintString('primary key', 'brown'), + ]); + + foreach ($this->toModels as $key => $toModel) { + $this->text['to_table'][$key] = "[".$this->command->paintString($toModel->getTable(), 'green')."]"; + $this->text['primary_key'][$key] = "[".$this->command->paintString($this->defaultOptions['primary_key'][$key], 'green')."]"; + } } /** - * ask which table where foreign key filed exists + * get connected db relation options * * @return void */ - protected function askWhereForeignKeyTable() + protected function setConnectedRelationOptions() { - if (! is_null($this->model)) { - $tables[] = $this->model->getTable(); + $fields = $this->getFields($this->model->getTable()); + $name = $this->defaultOptions['name']; + + if (! in_array($this->defaultOptions['type'], $fields) + OR ! in_array($this->defaultOptions['id'], $fields)) { + $this->options['name'] = $name = $this->command->ask("Can't find {$this->text['type']} or {$this->text['id']} in the table {$this->text['table']}, you may not use {$this->text['name']} as {$this->text['relation_name_text']}, what are you using?"); + $this->text['type'] = "[".$this->command->paintString($this->getTypeName($name), 'green')."]"; + $this->text['id'] = "[".$this->command->paintString($this->getIdName($name), 'green')."]"; } - if (! is_null($this->toModel)) { - $tables[] = $this->toModel->getTable(); + if (! in_array($this->getTypeName($name), $fields)) { + $this->options['type'] = $this->command->choice("Can't find {$this->text['type']} as {$this->text['related_type_text']}, what are you using?", $fields); } - $foreignTable = $this->command->choice("Which table that conatain {$this->name}_type and {$this->name}_id key?", $tables); + if (! in_array($this->getIdName($name), $fields)) { + $this->options['id'] = $this->command->choice("Can't find {$this->text['id']} as {$this->text['related_id_text']}, what are you using?", $fields); + } - $this->options['type'] = $this->command->choice("what {$this->name}_type field in {$foreignTable}?", - $this->database->getTableFields($foreignTable) - ); + foreach ($this->toModels as $key => $toModel) { + if (! $this->db->isFieldExists($table = $toModel->getTable(), $this->defaultOptions['primary_key'][$key])) { + $this->options['primary_key'][$key] = $this->command->choice( + "Can't find field {$this->text['primary_key'][$key]} in the table {$this->text['to_table'][$key]} as {$this->text['primary_text']}, choice one!", + $this->getFields($table) + ); + } + } + } - $this->options['id'] = $this->command->choice("what {$this->name}_id field in {$foreignTable}?", - $this->database->getTableFields($foreignTable) - ); + /** + * get relation options rules + * + * @return array + */ + protected function getRelationOptionsRules() + { + $ruels = [ + "The {$this->text['relation_name_text']} is {$this->text['name']}", + "There should be field {$this->text['type']} in table {$this->text['table']} as {$this->text['related_type_text']}", + "There should be field {$this->text['id']} in table {$this->text['table']} as {$this->text['related_id_text']}", + ]; + + foreach ($this->toModels as $key => $toModel) { + $rules[] = "There should be field {$this->text['primary_key'][$key]} in table {$this->text['to_table'][$key]} as {$this->text['primary_text']}"; + } - $foreignModel = $this->manager->tableToModel($foreignTable); - - if ($foreignModel != $this->toModel) { - $this->exchangeModelPosition(); + return $rules; + } + + /** + * ask to use custome options + * + * @return void + */ + protected function askToUseCustomeOptions() + { + $this->options = [ + 'name' => $this->command->ask("The {$this->text['relation_name_text']} of relation is will be?", $this->defaultOptions['name']), + 'type' => $this->command->ask("The {$this->text['related_type_text']} of relation is will be?", $this->getTypeName($this->options['name'])), + 'id' => $this->command->ask("The {$this->text['related_id_text']} of relation is will be?", $this->getIdName($this->options['name'])), + ]; + + foreach ($this->toModels as $key => $toModel) { + $this->options['primary_key'][$key] = $this->command->ask("The {$this->text['primary_text']} of the table {$this->text['to_table'][$key]} will be?", $this->defaultOptions['primary_key'][$key]); } } /** - * build relations model to model + * get method name form class + * + * @param string $name + * @return string + */ + protected function getRelationName($name) + { + return $name.$this->nameSuffix; + } + + /** + * get type name + * + * @param string $name + * @return string + */ + protected function getTypeName($name) + { + return ($name ?: $this->defaultOptions['name']).$this->typeSuffix; + } + + /** + * get id name + * + * @param string $name + * @return string + */ + protected function getIdName($name) + { + return ($name ?: $this->defaultOptions['name']).$this->idSuffix; + } + + /** + * build relations between models * * @return void */ @@ -114,10 +228,10 @@ public function buildRelations() { $this->command->buildMethod($this->model, 'morphTo', null, $this->options); - $this->toModels[] = $this->toModel; - - foreach ($this->toModels as $model) { - $this->command->buildMethod($model, 'morphOne', $this->model, $this->options); + foreach ($this->toModels as $key => $toModel) { + $options = $this->options; + $options['primary_key'] = $this->options['primary_key'][$key]; + $this->command->buildMethod($toModel, 'morphOne', $this->model, $options); } } } diff --git a/src/Flipbox/OrmManager/BothRelations/OneToOne.php b/src/Flipbox/OrmManager/BothRelations/OneToOne.php index b0698e8..f5e8c4f 100644 --- a/src/Flipbox/OrmManager/BothRelations/OneToOne.php +++ b/src/Flipbox/OrmManager/BothRelations/OneToOne.php @@ -2,48 +2,96 @@ namespace Flipbox\OrmManager\BothRelations; -use ReflectionClass; - class OneToOne extends BothRelation { /** - * check is position model is valid + * set default options * - * @return bool + * @param array $options + * @return void */ - protected function isPositionModelValid() + protected function setDefaultOptions(array $options=[]) { - $foreignTable = $this->toModel->getTable(); - $refModel = new ReflectionClass($this->model); - $foreignKey = strtolower($refModel->getShortName()).'_'.$this->model->getKeyName(); - - return $this->database->isFieldExists($foreignTable, $foreignKey); + $this->defaultOptions = [ + 'foreign_key' => $this->model->getForeignKey(), + 'primary_key' => $this->model->getKeyName() + ]; } /** - * ask which table where foreign key filed exists + * styling text * * @return void */ - protected function askWhereForeignKeyTable() + protected function stylingText() { - $foreignTable = $this->command->choice('Which table that conatain foreign key?', [ - $this->toModel->getTable(), $this->model->getTable(), - ]); + $modelTable = $this->model->getTable(); + $toModelTable = $this->toModel->getTable(); + $foreignKey = $this->defaultOptions['foreign_key']; + $primaryKey = $this->defaultOptions['primary_key']; - $this->options['foreign_key'] = $this->command->choice('what foreign key of both relation?', - $this->getFields($foreignTable) - ); + $this->text = [ + 'table' => "[".$this->command->paintString($modelTable ,'green')."]", + 'to_table' => "[".$this->command->paintString($toModelTable ,'green')."]", + 'foreign_key' => "[".$this->command->paintString($foreignKey ,'green')."]", + 'primary_key' => "[".$this->command->paintString($primaryKey ,'green')."]", + 'primary_text' => $this->command->paintString('primary key', 'brown'), + 'foreign_text' => $this->command->paintString('foreign key', 'brown') + ]; + } - $foreignModel = $this->manager->tableToModel($foreignTable); + /** + * get connected db relation options + * + * @return void + */ + protected function setConnectedRelationOptions() + { + $modelTable = $table = $this->model->getTable(); + $toModelTable = $table = $this->toModel->getTable(); + $foreignKey = $this->defaultOptions['foreign_key']; + $primaryKey = $this->defaultOptions['primary_key']; - if ($foreignModel != $this->toModel) { - $this->exchangeModelPosition(); + if (! $this->db->isFieldExists($toModelTable, $foreignKey)) { + $question = "Can't find field {$this->text['foreign_key']} in the table {$this->text['to_table']} as {$this->text['foreign_text']} of table {$this->text['table']}, choice one!"; + $this->options['foreign_key'] = $this->command->choice($question, $this->getFields($toModelTable)); + } + + if (! $this->db->isFieldExists($modelTable, $primaryKey)) { + $question = "Can't find field {$this->text['primary_key']} in the table {$this->text['table']} as {$this->text['primary_text']} of table {$this->text['table']}, choice one!"; + $this->options['primary_key'] = $this->command->choice($question, $this->getFields($modelTable)); } } /** - * build relations model to model + * get relation options rules + * + * @return array + */ + protected function getRelationOptionsRules() + { + return [ + "There should be field {$this->text['foreign_key']} in table {$this->text['to_table']} as {$this->text['foreign_text']} of table {$this->text['table']}", + "There should be field {$this->text['primary_key']} in table {$this->text['table']} as {$this->text['primary_text']}" + ]; + } + + /** + * ask to use custome options + * + * @return void + */ + protected function askToUseCustomeOptions() + { + $question = "The {$this->text['foreign_text']} of table {$this->text['table']} in the table {$this->text['to_table']}, will be?"; + $this->options['foreign_key'] = $this->command->ask($question, $this->defaultOptions['foreign_key']); + + $question = "The {$this->text['primary_text']} of the table {$this->text['table']}, will be?"; + $this->options['primary_key'] = $this->command->ask($question, $this->defaultOptions['primary_key']); + } + + /** + * build relations between models * * @return void */ diff --git a/src/Flipbox/OrmManager/Consoles/ModelBothConnect.php b/src/Flipbox/OrmManager/Consoles/ModelBothConnect.php index 9ff7510..fedcfe2 100644 --- a/src/Flipbox/OrmManager/Consoles/ModelBothConnect.php +++ b/src/Flipbox/OrmManager/Consoles/ModelBothConnect.php @@ -13,6 +13,13 @@ class ModelBothConnect extends ModelConnect { + /** + * use multiple to model + * + * @var bool + */ + protected $multipleToModel = false; + /** * The console command name. * @@ -35,17 +42,21 @@ class ModelBothConnect extends ModelConnect */ public function handle() { + $this->multipleToModel = in_array($this->argument('relation'), ['morphOneToOne', 'morphOneToMany', 'morphManyToMany']); + try { if ($this->option('interactive')) { extract($this->runInteractiveConnect()); } else { extract($this->getArgumentConnect()); } + $this->buildRelations($model, $relation, $toModel); } catch (Exception $e) { return $this->error($e->getMessage()); } } + /** * get data input from arguments * @@ -56,7 +67,7 @@ protected function getArgumentConnect() if ($this->isRequiredArgFulfilled($this->arguments())) { $data['model'] = $this->getModel($this->argument('model')); $data['relation'] = $this->getRelation($this->argument('relation')); - $data['toModel'] = $this->getModel($this->argument('to-model')); + $data['toModel'] = $this->getModel($this->argument('to-model'), $this->multipleToModel); return $data; } @@ -99,10 +110,12 @@ protected function runInteractiveConnect() $default = $search === false ? null : $search; $data['relation'] = $this->choice('Which relation between two models?', $this->manager->both_relations, $default); - $search = array_search($this->argument('model'), $models); + $search = array_search($this->argument('to-model'), $models); $default = $search === false ? null : $search; - $askToModel = $this->choice('Which model that you want to connect with '.$askModel, $models, $default); - $data['toModel'] = $this->getModel($askToModel); + $info = "(Use comma separated for multiple models)"; + $multipleInfo = $this->multipleToModel ? $this->paintString($info, 'green') : ''; + $askToModel = $this->choice('Which '.($this->multipleToModel ? 'models' : 'model').' that you want to connect with '.$askModel.' '.$multipleInfo, $models, $default, null, $this->multipleToModel); + $data['toModel'] = $this->getModel($askToModel, $this->multipleToModel); return $data; } @@ -127,10 +140,10 @@ protected function getRelation($relation) * * @param Model $model * @param string $relation - * @param mix Model $toModel + * @param mixed $toModel * @return void */ - protected function buildRelations(Model $model, $relation, Model $toModel) + protected function buildRelations(Model $model, $relation, $toModel) { try { $bothRelation = $this->newBothRelationInstance($relation, $model, $toModel); diff --git a/src/Flipbox/OrmManager/Consoles/ModelConnect.php b/src/Flipbox/OrmManager/Consoles/ModelConnect.php index 2b68f43..1203fbb 100644 --- a/src/Flipbox/OrmManager/Consoles/ModelConnect.php +++ b/src/Flipbox/OrmManager/Consoles/ModelConnect.php @@ -160,10 +160,21 @@ protected function runInteractiveConnect() * get model * * @param string $name + * @param bool $multiple * @return Object */ - protected function getModel($name) + protected function getModel($name, $multiple=false) { + if ($multiple) { + $models = []; + + foreach (explode(',', $name) as $model) { + $models[] = $this->getModel($model); + } + + return $models; + } + if ($this->manager->isModelExists($name)) { return $this->manager->makeClass($name); } @@ -199,7 +210,6 @@ public function buildMethod(Model $model, $relation, $toModel, $options=[]) { try { $relation = $this->newRelationInstance($relation, $model, $toModel, $options); - $relation->createMethod(); $this->info('Connection has been created'); diff --git a/src/Flipbox/OrmManager/Relations/BelongsTo.php b/src/Flipbox/OrmManager/Relations/BelongsTo.php index 48b7d23..e59b848 100644 --- a/src/Flipbox/OrmManager/Relations/BelongsTo.php +++ b/src/Flipbox/OrmManager/Relations/BelongsTo.php @@ -2,8 +2,6 @@ namespace Flipbox\OrmManager\Relations; -use Illuminate\Support\Str; - class BelongsTo extends HasOne { /** diff --git a/src/Flipbox/OrmManager/Relations/BelongsToMany.php b/src/Flipbox/OrmManager/Relations/BelongsToMany.php index bed4413..e6fc27c 100644 --- a/src/Flipbox/OrmManager/Relations/BelongsToMany.php +++ b/src/Flipbox/OrmManager/Relations/BelongsToMany.php @@ -6,13 +6,6 @@ class BelongsToMany extends Relation { - /** - * maps - * - * @var array - */ - protected $maps; - /** * set default options * @@ -28,16 +21,13 @@ protected function setDefaultOptions(array $options=[]) asort($tables, SORT_REGULAR); $pivotTable = implode('_', array_map([Str::class, 'singular'], $tables)); - $this->defaultOptions['pivot_table'] = $pivotTable; - - $no = 1; - foreach ($tables as $key => $table) { - $foreignKey = Str::singular($table).'_'.$this->$key->getKeyName(); - $this->defaultOptions['foreign_key_'.($no++)] = $foreignKey; - $this->maps[$foreignKey] = $this->manager->tableToModel($table); - } - - $this->checkingOptions = array_merge($this->defaultOptions, $options); + + $this->defaultOptions = [ + 'pivot_table' => $pivotTable, + 'foreign_key' => $this->model->getForeignKey(), + 'related_key' => $this->toModel->getForeignKey(), + 'relation' => $this->toModel->getTable() + ]; } /** @@ -47,12 +37,21 @@ protected function setDefaultOptions(array $options=[]) */ protected function stylingText() { - $pivotTable = $this->checkingOptions['pivot_table']; + $modelTable = $this->model->getTable(); + $toModelTable = $this->toModel->getTable(); + $pivotTable = $this->defaultOptions['pivot_table']; + $foreignKey = $this->defaultOptions['foreign_key']; + $relatedKey = $this->defaultOptions['related_key']; $this->text = [ + 'table' => "[".$this->command->paintString($modelTable ,'green')."]", + 'to_table' => "[".$this->command->paintString($toModelTable ,'green')."]", 'pivot_table' => "[".$this->command->paintString($pivotTable ,'green')."]", + 'foreign_key' => "[".$this->command->paintString($foreignKey ,'green')."]", + 'related_key' => "[".$this->command->paintString($relatedKey ,'green')."]", 'pivot_text' => $this->command->paintString('pivot table', 'brown'), 'foreign_text' => $this->command->paintString('foreign key', 'brown'), + 'related_text' => $this->command->paintString('related key', 'brown'), ]; } @@ -63,31 +62,27 @@ protected function stylingText() */ protected function setConnectedRelationOptions() { - $pivotTable = $this->checkingOptions['pivot_table']; + $pivotTable = $this->defaultOptions['pivot_table']; + $foreignKey = $this->defaultOptions['foreign_key']; + $relatedKey = $this->defaultOptions['related_key']; if (! $this->db->isTableExists($pivotTable)) { $question = "Can't find table {$this->text['pivot_table']} in the database as {$this->text['pivot_text']}, choice one!"; $pivotTable = $this->options['pivot_table'] = $this->command->choice( - $question, $this->db->getTables()); + $question, $this->getTables()); $this->text['pivot_table'] = "[".$this->command->paintString($pivotTable, 'green')."]"; } - $key = 1; - - foreach ($this->maps as $foreignKey => $model) { - $paintedTable = "[".$this->command->paintString($model->getTable(), 'green')."]"; - $foreignKey = $this->checkingOptions['foreign_key_'.$key]; - $paintedForeignKey = "[".$this->command->paintString($foreignKey, 'green')."]"; - - if (! $this->db->isFieldExists($pivotTable, $foreignKey)) { - $question = "Can't find field {$paintedForeignKey} in the table {$this->text['pivot_table']} as {$this->text['foreign_text']} of table {$paintedTable}, choice one!"; - $this->options['foreign_key_'.$key] = $this->command->choice( - $question, $this->getFields($pivotTable)); - } + if (! $this->db->isFieldExists($pivotTable, $foreignKey)) { + $question = "Can't find field {$this->text['foreign_key']} in the table {$this->text['pivot_table']} as {$this->text['foreign_text']} of table {$this->text['table']}, choice one!"; + $this->options['foreign_key'] = $this->command->choice($question, $this->getFields($pivotTable)); + } - $key++; - } + if (! $this->db->isFieldExists($pivotTable, $relatedKey)) { + $question = "Can't find field {$this->text['related_key']} in the table {$this->text['pivot_table']} as {$this->text['related_text']} of table {$this->text['to_table']}, choice one!"; + $this->options['related_key'] = $this->command->choice($question, $this->getFields($pivotTable)); + } } /** @@ -97,20 +92,11 @@ protected function setConnectedRelationOptions() */ protected function getRelationOptionsRules() { - $this->text['pivot_table'] = "[".$this->command->paintString($this->defaultOptions['pivot_table'], 'green')."]"; - $rules = ["There should be table {$this->text['pivot_table']} in the database as {$this->text['pivot_text']}"]; - - $key = 1; - - foreach ($this->maps as $foreignKey => $model) { - $paintedTable = "[".$this->command->paintString($model->getTable(), 'green')."]"; - $foreignKey = $this->checkingOptions['foreign_key_'.$key]; - $paintedForeignKey = "[".$this->command->paintString($foreignKey, 'green')."]"; - $rules[] = "There should be field {$paintedForeignKey} in the table {$this->text['pivot_table']} as {$this->text['foreign_text']} of table {$paintedTable}"; - $key++; - } - - return $rules; + return [ + "There should be table {$this->text['pivot_table']} in the database as {$this->text['pivot_text']}", + "There should be field {$this->text['foreign_key']} in table {$this->text['pivot_table']} as {$this->text['foreign_text']} of table {$this->text['table']}", + "There should be field {$this->text['related_key']} in table {$this->text['pivot_table']} as {$this->text['related_key']} of table {$this->text['to_table']}" + ]; } /** @@ -135,18 +121,11 @@ protected function askToUseCustomeOptions() $this->options['pivot_table'] = $this->command->ask($question, $this->defaultOptions['pivot_table']); $this->text['pivot_table'] = "[".$this->command->paintString($this->options['pivot_table'], 'green')."]"; - $key = 1; - - foreach ($this->maps as $foreignKey => $model) { - $paintedTable = "[".$this->command->paintString($model->getTable(), 'green')."]"; - $foreignKey = $this->checkingOptions['foreign_key_'.$key]; - $paintedForeignKey = "[".$this->command->paintString($foreignKey, 'green')."]"; - - $question = "The {$this->text['foreign_text']} of table {$paintedTable} in the table {$this->text['pivot_table']} will be?"; - - $this->options['foreign_key_'.$key] = $this->command->ask($question, $this->defaultOptions['foreign_key_'.$key]); - $key++; - } + $question = "The {$this->text['foreign_text']} of table {$this->text['table']} in the table {$this->text['pivot_table']}, will be?"; + $this->options['foreign_key'] = $this->command->ask($question, $this->defaultOptions['foreign_key']); + + $question = "The {$this->text['related_text']} of table {$this->text['to_table']} in the table {$this->text['pivot_table']}, will be?"; + $this->options['related_key'] = $this->command->ask($question, $this->defaultOptions['related_key']); } /** diff --git a/src/Flipbox/OrmManager/Relations/HasManyThrough.php b/src/Flipbox/OrmManager/Relations/HasManyThrough.php index 70ceccf..244a74d 100644 --- a/src/Flipbox/OrmManager/Relations/HasManyThrough.php +++ b/src/Flipbox/OrmManager/Relations/HasManyThrough.php @@ -28,8 +28,6 @@ protected function setDefaultOptions(array $options=[]) 'foreign_key_2' => Str::singular($intermediateModel->getTable()).'_'.$intermediateModel->getKeyName(), 'primary_key' => $this->model->getKeyName() ]; - - $this->checkingOptions = array_merge($this->defaultOptions, $options); } /** @@ -39,12 +37,12 @@ protected function setDefaultOptions(array $options=[]) */ protected function stylingText() { - $this->text['foreign_key_1'] = "[".$this->command->paintString($this->checkingOptions['foreign_key_1'] ,'green')."]"; - $this->text['foreign_key_2'] = "[".$this->command->paintString($this->checkingOptions['foreign_key_2'] ,'green')."]"; - $this->text['primary_key'] = "[".$this->command->paintString($this->checkingOptions['primary_key'] ,'green')."]"; + $this->text['foreign_key_1'] = "[".$this->command->paintString($this->defaultOptions['foreign_key_1'] ,'green')."]"; + $this->text['foreign_key_2'] = "[".$this->command->paintString($this->defaultOptions['foreign_key_2'] ,'green')."]"; + $this->text['primary_key'] = "[".$this->command->paintString($this->defaultOptions['primary_key'] ,'green')."]"; $this->text['model_table'] = "[".$this->command->paintString($this->model->getTable() ,'green')."]"; $this->text['to_model_table'] = "[".$this->command->paintString($this->toModel->getTable() ,'green')."]"; - $this->text['intermediate_model_table'] = "[".$this->command->paintString($this->checkingOptions['intermediate_model']->getTable() ,'green')."]"; + $this->text['intermediate_model_table'] = "[".$this->command->paintString($this->defaultOptions['intermediate_model']->getTable() ,'green')."]"; $this->text['foreign_text'] = $this->command->paintString('foreign key', 'brown'); $this->text['primary_text'] = $this->command->paintString('primary key', 'brown'); } @@ -56,27 +54,27 @@ protected function stylingText() */ protected function setConnectedRelationOptions() { - $intermediateModel = $this->checkingOptions['intermediate_model']; + $intermediateModel = $this->defaultOptions['intermediate_model']; if (! $this->db->isTableExists($intermediateModel->getTable())) { throw new TableNotExists($intermediateModel->getTable(), get_class($intermediateModel)); } - if (! $this->db->isFieldExists($table = $intermediateModel->getTable(), $this->checkingOptions['foreign_key_1'])) { + if (! $this->db->isFieldExists($table = $intermediateModel->getTable(), $this->defaultOptions['foreign_key_1'])) { $this->options['foreign_key_1'] = $this->command->choice( "Can't find field {$this->text['foreign_key_1']} in the table {$this->text['intermediate_model_table']} as {$this->text['foreign_text']} of table {$this->text['model_table']}, choice one!", $this->getFields($table) ); } - if (! $this->db->isFieldExists($table = $this->toModel->getTable(), $this->checkingOptions['foreign_key_2'])) { + if (! $this->db->isFieldExists($table = $this->toModel->getTable(), $this->defaultOptions['foreign_key_2'])) { $this->options['foreign_key_2'] = $this->command->choice( "Can't find field {$this->text['foreign_key_2']} in the table {$this->text['to_model_table']} as {$this->text['foreign_text']} of table {$this->text['intermediate_model_table']}, choice one!", $this->getFields($table) ); } - if (! $this->db->isFieldExists($table = $this->model->getTable(), $primaryKey = $this->checkingOptions['primary_key'])) { + if (! $this->db->isFieldExists($table = $this->model->getTable(), $primaryKey = $this->defaultOptions['primary_key'])) { $this->options['primary_key'] = $this->command->choice( "Can't find field {$this->text['primary_key']} in the table {$this->text['model_table']} as {$this->text['primary_text']}, choice one!", $this->getFields($table) @@ -91,7 +89,7 @@ protected function setConnectedRelationOptions() */ protected function getRelationOptionsRules() { - $intermediateModel = $this->checkingOptions['intermediate_model']; + $intermediateModel = $this->defaultOptions['intermediate_model']; return [ "There should be field {$this->text['foreign_key_1']} in table {$this->text['intermediate_model_table']} as {$this->text['foreign_text']} of table {$this->text['model_table']}", @@ -118,7 +116,7 @@ protected function getMethodName($name) */ protected function askToUseCustomeOptions() { - $intermediateModel = $this->checkingOptions['intermediate_model']; + $intermediateModel = $this->defaultOptions['intermediate_model']; $this->options['foreign_key_1'] = $this->command->ask( "The {$this->text['foreign_text']} of table {$this->text['model_table']} in the table {$this->text['intermediate_model_table']} will be?", diff --git a/src/Flipbox/OrmManager/Relations/HasOne.php b/src/Flipbox/OrmManager/Relations/HasOne.php index ea1c13e..d0ffea4 100644 --- a/src/Flipbox/OrmManager/Relations/HasOne.php +++ b/src/Flipbox/OrmManager/Relations/HasOne.php @@ -2,8 +2,6 @@ namespace Flipbox\OrmManager\Relations; -use Illuminate\Support\Str; - class HasOne extends Relation { /** @@ -15,11 +13,9 @@ class HasOne extends Relation protected function setDefaultOptions(array $options=[]) { $this->defaultOptions = [ - 'foreign_key' => Str::singular($this->model->getTable()).'_'.$this->model->getKeyName(), + 'foreign_key' => $this->model->getForeignKey(), 'primary_key' => $this->model->getKeyName() ]; - - $this->checkingOptions = array_merge($this->defaultOptions, $options); } /** @@ -29,10 +25,10 @@ protected function setDefaultOptions(array $options=[]) */ protected function stylingText() { - $modelTable = $table = $this->model->getTable(); - $toModelTable = $table = $this->toModel->getTable(); - $foreignKey = $this->checkingOptions['foreign_key']; - $primaryKey = $this->checkingOptions['primary_key']; + $modelTable = $this->model->getTable(); + $toModelTable = $this->toModel->getTable(); + $foreignKey = $this->defaultOptions['foreign_key']; + $primaryKey = $this->defaultOptions['primary_key']; $this->text = [ 'table' => "[".$this->command->paintString($modelTable ,'green')."]", @@ -53,15 +49,15 @@ protected function setConnectedRelationOptions() { $modelTable = $table = $this->model->getTable(); $toModelTable = $table = $this->toModel->getTable(); - $foreignKey = $this->checkingOptions['foreign_key']; - $primaryKey = $this->checkingOptions['primary_key']; + $foreignKey = $this->defaultOptions['foreign_key']; + $primaryKey = $this->defaultOptions['primary_key']; - if (! $this->database->isFieldExists($toModelTable, $foreignKey)) { + if (! $this->db->isFieldExists($toModelTable, $foreignKey)) { $question = "Can't find field {$this->text['foreign_key']} in the table {$this->text['to_table']} as {$this->text['foreign_text']} of table {$this->text['table']}, choice one!"; $this->options['foreign_key'] = $this->command->choice($question, $this->getFields($toModelTable)); } - if (! $this->database->isFieldExists($modelTable, $primaryKey)) { + if (! $this->db->isFieldExists($modelTable, $primaryKey)) { $question = "Can't find field {$this->text['primary_key']} in the table {$this->text['table']} as {$this->text['primary_text']} of table {$this->text['table']}, choice one!"; $this->options['primary_key'] = $this->command->choice($question, $this->getFields($modelTable)); } @@ -76,7 +72,7 @@ protected function getRelationOptionsRules() { return [ "There should be field {$this->text['foreign_key']} in table {$this->text['to_table']} as {$this->text['foreign_text']} of table {$this->text['table']}", - "There should be field {$this->text['primary_key']} in table {$this->text['table']} as {$this->text['primary_text']} of table {$this->text['table']}" + "There should be field {$this->text['primary_key']} in table {$this->text['table']} as {$this->text['primary_text']}" ]; } diff --git a/src/Flipbox/OrmManager/Relations/MorphOne.php b/src/Flipbox/OrmManager/Relations/MorphOne.php index 2fa0a13..0329784 100644 --- a/src/Flipbox/OrmManager/Relations/MorphOne.php +++ b/src/Flipbox/OrmManager/Relations/MorphOne.php @@ -28,10 +28,10 @@ class MorphOne extends MorphTo */ protected function setDefaultOptions(array $options=[]) { - parent::setDefaultOptions(); + + parent::setDefaultOptions($options); $this->defaultOptions['primary_key'] = $this->toModel->getKeyName(); - $this->checkingOptions = array_merge($this->defaultOptions, $options); } /** @@ -43,8 +43,8 @@ protected function stylingText() { parent::stylingText(); - $this->text['model_table'] = "[".$this->command->paintString($this->model->getTable(), 'green')."]"; - $this->text['primary_key'] = "[".$this->command->paintString($this->checkingOptions['primary_key'], 'green')."]"; + $this->text['table'] = "[".$this->command->paintString($this->model->getTable(), 'green')."]"; + $this->text['primary_key'] = "[".$this->command->paintString($this->defaultOptions['primary_key'], 'green')."]"; $this->text['primary_text'] = $this->command->paintString('primary key', 'brown'); } @@ -59,7 +59,7 @@ protected function setConnectedRelationOptions() if (! $this->db->isFieldExists($table = $this->model->getTable(), $this->defaultOptions['primary_key'])) { $this->options['primary_key'] = $this->command->choice( - "Can't find field {$this->text['primary_key']} in the table {$this->text['model_table']} as {$this->text['primary_text']}, choice one!", + "Can't find field {$this->text['primary_key']} in the table {$this->text['table']} as {$this->text['primary_text']}, choice one!", $this->getFields($table) ); } @@ -85,7 +85,7 @@ protected function getRelationOptionsRules() { $rules = parent::getRelationOptionsRules(); - $rules[] = "There should be field {$this->text['primary_key']} in table {$this->text['model_table']} as {$this->text['primary_text']}"; + $rules[] = "There should be field {$this->text['primary_key']} in table {$this->text['table']} as {$this->text['primary_text']}"; return $rules; } @@ -100,7 +100,7 @@ protected function askToUseCustomeOptions() parent::askToUseCustomeOptions(); $this->options['primary_key'] = $this->command->ask( - "The {$this->text['primary_text']} of the table {$this->text['model_table']} will be?", + "The {$this->text['primary_text']} of the table {$this->text['table']} will be?", $this->defaultOptions['primary_key'] ); } diff --git a/src/Flipbox/OrmManager/Relations/MorphTo.php b/src/Flipbox/OrmManager/Relations/MorphTo.php index 6c4696d..0d175a5 100644 --- a/src/Flipbox/OrmManager/Relations/MorphTo.php +++ b/src/Flipbox/OrmManager/Relations/MorphTo.php @@ -39,7 +39,7 @@ protected function setDefaultOptions(array $options=[]) { $this->text['relation_name_text'] = $this->command->paintString('relation name', 'brown'); - if (! isset($options['name'])) { + if (! isset($options['name'])) { $refModel = new ReflectionClass($this->model); $name = $this->getRelationName(strtolower($refModel->getShortName())); $name = $this->command->ask("What {$this->text['relation_name_text']} do you use?", $name); @@ -52,8 +52,6 @@ protected function setDefaultOptions(array $options=[]) 'type' => $this->getTypeName($name), 'id' => $this->getIdName($name) ]; - - $this->checkingOptions = array_merge($this->defaultOptions, $options); } /** @@ -63,13 +61,14 @@ protected function setDefaultOptions(array $options=[]) */ protected function stylingText() { - $this->text['model_table'] = "[".$this->command->paintString($this->model->getTable(), 'green')."]"; - $this->text['name'] = "[".$this->command->paintString($this->checkingOptions['name'], 'green')."]"; - $this->text['type'] = "[".$this->command->paintString($this->checkingOptions['type'], 'green')."]"; - $this->text['id'] = "[".$this->command->paintString($this->checkingOptions['id'], 'green')."]"; - $this->text['related_type_text'] = $this->command->paintString('related type', 'brown'); - $this->text['related_id_text'] = $this->command->paintString('related id', 'brown'); - + $this->text = [ + 'table' => "[".$this->command->paintString($this->model->getTable(), 'green')."]", + 'name' => "[".$this->command->paintString($this->defaultOptions['name'], 'green')."]", + 'type' => "[".$this->command->paintString($this->defaultOptions['type'], 'green')."]", + 'id' => "[".$this->command->paintString($this->defaultOptions['id'], 'green')."]", + 'related_type_text' => $this->command->paintString('related type', 'brown'), + 'related_id_text' => $this->command->paintString('related id', 'brown') + ]; } /** @@ -80,11 +79,11 @@ protected function stylingText() protected function setConnectedRelationOptions() { $fields = $this->getFields($this->model->getTable()); - $name = $this->checkingOptions['name']; + $name = $this->defaultOptions['name']; - if (! in_array($this->checkingOptions['type'], $fields) - OR ! in_array($this->checkingOptions['id'], $fields)) { - $this->options['name'] = $name = $this->command->ask("Can't find {$this->text['type']} or {$this->text['id']} in the table {$this->text['model_table']}, you may not use {$this->text['name']} as {$this->text['relation_name_text']}, what are you using?"); + if (! in_array($this->defaultOptions['type'], $fields) + OR ! in_array($this->defaultOptions['id'], $fields)) { + $this->options['name'] = $name = $this->command->ask("Can't find {$this->text['type']} or {$this->text['id']} in the table {$this->text['table']}, you may not use {$this->text['name']} as {$this->text['relation_name_text']}, what are you using?"); $this->text['type'] = "[".$this->command->paintString($this->getTypeName($name), 'green')."]"; $this->text['id'] = "[".$this->command->paintString($this->getIdName($name), 'green')."]"; } @@ -107,8 +106,8 @@ protected function getRelationOptionsRules() { return [ "The {$this->text['relation_name_text']} is {$this->text['name']}", - "There should be field {$this->text['type']} in table {$this->text['model_table']} as {$this->text['related_type_text']}", - "There should be field {$this->text['id']} in table {$this->text['model_table']} as {$this->text['related_id_text']}", + "There should be field {$this->text['type']} in table {$this->text['table']} as {$this->text['related_type_text']}", + "There should be field {$this->text['id']} in table {$this->text['table']} as {$this->text['related_id_text']}", ]; } @@ -119,9 +118,11 @@ protected function getRelationOptionsRules() */ protected function askToUseCustomeOptions() { - $this->options['name'] = $this->command->ask("The {$this->text['relation_name_text']} of relation is will be?", $this->defaultOptions['name']); - $this->options['type'] = $this->command->ask("The {$this->text['related_type_text']} of relation is will be?", $this->getTypeName($this->options['name'])); - $this->options['id'] = $this->command->ask("The {$this->text['related_id_text']} of relation is will be?", $this->getIdName($this->options['name'])); + $this->options = [ + 'name' => $this->command->ask("The {$this->text['relation_name_text']} of relation is will be?", $this->defaultOptions['name']), + 'type' => $this->command->ask("The {$this->text['related_type_text']} of relation is will be?", $this->getTypeName($this->options['name'])), + 'id' => $this->command->ask("The {$this->text['related_id_text']} of relation is will be?", $this->getIdName($this->options['name'])), + ]; } /** @@ -175,8 +176,8 @@ protected function getMethodName($name) { $methodName = $this->getRelationName($name); - if ($this->checkingOptions['name'] !== $methodName) { - return $this->checkingOptions['name']; + if ($this->defaultOptions['name'] !== $methodName) { + return $this->defaultOptions['name']; } return $methodName; diff --git a/src/Flipbox/OrmManager/Relations/MorphToMany.php b/src/Flipbox/OrmManager/Relations/MorphToMany.php index 79142a8..12f344f 100644 --- a/src/Flipbox/OrmManager/Relations/MorphToMany.php +++ b/src/Flipbox/OrmManager/Relations/MorphToMany.php @@ -2,172 +2,14 @@ namespace Flipbox\OrmManager\Relations; -use ReflectionClass; -use Illuminate\Support\Str; - -class MorphToMany extends Relation -{ - /** - * required options to replace - * - * @var array - */ - protected $requiredOptions = ['name']; - - /** - * method suffix - * - * @var string - */ - protected $nameSuffix = 'able'; - - /** - * set default options - * - * @param array $options - * @return void - */ - protected function setDefaultOptions(array $options=[]) - { - $this->text['relation_name_text'] = $this->command->paintString('relation name', 'brown'); - - $refToModel = new ReflectionClass($this->toModel); - - if (! isset($options['name'])) { - $name = $this->getRelationName(strtolower($refToModel->getShortName())); - $name = $this->command->ask("What {$this->text['relation_name_text']} do you use?", $name); - } else { - $name = $options['name']; - } - - $this->defaultOptions = [ - 'name' => $name, - 'pivot_table' => Str::plural($name), - 'foreign_key' => $name.'_id', - 'related_key' => strtolower($refToModel->getShortName()).'_id' - ]; - - $this->checkingOptions = array_merge($this->defaultOptions, $options); - } - - /** - * styling text - * - * @return void - */ - protected function stylingText() - { - $this->text = [ - 'name' => "[".$this->command->paintString($this->checkingOptions['name'], 'green')."]", - 'pivot_table' => "[".$this->command->paintString($this->checkingOptions['pivot_table'], 'green')."]", - 'model_table' => "[".$this->command->paintString($this->model->getTable(), 'green')."]", - 'to_model_table' => "[".$this->command->paintString($this->toModel->getTable(), 'green')."]", - 'foreign_key' => "[".$this->command->paintString($this->checkingOptions['foreign_key'], 'green')."]", - 'related_key' => "[".$this->command->paintString($this->checkingOptions['related_key'], 'green')."]", - 'pivot_text' => $this->command->paintString('pivot table', 'brown'), - 'foreign_text' => $this->command->paintString('foreign key', 'brown'), - 'related_text' => $this->command->paintString('related key', 'brown'), - ]; - } - - /** - * get relation name form table name - * - * @param string $tableName - * @return string - */ - protected function getRelationName($tableName) - { - $name = Str::singular($tableName); - - return $name.$this->nameSuffix; - } - +class MorphToMany extends MorphedByMany +{ /** - * set connected db relation options + * reverse operation * - * @return void + * @var bool */ - protected function setConnectedRelationOptions() - { - $foreignKey = $this->checkingOptions['foreign_key']; - - if (! $this->db->isTableExists($table = $this->checkingOptions['pivot_table'])) { - $table = $this->options['pivot_table'] = $this->command->choice( - "Can't find table {$this->text['pivot_table']} as {$this->text['pivot_text']}, what are you using?", - $this->db->getTables() - ); - - $this->text['pivot_table'] = "[".$this->command->paintString($table, 'green')."]"; - $name = Str::singular($table); - $foreignKey = $name.'_id'; - } - - if (! $this->db->isFieldExists($table, $foreignKey)) { - $this->options['foreign_key'] = $this->command->choice( - "Can't find field {$this->text['foreign_key']} in the table {$this->text['pivot_table']} as {$this->text['foreign_text']} of table {$this->text['model_table']}, choice one!", - $this->getFields($table) - ); - } - - if (! $this->db->isFieldExists($table, $this->checkingOptions['related_key'])) { - $this->options['related_key'] = $this->command->choice( - "Can't find field {$this->text['related_key']} in the table {$this->text['pivot_table']} as {$this->text['related_text']} of table {$this->text['to_model_table']}, choice one!", - $this->getFields($table) - ); - } - } - - /** - * get relation options rules - * - * @return array - */ - protected function getRelationOptionsRules() - { - return [ - "There should be table {$this->text['pivot_table']} as {$this->text['pivot_text']} of both relation", - "There should be field {$this->text['foreign_key']} in the table {$this->text['pivot_table']} as {$this->text['foreign_text']} of table {$this->text['model_table']}", - "There should be field {$this->text['related_key']} in the table {$this->text['pivot_table']} as {$this->text['related_text']} of table {$this->text['to_model_table']}", - ]; - } - - /** - * ask to use custome options - * - * @return void - */ - protected function askToUseCustomeOptions() - { - $this->options['pivot_table'] = $this->command->ask( - "The {$this->text['pivot_text']} of both relation will be?", - $this->defaultOptions['pivot_table'] - ); - - $name = Str::singular($this->options['pivot_table']); - $foreignKey = $name.'_id'; - - $this->options['foreign_key'] = $this->command->ask( - "The {$this->text['foreign_text']} of table {$this->text['model_table']} in the table {$this->text['pivot_table']} will be?", - $foreignKey - ); - - $this->options['related_key'] = $this->command->ask( - "The {$this->text['related_text']} of table {$this->text['to_model_table']} in the table {$this->text['pivot_table']} will be?", - $this->defaultOptions['related_key'] - ); - } - - /** - * get method name form class - * - * @param string $name - * @return string - */ - protected function getMethodName($name) - { - return Str::plural($name); - } + protected $reverse = true; /** * get stub method file diff --git a/src/Flipbox/OrmManager/Relations/MorphedByMany.php b/src/Flipbox/OrmManager/Relations/MorphedByMany.php index 6ef3330..4a71293 100644 --- a/src/Flipbox/OrmManager/Relations/MorphedByMany.php +++ b/src/Flipbox/OrmManager/Relations/MorphedByMany.php @@ -2,14 +2,169 @@ namespace Flipbox\OrmManager\Relations; -class MorphedByMany extends MorphToMany +use ReflectionClass; +use Illuminate\Support\Str; + +class MorphedByMany extends Relation { /** - * reverse operation + * required options to replace + * + * @var array + */ + protected $requiredOptions = ['name']; + + /** + * method suffix + * + * @var string + */ + protected $nameSuffix = 'able'; + + /** + * set default options + * + * @param array $options + * @return void + */ + protected function setDefaultOptions(array $options=[]) + { + $this->text['relation_name_text'] = $this->command->paintString('relation name', 'brown'); + + if (! isset($options['name'])) { + $refModel = new ReflectionClass($this->model); + $name = $this->getRelationName(strtolower($refModel->getShortName())); + $name = $this->command->ask("What {$this->text['relation_name_text']} do you use?", $name); + } else { + $name = $options['name']; + } + + $this->defaultOptions = [ + 'name' => $name, + 'pivot_table' => Str::plural($name), + 'foreign_key' => $name.'_id', + 'related_key' => $this->model->getForeignKey() + ]; + } + + /** + * styling text + * + * @return void + */ + protected function stylingText() + { + $this->text = array_merge($this->text, [ + 'name' => "[".$this->command->paintString($this->defaultOptions['name'], 'green')."]", + 'pivot_table' => "[".$this->command->paintString($this->defaultOptions['pivot_table'], 'green')."]", + 'table' => "[".$this->command->paintString($this->model->getTable(), 'green')."]", + 'to_table' => "[".$this->command->paintString($this->toModel->getTable(), 'green')."]", + 'foreign_key' => "[".$this->command->paintString($this->defaultOptions['foreign_key'], 'green')."]", + 'related_key' => "[".$this->command->paintString($this->defaultOptions['related_key'], 'green')."]", + 'pivot_text' => $this->command->paintString('pivot table', 'brown'), + 'foreign_text' => $this->command->paintString('foreign key', 'brown'), + 'related_text' => $this->command->paintString('related key', 'brown'), + ]); + } + + /** + * get relation name form table name + * + * @param string $tableName + * @return string + */ + protected function getRelationName($tableName) + { + $name = Str::singular($tableName); + + return $name.$this->nameSuffix; + } + + /** + * set connected db relation options + * + * @return void + */ + protected function setConnectedRelationOptions() + { + $foreignKey = $this->defaultOptions['foreign_key']; + + if (! $this->db->isTableExists($table = $this->defaultOptions['pivot_table'])) { + $table = $this->options['pivot_table'] = $this->command->choice( + "Can't find table {$this->text['pivot_table']} as {$this->text['pivot_text']}, what are you using?", + $this->getTables() + ); + + $this->text['pivot_table'] = "[".$this->command->paintString($table, 'green')."]"; + $name = Str::singular($table); + $foreignKey = $name.'_id'; + } + + if (! $this->db->isFieldExists($table, $foreignKey)) { + $this->options['foreign_key'] = $this->command->choice( + "Can't find field {$this->text['foreign_key']} in the table {$this->text['pivot_table']} as {$this->text['foreign_text']} of table {$this->text['to_table']}, choice one!", + $this->getFields($table) + ); + } + + if (! $this->db->isFieldExists($table, $this->defaultOptions['related_key'])) { + $this->options['related_key'] = $this->command->choice( + "Can't find field {$this->text['related_key']} in the table {$this->text['pivot_table']} as {$this->text['related_text']} of table {$this->text['table']}, choice one!", + $this->getFields($table) + ); + } + } + + /** + * get relation options rules + * + * @return array + */ + protected function getRelationOptionsRules() + { + return [ + "There should be table {$this->text['pivot_table']} as {$this->text['pivot_text']} of both relation", + "There should be field {$this->text['foreign_key']} in the table {$this->text['pivot_table']} as {$this->text['foreign_text']} of table {$this->text['to_table']}", + "There should be field {$this->text['related_key']} in the table {$this->text['pivot_table']} as {$this->text['related_text']} of table {$this->text['table']}", + ]; + } + + /** + * ask to use custome options + * + * @return void + */ + protected function askToUseCustomeOptions() + { + $this->options['pivot_table'] = $this->command->ask( + "The {$this->text['pivot_text']} of both relation will be?", + $this->defaultOptions['pivot_table'] + ); + + $name = Str::singular($this->options['pivot_table']); + $foreignKey = $name.'_id'; + + $this->options['foreign_key'] = $this->command->ask( + "The {$this->text['foreign_text']} of table {$this->text['to_table']} in the table {$this->text['pivot_table']} will be?", + $foreignKey + ); + + $this->options['related_key'] = $this->command->ask( + "The {$this->text['related_text']} of table {$this->text['table']} in the table {$this->text['pivot_table']} will be?", + $this->defaultOptions['related_key'] + ); + } + + /** + * get method name form class * - * @var bool + * @param string $name + * @return string */ - protected $reverse = true; + protected function getMethodName($name) + { + return Str::plural($name); + } /** * get stub method file diff --git a/src/Flipbox/OrmManager/Relations/Relation.php b/src/Flipbox/OrmManager/Relations/Relation.php index 879a9b6..1e4a8fe 100644 --- a/src/Flipbox/OrmManager/Relations/Relation.php +++ b/src/Flipbox/OrmManager/Relations/Relation.php @@ -2,7 +2,6 @@ namespace Flipbox\OrmManager\Relations; -use Exception; use ReflectionClass; use Illuminate\Support\Str; use Illuminate\Console\Command; @@ -33,7 +32,7 @@ abstract class Relation * * @var DatabaseConnection */ - protected $database; + protected $db; /** * model that want to connect to @@ -70,13 +69,6 @@ abstract class Relation */ protected $requiredOptions = []; - /** - * options that will be check - * - * @var array - */ - protected $checkingOptions = []; - /** * new line * @@ -104,14 +96,14 @@ abstract class Relation * @param Command $command * @param ModelManager $manager * @param Model $model - * @param Model $toModel + * @param mixed $toModel * @param array $options * @return void */ public function __construct(Command $command, ModelManager $manager, Model $model, - Model $toModel=null, + $toModel=null, array $options=[]) { $this->command = $command; @@ -123,7 +115,7 @@ public function __construct(Command $command, $this->showCaptionProcess($model, $toModel); $this->setDefaultOptions($options); $this->stylingText(); - $this->setRelationOptions(); + $this->setRelationOptions($options); } /** @@ -152,19 +144,22 @@ protected function showCaptionProcess(Model $model, Model $toModel=null) /** * set relation option required * + * @param array $options * @return void */ - protected function setRelationOptions() + protected function setRelationOptions(array $options = []) { - if ($this->db->isConnected()) { - if (! $this->db->isTableExists($this->model->getTable())) { - throw new TableNotExists($this->model->getTable(), $refModel->getShortName()); - } + if (! $this->db->isTableExists($this->model->getTable())) { + throw new TableNotExists($this->model->getTable(), $refModel->getShortName()); + } - if (! is_null($this->toModel) AND ! $this->db->isTableExists($this->toModel->getTable())) { - throw new TableNotExists($this->toModel->getTable(), $refToModel->getShortName()); - } + if (! is_null($this->toModel) AND ! $this->db->isTableExists($this->toModel->getTable())) { + throw new TableNotExists($this->toModel->getTable(), $refToModel->getShortName()); + } + if (count($options) > 0) { + $this->options = $options; + } elseif ($this->db->isConnected()) { $this->setConnectedRelationOptions(); } else { $this->setNotConnectedRelationOptions(); @@ -185,7 +180,7 @@ public function createMethod() $modelCode = $this->clearDefaultModelContent( file_get_contents($refModel->getFileName()) ); - + $this->writeMethodToFile($refModel->getFileName(), $modelCode, $methodCode); } @@ -283,8 +278,6 @@ protected function getMethodName($name) */ protected function applyOptions($stub) { - $this->mergeUnchechingOptions(); - $replaced = false; foreach (array_reverse($this->defaultOptions) as $key => $option) { @@ -321,20 +314,6 @@ protected function applyOptions($stub) return $stub; } - /** - * merger unchecking options to options - * - * @return void - */ - protected function mergeUnchechingOptions() - { - foreach ($this->checkingOptions as $key => $option) { - if ($this->checkingOptions[$key] !== $this->defaultOptions[$key]) { - $this->options[$key] = $option; - } - } - } - /** * clear default laravel model generator content * @@ -393,6 +372,16 @@ protected function setNotConnectedRelationOptions() } } + /** + * get model tables + * + * @return array + */ + protected function getTables() + { + return $this->db->getTables(); + } + /** * get model fileds * diff --git a/src/Flipbox/OrmManager/Stubs/belongsToMany.stub b/src/Flipbox/OrmManager/Stubs/belongsToMany.stub index d9d6d00..42f8b8c 100644 --- a/src/Flipbox/OrmManager/Stubs/belongsToMany.stub +++ b/src/Flipbox/OrmManager/Stubs/belongsToMany.stub @@ -3,5 +3,5 @@ */ public function DummyMethodName() { - return $this->belongsToMany(DummyToModel::class, 'pivot_table', 'foreign_key_1', 'foreign_key_2'); + return $this->belongsToMany(DummyToModel::class, 'pivot_table', 'foreign_key', 'related_key', 'relation'); } \ No newline at end of file