hmm I'll need to learn more about custom behaviour.
Is there a way to retrieve dynamically the aliasOfSlaves ?
Or I need to list all the lalias that couls be deleted ?
because from what I already know, create a behaviour is equivalent to what I did with my trait SoftDelete
<?php
namespace App\Models\Traits;
trait ModelSoftDeletableTrait
{
/**
* @var bool
*/
protected $deleted;
/**
* @return bool
*/
public function isDeleted()
{
return $this->deleted;
}
/**
* @param bool $deleted
*/
public function setDeleted($deleted)
{
$this->deleted = $deleted;
}
public function beforeDelete()
{
//We update the model
$this->setDeleted(true);
$this->save();
//TODO find a way to retrieve relation hasMany and launch a delete for the related records
//return false to stop the deletion
return false;
}
}
Edit : Talking about behaviour, I tried to use the timestampable behaviour (instead of setting the date manually on beforeValidationOnUpdate event) but it doesn't work.
here is my behaviour
$this->addBehavior(
new Timestampable(
[
'beforeValidationOnCreate' => [
'field' => 'createdAt',
'format' => function () {
$datetime = new \DateTime();
return $datetime->format(\DateTime::ISO8601);
}
],
'beforeValidationOnUpdate' => [
'field' => 'updatedAt',
'format' => function () {
$datetime = new \DateTime();
return $datetime->format(\DateTime::ISO8601);
}
]
]
)
);
in the initialize of my model (in the model directly, not in a trait or another class) but it doesn't work, my field updatedAt is updated but to 0000-00-00 00:00:00
, not the current date.
and for some reasons I can't do a dump in the function to check the datetime value.