Skip to content

Commit

Permalink
[FIX] both relation and relation process
Browse files Browse the repository at this point in the history
  • Loading branch information
dedensaka committed Feb 27, 2017
1 parent 1f974fa commit d239e67
Show file tree
Hide file tree
Showing 18 changed files with 810 additions and 628 deletions.
138 changes: 22 additions & 116 deletions src/Flipbox/OrmManager/BothRelations/BothRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
}
122 changes: 98 additions & 24 deletions src/Flipbox/OrmManager/BothRelations/ManyToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Loading

0 comments on commit d239e67

Please sign in to comment.