Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create new config relation #268

Open
wants to merge 3 commits into
base: v1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 30 additions & 21 deletions config/models.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,7 @@
| 'billing_invoices' => 'Invoice',
*/

'model_names' => [

],
'model_names' => [],

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -375,6 +373,19 @@
'relation_name_strategy' => 'related',
// 'relation_name_strategy' => 'foreign_key',

'relation' => [
'options' => [
/*
| 'true' return $this->belongsTo(User::class, 'user_id', 'id'); (post.user_id --> user.id)
| return $this->hasMany(Comment::class, 'post_id', 'id'); (comment.post_id --> post.id)
|
| 'false' return $this->belongsTo(User::class); (post.user_id --> user.id)
| return $this->hasMany(Comment::class); (comment.post_id --> post.id)
*/
'show_key' => false, // default: false
]
],

/*
|--------------------------------------------------------------------------
| Determines need or not to generate constants with properties names like
Expand Down Expand Up @@ -413,9 +424,7 @@
| You can enable pluralization for certain tables
|
*/
'override_pluralize_for' => [

],
'override_pluralize_for' => [],
/*
|--------------------------------------------------------------------------
| Move $fillable property to base files
Expand Down Expand Up @@ -493,18 +502,18 @@
|
*/

// 'connections' => [
// 'read_only_external' => [
// 'parent' => \App\Models\ReadOnlyModel::class,
// 'connection' => true,
// 'users' => [
// 'connection' => false,
// ],
// 'my_other_database' => [
// 'password_resets' => [
// 'connection' => false,
// ]
// ]
// ],
// ],
];
// 'connections' => [
// 'read_only_external' => [
// 'parent' => \App\Models\ReadOnlyModel::class,
// 'connection' => true,
// 'users' => [
// 'connection' => false,
// ],
// 'my_other_database' => [
// 'password_resets' => [
// 'connection' => false,
// ]
// ]
// ],
// ],
];
34 changes: 21 additions & 13 deletions src/Coders/Model/Relations/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,20 @@ public function body()
{
$body = 'return $this->belongsTo(';

$body .= $this->related->getQualifiedUserClassName().'::class';
$body .= $this->related->getQualifiedUserClassName() . '::class';

if ($this->needsForeignKey()) {
$foreignKey = $this->parent->usesPropertyConstants()
? $this->parent->getQualifiedUserClassName().'::'.strtoupper($this->foreignKey())
? $this->parent->getQualifiedUserClassName() . '::' . strtoupper($this->foreignKey())
: $this->foreignKey();
$body .= ', '.Dumper::export($foreignKey);
$body .= ', ' . Dumper::export($foreignKey);
}

if ($this->needsOtherKey()) {
$otherKey = $this->related->usesPropertyConstants()
? $this->related->getQualifiedUserClassName().'::'.strtoupper($this->otherKey())
? $this->related->getQualifiedUserClassName() . '::' . strtoupper($this->otherKey())
: $this->otherKey();
$body .= ', '.Dumper::export($otherKey);
$body .= ', ' . Dumper::export($otherKey);
}

$body .= ')';
Expand All @@ -100,10 +100,10 @@ public function body()
// or a composite unique key. Otherwise it should be a has-many relationship which is not
// supported at the moment. @todo: Improve relationship resolution.
foreach ($this->command->references as $index => $column) {
$body .= "\n\t\t\t\t\t->where(".
Dumper::export($this->qualifiedOtherKey($index)).
", '=', ".
Dumper::export($this->qualifiedForeignKey($index)).
$body .= "\n\t\t\t\t\t->where(" .
Dumper::export($this->qualifiedOtherKey($index)) .
", '=', " .
Dumper::export($this->qualifiedForeignKey($index)) .
')';
}
}
Expand Down Expand Up @@ -140,7 +140,11 @@ public function returnType()
*/
protected function needsForeignKey()
{
$defaultForeignKey = $this->related->getRecordName().'_id';
if ($this->parent->config('relation.options.show_key')) {
return true;
}

$defaultForeignKey = $this->related->getRecordName() . '_id';

return $defaultForeignKey != $this->foreignKey() || $this->needsOtherKey();
}
Expand All @@ -162,14 +166,18 @@ protected function foreignKey($index = 0)
*/
protected function qualifiedForeignKey($index = 0)
{
return $this->parent->getTable().'.'.$this->foreignKey($index);
return $this->parent->getTable() . '.' . $this->foreignKey($index);
}

/**
* @return bool
*/
protected function needsOtherKey()
{
if ($this->parent->config('relation.options.show_key')) {
return true;
}

$defaultOtherKey = $this->related->getPrimaryKey();

return $defaultOtherKey != $this->otherKey();
Expand All @@ -192,7 +200,7 @@ protected function otherKey($index = 0)
*/
protected function qualifiedOtherKey($index = 0)
{
return $this->related->getTable().'.'.$this->otherKey($index);
return $this->related->getTable() . '.' . $this->otherKey($index);
}

/**
Expand All @@ -212,4 +220,4 @@ private function isNullable()
{
return (bool) $this->parent->getBlueprint()->column($this->foreignKey())->get('nullable');
}
}
}
24 changes: 12 additions & 12 deletions src/Coders/Model/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function __construct(
*/
public function hint()
{
return '\\'.Collection::class.'|'.$this->reference->getQualifiedUserClassName().'[]';
return '\\' . Collection::class . '|' . $this->reference->getQualifiedUserClassName() . '[]';
}

/**
Expand Down Expand Up @@ -99,32 +99,32 @@ public function body()
{
$body = 'return $this->belongsToMany(';

$body .= $this->reference->getQualifiedUserClassName().'::class';
$body .= $this->reference->getQualifiedUserClassName() . '::class';

if ($this->needsPivotTable()) {
$body .= ', '.Dumper::export($this->pivotTable());
$body .= ', ' . Dumper::export($this->pivotTable());
}

if ($this->needsForeignKey()) {
$foreignKey = $this->parent->usesPropertyConstants()
? $this->reference->getQualifiedUserClassName().'::'.strtoupper($this->foreignKey())
? $this->reference->getQualifiedUserClassName() . '::' . strtoupper($this->foreignKey())
: $this->foreignKey();
$body .= ', '.Dumper::export($foreignKey);
$body .= ', ' . Dumper::export($foreignKey);
}

if ($this->needsOtherKey()) {
$otherKey = $this->reference->usesPropertyConstants()
? $this->reference->getQualifiedUserClassName().'::'.strtoupper($this->otherKey())
? $this->reference->getQualifiedUserClassName() . '::' . strtoupper($this->otherKey())
: $this->otherKey();
$body .= ', '.Dumper::export($otherKey);
$body .= ', ' . Dumper::export($otherKey);
}

$body .= ')';

$fields = $this->getPivotFields();

if (! empty($fields)) {
$body .= "\n\t\t\t\t\t->withPivot(".$this->parametrize($fields).')';
if (!empty($fields)) {
$body .= "\n\t\t\t\t\t->withPivot(" . $this->parametrize($fields) . ')';
}

if ($this->pivot->usesTimestamps()) {
Expand Down Expand Up @@ -173,7 +173,7 @@ protected function pivotTable()
*/
protected function needsForeignKey()
{
$defaultForeignKey = $this->parentRecordName().'_id';
$defaultForeignKey = $this->parentRecordName() . '_id';

return $this->foreignKey() != $defaultForeignKey || $this->needsOtherKey();
}
Expand All @@ -191,7 +191,7 @@ protected function foreignKey()
*/
protected function needsOtherKey()
{
$defaultOtherKey = $this->referenceRecordName().'_id';
$defaultOtherKey = $this->referenceRecordName() . '_id';

return $this->otherKey() != $defaultOtherKey;
}
Expand Down Expand Up @@ -241,7 +241,7 @@ private function parametrize($fields = [])
{
return (string) implode(', ', array_map(function ($field) {
$field = $this->reference->usesPropertyConstants()
? $this->pivot->getQualifiedUserClassName().'::'.strtoupper($field)
? $this->pivot->getQualifiedUserClassName() . '::' . strtoupper($field)
: $field;

return Dumper::export($field);
Expand Down
4 changes: 2 additions & 2 deletions src/Coders/Model/Relations/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class HasMany extends HasOneOrMany
*/
public function hint()
{
return '\\'.Collection::class.'|'.$this->related->getQualifiedUserClassName().'[]';
return '\\' . Collection::class . '|' . $this->related->getQualifiedUserClassName() . '[]';
}

/**
Expand Down Expand Up @@ -66,4 +66,4 @@ public function returnType()
{
return \Illuminate\Database\Eloquent\Relations\HasMany::class;
}
}
}
2 changes: 1 addition & 1 deletion src/Coders/Model/Relations/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ public function returnType()
{
return \Illuminate\Database\Eloquent\Relations\HasOne::class;
}
}
}
24 changes: 16 additions & 8 deletions src/Coders/Model/Relations/HasOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,22 @@ abstract public function name();
*/
public function body()
{
$body = 'return $this->'.$this->method().'(';
$body = 'return $this->' . $this->method() . '(';

$body .= $this->related->getQualifiedUserClassName().'::class';
$body .= $this->related->getQualifiedUserClassName() . '::class';

if ($this->needsForeignKey()) {
$foreignKey = $this->parent->usesPropertyConstants()
? $this->related->getQualifiedUserClassName().'::'.strtoupper($this->foreignKey())
? $this->related->getQualifiedUserClassName() . '::' . strtoupper($this->foreignKey())
: $this->foreignKey();
$body .= ', '.Dumper::export($foreignKey);
$body .= ', ' . Dumper::export($foreignKey);
}

if ($this->needsLocalKey()) {
$localKey = $this->related->usesPropertyConstants()
? $this->related->getQualifiedUserClassName().'::'.strtoupper($this->localKey())
? $this->related->getQualifiedUserClassName() . '::' . strtoupper($this->localKey())
: $this->localKey();
$body .= ', '.Dumper::export($localKey);
$body .= ', ' . Dumper::export($localKey);
}

$body .= ');';
Expand All @@ -91,7 +91,11 @@ abstract protected function method();
*/
protected function needsForeignKey()
{
$defaultForeignKey = $this->parent->getRecordName().'_id';
if ($this->parent->config('relation.options.show_key')) {
return true;
}

$defaultForeignKey = $this->parent->getRecordName() . '_id';

return $defaultForeignKey != $this->foreignKey() || $this->needsLocalKey();
}
Expand All @@ -109,6 +113,10 @@ protected function foreignKey()
*/
protected function needsLocalKey()
{
if ($this->parent->config('relation.options.show_key')) {
return true;
}

return $this->parent->getPrimaryKey() != $this->localKey();
}

Expand All @@ -119,4 +127,4 @@ protected function localKey()
{
return $this->command->references[0];
}
}
}