CakePHP当您在控制器中时,在模型中调用函数

时间:2013-06-18 06:05:58

标签: php cakephp

在向模型调用函数时出现此错误。

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 

这是我的控制器

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

这是我的Model

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

所以我在这里只是在Blackout模型中调用singleBlockWeekly函数。我想知道为什么即使我在singleBlockWeekly函数上没有任何SQL相关代码,我也会得到奇怪的SQL错误?

非常感谢您的帮助!谢谢! :)

2 个答案:

答案 0 :(得分:2)

我认为这是一个插件? 你不能只把它包含为'Blackout',你需要使用文档化的插件语法'Calendar.Blackout'。 否则,您将加载AppModel实例,当然,该实例没有此方法。

如果你调试了模型实例,你可能已经找到了自己:

debug($this->Blackout);

此SQL错误始终是滥用模型包含语句的指示。

答案 1 :(得分:1)

加载模型时出现问题,请尝试此操作

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