2

I'm making a Cron job in CodeIgniter and I need access to my models in order to perform it's tasks. Here's my controller code.

<?php
/**
* 
*/
class Cron extends CI_Controller
{
    function __construct() {
        if ( PHP_SAPI !== 'cli' ) exit('No web access allowed');
    }

    public function send_mail() {
        $this->load->model("mail_model");
        foreach ($this->mail_model->cron_job() as $mail) {
            echo $mail->id;
        }
    }
}

If I call the send_mail function using CLI using this command:

php /home/path/to/project/index.php cron send_mail

It returns this error:

Message:  Undefined property: Cron::$load
PHP Fatal error:  Call to a member function model() on null in /home/path/to/project/application/controllers/cron.php on line 12

If I var_dump($this) it returns an empty object so I'm confused because it's extending CI_Controller. Any ideas?

KernelPanic
  • 2,328
  • 7
  • 47
  • 90
CatBrownie
  • 150
  • 3
  • 11
  • It extends CI_Controller, but where is the CI_Controller code? I don't see an include() or require(). – Octopus Nov 24 '15 at 20:34
  • @Octopus How do I include the CI_Controller code? – CatBrownie Nov 24 '15 at 21:35
  • near the top of your code you might need a line like this: `require_once('path/to/fileCI_Controller.php')`, but I'm not sure if maybe that file is already included somewehre and this Cron file is included afterwards. `require_once()`, `require()`, `include_once()` and `include()` are really similar functions with subtle differences, you should check your reference to see the differences. – Octopus Nov 24 '15 at 21:39
  • Tried doing "require '../../system/core/Controller.php';" and returns file not found error. – CatBrownie Nov 24 '15 at 21:47

1 Answers1

0

Error says, what empty is $this->load, not $this. Check, how $this->load initialised.

Edit your constructor:

function __construct() {
    if ( PHP_SAPI !== 'cli' ) exit('No web access allowed');
    parent::__construct();
}
Nick
  • 9,735
  • 7
  • 59
  • 89
  • There's no 'load' method in the '$this' object. This is what the var_dump returns. object(Cron)#11 (0) {} – CatBrownie Nov 24 '15 at 21:40
  • Your code trying to use property `load` of `$this`: `$this->load->model("mail_model");' – Nick Nov 24 '15 at 21:42
  • Yeah, that property should be in the $this object which it's inherited from the CI_Controller class. And I can't access it's properties in CLI. – CatBrownie Nov 24 '15 at 21:49
  • I see what `load` initialized in constructor of CI_Controller: https://github.com/bcit-ci/CodeIgniter/blob/5703f50fea1577fdc6de1d5c6a7c99b65488951b/system/core/Controller.php. So, add `parent::__construct();` into your constuctor. – Nick Nov 24 '15 at 21:59
  • It doesn't return an error now, but if I call my "send_mail()" function it doesn't output anything. – CatBrownie Nov 24 '15 at 22:15
  • Explore `$this->mail_model`, type `print_r($this->mail_model);` before foreach – Nick Nov 24 '15 at 22:28
  • Doesn't output anything either, but I found out that it's because of the database or the session libraries, because if I delete them from the autoload.php, CLI works perfectly. – CatBrownie Nov 24 '15 at 23:03