Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

cakephp

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! :)

like image 291
PinoyStackOverflower Avatar asked Nov 19 '25 17:11

PinoyStackOverflower


2 Answers

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.

like image 174
mark Avatar answered Nov 21 '25 06:11

mark


Problem in loading Model, SO try this

$this->loadModel('Blackout');
$dates = $this->Blackout->singleBlockWeekly($start_date,$endsOn,$repeatDays,$frequency);
like image 40
AnNaMaLaI Avatar answered Nov 21 '25 08:11

AnNaMaLaI



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!