Skip to content

Commit 8d8c412

Browse files
committed
Update ActiveRecord.php
1 parent 522d1ce commit 8d8c412

1 file changed

Lines changed: 14 additions & 25 deletions

File tree

src/ActiveRecord.php

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@
77
namespace Evas\Orm;
88

99
use Evas\Base\App;
10+
use Evas\Base\Help\HooksTrait;
1011
use Evas\Db\Interfaces\DatabaseInterface;
1112
use Evas\Db\Interfaces\QueryBuilderInterface;
1213
use Evas\Db\Table;
1314
use Evas\Orm\Exceptions\LastInsertIdUndefinedException;
1415

1516
abstract class ActiveRecord
1617
{
18+
// подключаем поддержку произвольных хуков в наследуемых классах
19+
use HooksTrait;
20+
1721
/** @var string имя соединения с базой данных*/
1822
public static $dbname;
1923
/** @var string имя соединения с базой данных только для записи */
@@ -184,21 +188,6 @@ public function getUpdatedProperties(): array
184188
}
185189
}
186190

187-
/**
188-
* Вызов метода при наличии. Для хуков событий.
189-
* @param string имя метода
190-
* @param mixed аргумент метода
191-
*/
192-
private function callMethodIfExists(string $methodName, ...$methodArgs)
193-
{
194-
if (defined('EVAS_DEBUG') && true == EVAS_DEBUG) {
195-
echo $methodName . ('cli' == PHP_SAPI ? "\n" : '<br>');
196-
}
197-
if (method_exists($this, $methodName)) {
198-
call_user_func_array([$this, $methodName], $methodArgs);
199-
}
200-
}
201-
202191

203192
/**
204193
* Сохранение записи.
@@ -209,27 +198,27 @@ public function save()
209198
{
210199
$props = $this->getUpdatedProperties();
211200
if (empty($props)) {
212-
$this->callMethodIfExists('nothingSave');
201+
$this->hook('nothingSave');
213202
return $this;
214203
}
215204

216-
$this->callMethodIfExists('beforeSave');
205+
$this->hook('beforeSave');
217206
$pk = static::primaryKey();
218207
if (empty($this->$pk)) {
219-
$this->callMethodIfExists('beforeInsert');
208+
$this->hook('beforeInsert');
220209
static::getDb(true)->insert(static::tableName(), $props);
221210
$this->$pk = static::lastInsertId();
222211
if (empty($this->$pk)) {
223212
throw new LastInsertIdUndefinedException();
224213
}
225-
$this->callMethodIfExists('afterInsert');
214+
$this->hook('afterInsert');
226215
} else {
227-
$this->callMethodIfExists('beforeUpdate');
216+
$this->hook('beforeUpdate');
228217
static::getDb(true)->update(static::tableName(), $props)
229218
->where("$pk = ?", [$this->$pk])->one();
230-
$this->callMethodIfExists('afterUpdate');
219+
$this->hook('afterUpdate');
231220
}
232-
$this->callMethodIfExists('afterSave');
221+
$this->hook('afterSave');
233222
return static::getDb()->identityMapUpdate($this, $pk);
234223
}
235224

@@ -240,17 +229,17 @@ public function delete()
240229
{
241230
$pk = static::primaryKey();
242231
if (empty($this->$pk)) {
243-
$this->callMethodIfExists('nothingDelete');
232+
$this->hook('nothingDelete');
244233
return $this;
245234
}
246-
$this->callMethodIfExists('beforeDelete');
235+
$this->hook('beforeDelete');
247236
$qr = static::getDb(true)->delete(static::tableName())
248237
->where("$pk = ?", [$this->$pk])->one();
249238
if (0 < $qr->rowCount()) {
250239
static::getDb()->identityMapUnset($this, $pk);
251240
$this->id = null;
252241
}
253-
$this->callMethodIfExists('afterDelete', $qr->rowCount());
242+
$this->hook('afterDelete', $qr->rowCount());
254243
return $this;
255244
}
256245

0 commit comments

Comments
 (0)