0

I am calling one controller method from another controller's method. I can confirm that the method is being called because when I do a print_r on the passed data, I see everything I expect. My problem is that when I call $this->Log->save($this->data) nothing is written to the DB. I am a .NET developer, hence I know preciously little about cakePHP, but I know that this is the way one is supposed to save stuff to the DB, hence I am majorly stumped.

<?php
//The calling controller
App::import('Controller', 'Logs');
class othAuthComponent extends Object //I noticed this inherits from Object, 
                                      //but it doesn't seem to be a problem
{
    function SomeFunction()
    {
        //create instance of our Logs controller as per example
        $Logger = new LogsController;
        $Logger->constructClasses();
        $Logger->cms_addlog($user['User']['name'].' Logged in', $user['User']['id']);
    }
}
?>

And the offender:

<?php
//the called controller
class LogsController extends AppController 
{
    function cms_addlog($note,$ba_id)
    {

        $this->Log->create();
        $curDateTime = getdate();

        $this->data['LogTime'] = $curDateTime;
        $this->data['Note'] = $note;
        $this->data['brandactivatorid'] = $ba_id;
        //print_r($this->data);
            //die();
        $this->Log->save($this->data);
    }
}
?>
tereško
  • 58,060
  • 25
  • 98
  • 150
Captain Kenpachi
  • 6,960
  • 7
  • 47
  • 68

2 Answers2

2

The correct way to do this is to make the cms_addlog function part of the Log model.

Then call

$this->Log->cms_addlog($user['User']['name'].' Logged in', $user['User']['id']);

Joey
  • 21
  • 1
  • 2
1

You're not supposed to call Controller actions/methods from anywhere in your code. Controller actions are meant to be directly accessed from the browser.

The clean way is to implement a addLog method in your model and call that one either from the controller, or from the component.

Please read http://book.cakephp.org/2.0/en/getting-started/cakephp-structure.html and http://book.cakephp.org/2.0/en/cakephp-overview/understanding-model-view-controller.html for further reference.

nanoman
  • 1,069
  • 12
  • 27
  • Thank you. But can you tell me why my function runs 90% through and then does not do the most important bit of all? Surely the location of the method is more of a best practice consideration than an actual error? I'll do what you suggested, but I can't figure out why this doesn't work. – Captain Kenpachi Mar 11 '13 at 14:38
  • I think with what you're doing you're triggering undefined behavior - this is clearly to be considered an error. – nanoman Mar 11 '13 at 14:46
  • I meant undefined Framework behavior, not undefined PHP behavior. I would suggest to clean up your code structure first before trying to debug this. – nanoman Mar 12 '13 at 14:37
  • I have cleaned up my code and put the method inside the Model definition. Still nothing. – Captain Kenpachi Mar 12 '13 at 15:17
  • What is the return value of your save() call? – nanoman Mar 12 '13 at 22:38