蛋糕php 2通过型号保存数据

发布于 2025-01-17 20:22:57 字数 1377 浏览 6 评论 0原文

我的项目的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟花肆意 2025-01-24 20:22:57

您的类仅实现一个接口,没有进一步使用 $uses 属性的逻辑。

$uses 属性只能被控制器直接识别,如果您需要在其他地方加载模型,那么您必须使用 ClassRegistry::init(),类似:

App::uses('ClassRegistry', 'Utility');

// ...

$QueueManagerJob = ClassRegistry::init('QueueManagerJob');

另请注意,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 use ClassRegistry::init(), like:

App::uses('ClassRegistry', 'Utility');

// ...

$QueueManagerJob = ClassRegistry::init('QueueManagerJob');

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文