PinoyStackOverflower
PinoyStackOverflower

Reputation: 5302

CakePHP Calling a function in a model when you're in a controller

I am getting this error when calling a function to a model.

Database Error

Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'singleBlockWeekly' at line 1

SQL Query: singleBlockWeekly 

This is my Controller

class BlackoutController extends CalendarAppController
{
    var $uses = array('Studio','Blackout','BlackoutRepeatTypeDetails','Calendar.Event');
    var $components = array('Accesscontrol','RequestHandler');
    function event_checker()
    {
        $start_date = $this->request->data['start_date'];
        $end_date = $this->request->data['end_date'];
        $endsOn = $this->request->data['endsOn'];
        $repeatType = $this->request->data['repeatType'];
        $blockType = $this->request->data['blockType'];
        $frequency = $this->request->data['freq'];
        $repeatDays = $this->request->data['repeatDays'];

        #single type 
        if($blockType == "single"){
            if($repeatType == "weekly"){
                $dates = $this->Blackout->singleBlockWeekly($start_date,$endsOn,$repeatDays,$frequency);
                debug($dates);
                die;
            }
        }
     }
}

This is my Model

class Blackout extends CalendarAppModel
{
   var $name = 'Blackout';
   var $useTable = false;
   function singleBlockWeekly($startDate,$endDate,$repeatEvery = array(),$freq)
   {
     /*my code here...brevity...*/
   }
}

So i'm just doing here is just calling the singleBlockWeekly function in the Blackout Model. I'm wondering why am I getting the weird SQL error even if I don't have any SQL related code on the singleBlockWeekly function?

Your help will be greatly appreciated! Thanks! :)

Upvotes: 3

Views: 7108

Answers (2)

AnNaMaLaI
AnNaMaLaI

Reputation: 4084

Problem in loading Model, SO try this

$this->loadModel('Blackout');
$dates = $this->Blackout->singleBlockWeekly($start_date,$endsOn,$repeatDays,$frequency);

Upvotes: 1

mark
mark

Reputation: 21743

I assume this is a plugin? Than you cannot just include it as 'Blackout', you need to use the documented plugin syntax 'Calendar.Blackout'. Otherwise you will load an AppModel instance which, of course, does not have this method.

You could have found that out yourself if you had debugged the model instance:

debug($this->Blackout);

This SQL error is always an indicator of a misuse of model include statements.

Upvotes: 2

Related Questions