如何使用CakePHP 3.3存儲全部$($ data)的所有$數據?
我用於實現小日誌代碼在AppModel :: beforeSave(該$數據轉換爲JSON和將其存儲在VARCHAR,用戶名和日期時間)($數據)(蛋糕1.2)
更新:
我試着使用EventListenerInterface:
namespace App\Model\Event;
use Cake\Event\EventListenerInterface;
use Cake\ORM\TableRegistry;
class LogModel implements EventListenerInterface
{
public function implementedEvents()
{
return [
'Model.beforeSave' => 'logSave'
];
}
public function logSave($event, $data = null, $options = null)
{
$eventTable = TableRegistry::get('Event');
$eventEntity = $eventTable->newEntity();
$eventEntity->event = json_encode($event);
$eventEntity->data = json_encode($data);
$eventEntity->className = get_class($event->__get('subject'));
//var_dump($eventEntity->className);exit();
$eventTable->save($eventEntity);
}
}
bootstrap.php中:
//event global
use Cake\Event\EventManager;
use App\Model\Event\LogModel;
$logModel = new LogModel();
EventManager::instance()->on($logModel);
但是這導致了無限遞歸調用,因爲我需要保存(記錄)所有保存的數據。有沒有辦法避免這種情況?
究竟有什麼用CakePHP的3做類似的問題呢?目前您的問題不包含任何實際問題。 – ndm
Cake3中的「beforeSave等價」與所有表(或實體)在哪裏? – celsowm