2016-09-17 165 views
0

我是CRON任务的新成员,所以我在练习它;对于初学者,我想提出一个cron作业将使用CodeIgniter和PHP用户, 这里是我的模型:CodeIgniter任务调度程序将任务调度到任务调度程序

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

    } 

和:这是我的控制器:

<?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 

(可选):我把AddAUser,所以基本上变成这个样子:

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

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

回答

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

} 

数据库加载器是在一个错误的地点和前缀可能是有冲突的。

+0

Codeigniter是一个旧框架! ,请使用新的框架像Yii'','Phalconphp','laravel'或者其他微型框架的小项目 –

+0

哦!我将在我们的论文之后练习laravel,但是这个东西并没有工作,我想问的是我会在任务调度程序中放入什么东西!什么网址 – GGsThePlayDude

+0

请把你的错误代码在这里检查它!这段代码在我所有的项目中工作良好,删除'if(!$ this-> input-> is_cli_request())'并在浏览器中测试它 –