蛋糕php 2通过型号保存数据
我的项目的lib/event
目录和一个称为queuemanagerjob
的模型中有一个事件侦听器。
我的应用程序在某个时候派遣了一个事件,我想通过模型保存进入数据库的条目。派遣我的事件时,它将在侦听器中运行Store
方法,在这里我引用了我的模型并试图保存它。
我已经通过$ use
数组绘制了模型,但是我会收到以下错误:
在null上调用成员函数set()
<?php
App::uses('CakeEventListener', 'Event', 'CakeLog');
class DispatchJobListener implements CakeEventListener {
public $uses = array('QueueManagerJob');
public function implementedEvents() {
return array(
'QueueManagerModule.QueueManagerJob.dispatchJob' => 'store'
);
}
/**
* Store the data
*/
public function store($event) {
$event->stopPropagation();
$job = [
'queue' => 'default',
'payload' => json_encode([]),
'attempts' => 0,
'reserved_at' => null,
'available_at' => date('Y-m-d H:i:s'),
'created_at' => date('Y-m-d H:i:s')
];
$this->QueueManagerJob->set($job);
// invalid
if (!$this->QueueManagerJob->validates()) {
// $this->QueueManagerJob->validationErrors;
// ...
}
// save the job
$this->QueueManagerJob->save($job);
}
}
I've got an event listener in my project's Lib/Event
directory and a model called QueueManagerJob
.
My application dispatches an event at some point and I'd like to save an entry into my database through my model. When my event is dispatched, it runs a store
method in the listener and it's here where I'm referencing my model and am trying to save it.
I've pulled the model in via the $uses
array but I'm getting the following error:
Call to a member function set() on null
<?php
App::uses('CakeEventListener', 'Event', 'CakeLog');
class DispatchJobListener implements CakeEventListener {
public $uses = array('QueueManagerJob');
public function implementedEvents() {
return array(
'QueueManagerModule.QueueManagerJob.dispatchJob' => 'store'
);
}
/**
* Store the data
*/
public function store($event) {
$event->stopPropagation();
$job = [
'queue' => 'default',
'payload' => json_encode([]),
'attempts' => 0,
'reserved_at' => null,
'available_at' => date('Y-m-d H:i:s'),
'created_at' => date('Y-m-d H:i:s')
];
$this->QueueManagerJob->set($job);
// invalid
if (!$this->QueueManagerJob->validates()) {
// $this->QueueManagerJob->validationErrors;
// ...
}
// save the job
$this->QueueManagerJob->save($job);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的类仅实现一个接口,没有进一步使用
$uses
属性的逻辑。$uses
属性只能被控制器直接识别,如果您需要在其他地方加载模型,那么您必须使用ClassRegistry::init()
,类似:另请注意,
App::uses()
只接受两个参数,类名和包名,示例中的第三个参数不会执行任何操作!Your class only implements an interface, there is no further logic that would make use of the
$uses
property.The
$uses
property is only recognized out of the box by controllers, if you need to load models in other places, then you have to useClassRegistry::init()
, like:Also note that
App::uses()
only accepts two arguments, the class name and the package name, the third argument in your example won't do anything!