CodeIgniter任务调度程序将该函数带到任务调度程序

时间:2016-09-17 08:44:09

标签: php codeigniter cron

我是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);
}

}

  ?>

我打算将它添加到数据库中,即使firstname和lastname相同也没问题,这只是一个试验工作。

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

我尝试过这件事:在任务计划程序上浏览它

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

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

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

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

1 个答案:

答案 0 :(得分:0)

更改您的模型,如下代码:

<?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);
}

}

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