【问题标题】:CodeIgniter task scheduler taking the function to the task schedulerCodeIgniter 任务调度器将功能带到任务调度器
【发布时间】:2016-09-17 18:43:09
【问题描述】:

我是 CRON 工作的新手,所以我正在练习它;对于初学者,我正在做一个 CRON 工作,使用 CodeIgniter 和 PHP 添加用户, 这是我的模型:

    <?php 
    class Cron_Model extends CI_Model{

    public function adduser($firstname,$lastname){
    $data = array(
        'firstname' => $firstname,
        'lastname' => $lastname
        );
    $query = $this->db->insert('user_account',$data);
    return $query;
       }

    }

AND : 这是我的控制器:

    <?php
    class Cron_Controller extends CI_Controller{

    public function __construct(){
    parent::__construct();

    $this->load->database();
    $this->load->model('Cron_Model');

    // this controller can only be called from the command line
    if (!$this->input->is_cli_request()) show_error('Direct access is not allowed');

}

public function AddAUser(){
    $fname = "JUNCEL";
    $lname = "CARREON";

    $this->Cron_Model->adduser($fname,$lname);
}

}

  ?>

我将把这个添加到数据库中,即使名字和姓氏相同也没关系,这只是一个试验工作。

所以现在我正在尝试将函数 AddAUser() 调用到任务调度程序,

我试过这个东西:在任务计划程序上浏览它

  C:\xampp\htdocs\post\application\controllers\cron_controller.php

然后在Add arguments(optional):我把AddAUser,所以基本上变成了这样:

 C:\xampp\htdocs\post\application\controllers\cron_controller.php AddAUser

然后我尝试运行它,但我没有在数据库中看到任何内容!发生了什么?

【问题讨论】:

    标签: php codeigniter cron


    【解决方案1】:

    像这样更改您的模型代码:

    <?php 
    class cronjob extends CI_Model{
    
    public function __construct()
    {
        //load and config db
        $this->load->database();
        parent::__construct();
    }
    public function adduser($firstname,$lastname){
    $data = array(
        'firstname' => $firstname,
        'lastname' => $lastname
        );
    $query = $this->db->insert('user_account',$data);
    return $query;
       }
    
    }
    

    将控制器更改为:

      <?php
        //Change It to `class Cron extends...` maybe your Prefixes have conflict
        class Cron_Controller extends CI_Controller{
    
        public function __construct(){
        parent::__construct();
    
    
        // this controller can only be called from the command line
        if (!$this->input->is_cli_request()) show_error('Direct access is not allowed');
    
    }
    
    public function AddAUser(){
        //Database Load is accable just in a Model Class
        $this->load->model('cronjob');
        $fname = "JUNCEL";
        $lname = "CARREON";
    
        $this->cronjob->adduser($fname,$lname);
    }
    
    }
    

    数据库加载器位置错误,前缀可能有冲突。

    【讨论】:

    • Codeigniter 是一个老框架! , 请使用新的框架 Like Yii , Phalconphp , laravel , 或者其他小项目的微框架
    • 哦,好吧!我将在我们的论文之后练习 laravel,但这东西不起作用我想问的是我将在任务调度程序中放入什么!什么网址
    • 请把你的错误码放在这里检查一下!这段代码在我所有的项目中运行良好,删除 if (!$this-&gt;input-&gt;is_cli_request()) 并在浏览器中测试它
    猜你喜欢
    • 1970-01-01
    • 2012-02-03
    • 1970-01-01
    • 2012-10-06
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    • 2010-12-05
    相关资源
    最近更新 更多