Skip to content
Merged
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
35 changes: 31 additions & 4 deletions src/SoftCascade.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,15 @@ protected function relations($model, $foreignKeyIds)
$foreignKeyIdsUse = $foreignKeyIds;

//Many to many relations need to get related ids and related local key
if (get_class($modelRelation) == 'Illuminate\Database\Eloquent\Relations\BelongsToMany') {
extract($this->gettBelongsToManyData($modelRelation, $foreignKeyIds));
$classModelRelation = get_class($modelRelation);
if ($classModelRelation == 'Illuminate\Database\Eloquent\Relations\BelongsToMany') {
extract($this->getBelongsToManyData($modelRelation, $foreignKeyIds));
} else if ($classModelRelation == 'Illuminate\Database\Eloquent\Relations\MorphMany') {
extract($this->getMorphManyData($modelRelation, $foreignKeyIds));
}

$affectedRowsOnExecute = $this->affectedRowsOnExecute($modelRelation, $foreignKeyUse, $foreignKeyIdsUse);

if ($action === 'restrict' && $affectedRowsOnExecute > 0) {
DB::rollBack(); //Rollback the transaction before throw exception
throw (new SoftCascadeRestrictedException)->setModel(get_class($modelRelation->getModel()), $foreignKeyUse, $foreignKeyIdsUse->toArray());
Expand All @@ -112,7 +115,7 @@ protected function relations($model, $foreignKeyIds)
* @param array $foreignKeyIds
* @return array
*/
protected function gettBelongsToManyData($relation, $foreignKeyIds)
protected function getBelongsToManyData($relation, $foreignKeyIds)
{
$relationConnectionName = $relation->getConnection()->getName();
$relationTable = $relation->getTable();
Expand All @@ -133,6 +136,30 @@ protected function gettBelongsToManyData($relation, $foreignKeyIds)
];
}

/**
* Get morph many related key ids and key use
*
* @param Illuminate\Database\Eloquent\Relations\Relation $relation
* @param array $foreignKeyIds
* @return array
*/
protected function getMorphManyData($relation, $foreignKeyIds)
{
$relationConnectionName = $relation->getConnection()->getName();
$relatedClass = $relation->getRelated();
$foreignKeyUse = $relatedClass->getKeyName();
$foreignKeyIdsUse = $relatedClass::where($relation->getMorphType(), $relation->getMorphClass())
->whereIn($relation->getQualifiedForeignKeyName(), $foreignKeyIds)
->select($foreignKeyUse)
->get()->toArray();
$foreignKeyIdsUse = array_column($foreignKeyIdsUse, $foreignKeyUse);

return [
'foreignKeyIdsUse' => collect($foreignKeyIdsUse),
'foreignKeyUse' => $relation->getRelated()->getKeyName()
];
}

/**
* Execute delete, or restore.
*
Expand Down
22 changes: 22 additions & 0 deletions tests/App/Comment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Askedio\Tests\App;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
use \Illuminate\Database\Eloquent\SoftDeletes;

protected $table = 'comments';

protected $fillable = ['body'];

/**
* Get all of the owning commentable models.
*/
public function commentable()
{
return $this->morphTo();
}
}
26 changes: 26 additions & 0 deletions tests/App/Post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Askedio\Tests\App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
use \Illuminate\Database\Eloquent\SoftDeletes;
use \Askedio\SoftCascade\Traits\SoftCascadeTrait;
use \Illuminate\Database\Eloquent\SoftDeletes;

protected $table = 'posts';

protected $fillable = ['title', 'body'];

protected $softCascade = ['comments'];

/**
* Get all of the post's comments.
*/
public function comments()
{
return $this->morphMany('Askedio\Tests\App\Comment', 'commentable');
}
}
26 changes: 26 additions & 0 deletions tests/App/Video.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Askedio\Tests\App;

use Illuminate\Database\Eloquent\Model;

class Video extends Model
{
use \Illuminate\Database\Eloquent\SoftDeletes;
use \Askedio\SoftCascade\Traits\SoftCascadeTrait;
use \Illuminate\Database\Eloquent\SoftDeletes;

protected $table = 'videos';

protected $fillable = ['title', 'url'];

protected $softCascade = ['comments'];

/**
* Get all of the video's comments.
*/
public function comments()
{
return $this->morphMany('Askedio\Tests\App\Comment', 'commentable');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
DB::connection()->getSchemaBuilder()->create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title', 100);
$table->text('body');
$table->timestamps();
$table->softDeletes();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::connection()->getSchemaBuilder()->dropIfExists('posts');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateVideosTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
DB::connection()->getSchemaBuilder()->create('videos', function (Blueprint $table) {
$table->increments('id');
$table->string('title', 100);
$table->string('url', 255);
$table->timestamps();
$table->softDeletes();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::connection()->getSchemaBuilder()->dropIfExists('videos');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateCommentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
DB::connection()->getSchemaBuilder()->create('comments', function (Blueprint $table) {
$table->increments('id');
$table->string('body', 255);
$table->morphs('commentable');
$table->timestamps();
$table->softDeletes();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::connection()->getSchemaBuilder()->dropIfExists('comments');
}
}
32 changes: 32 additions & 0 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
use Askedio\Tests\App\BadRelation;
use Askedio\Tests\App\BadRelationAction;
use Askedio\Tests\App\BadRelationB;
use Askedio\Tests\App\Comment;
use Askedio\Tests\App\Languages;
use Askedio\Tests\App\Post;
use Askedio\Tests\App\Profiles;
use Askedio\Tests\App\User;
use Askedio\Tests\App\Video;

/**
* TO-DO: Need better testing.
Expand Down Expand Up @@ -42,6 +45,35 @@ private function createUserRaw()
return $user;
}

private function createCommentRaw()
{
$post = Post::create([
'title' => 'Post',
'body' => 'Post chulo'
])->comments()->saveMany([
new Comment(['body' => 'comentario post']),
]);

$video = Video::create([
'title' => 'Video',
'url' => 'Video chulo'
])->comments()->saveMany([
new Comment(['body' => 'comentario video']),
]);

return $this;
}

public function testPolymorphicRelation()
{
$this->createCommentRaw();

Post::first()->delete();

$this->assertDatabaseHas('videos', ['deleted_at' => null]);
$this->assertDatabaseMissing('posts', ['deleted_at' => null]);
}

public function testBadRelation()
{
$this->createUserRaw();
Expand Down
32 changes: 32 additions & 0 deletions tests/LumenIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
use Askedio\Tests\App\BadRelation;
use Askedio\Tests\App\BadRelationAction;
use Askedio\Tests\App\BadRelationB;
use Askedio\Tests\App\Comment;
use Askedio\Tests\App\Languages;
use Askedio\Tests\App\Post;
use Askedio\Tests\App\Profiles;
use Askedio\Tests\App\User;
use Askedio\Tests\App\Video;

/**
* TO-DO: Need better testing.
Expand Down Expand Up @@ -46,6 +49,35 @@ private function createUserRaw()
return $user;
}

private function createCommentRaw()
{
$post = Post::create([
'title' => 'Post',
'body' => 'Post chulo'
])->comments()->saveMany([
new Comment(['body' => 'comentario post']),
]);

$video = Video::create([
'title' => 'Video',
'url' => 'Video chulo'
])->comments()->saveMany([
new Comment(['body' => 'comentario video']),
]);

return $this;
}

public function testPolymorphicRelation()
{
$this->createCommentRaw();

Post::first()->delete();

$this->assertDatabaseHas('videos', ['deleted_at' => null]);
$this->assertDatabaseMissing('posts', ['deleted_at' => null]);
}

public function testBadRelation()
{
$this->createUserRaw();
Expand Down