Answer the question
In order to leave comments, you need to log in
What is the best way to implement cascading hiding in nesting?
Situation : Suppose there is the following nesting in a service: Category->Groups->Services.
Answer the question
In order to leave comments, you need to log in
I did not check for errors, but I think you can do something like this:
interface SoftDeletableInterface
{
public function softDelete() : bool;
public function getChildren() : array;
}
abstract class SoftDeletableModel extends ActiveRecord implements SoftDeletableInterface
{
public function getChildren() : array
{
return [];
}
final public function softDelete(bool $inTransaction = false) : bool
{
if ($inTransaction) {
$this->softDeleteInternal();
return true;
}
$transaction = \Yii::$app->db->beginTransaction();
try {
$this->softDeleteInternal();
$transaction->commit();
return true;
} catch (\Exception $exception) {
$transaction->rollBack();
return false;
}
}
private function softDeleteInternal() : void
{
$this->updateAttributes(['is_deleted' => true]);
foreach ($this->getChildren() as $children) {
foreach ((array)$children as $child) {
$child->softDelete(true);
}
}
}
}
class Category extends SoftDeletableModel
{
public function getChildren() : array
{
return $this->groups;
}
public function getGroups()
{
return $this->hasMany(Group::class, ['category_id' => 'id']);
}
}
class Group extends SoftDeletableModel
{
public function getChildren() : array
{
return [$this->services, $this->users] /* array of array */;
}
public function getServices()
{
return $this->hasMany(Service::class, ['group_id' => 'id']);
}
public function getUsers()
{
return $this->hasMany(User::class, ['group_id' => 'id']);
}
}
class Service extends SoftDeletableModel
{
}
class User extends SoftDeletableModel
{
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question