【问题标题】:CakePHP: Models in AppControllerCakePHP:AppController 中的模型
【发布时间】:2023-03-26 11:11:02
【问题描述】:

我在 appController 中创建了一个函数来发送电子邮件:

protected function sendEmail($studentId = null, $courseId = null, $action = null) {

    $course = $this->Course->find('first', array('conditions' => array('Course.id' => $courseId)));
    if(! $course) {
        throw new NotFoundException(__('Invalid course'));
    }
}

我想验证课程和学生,但根据我从哪个控制器调用它,我必须修改我的 $this->xxx->find 语句。我想出的唯一其他解决方案是验证/发送 $course/$student 数据头,但我仍然想运行手动 SQL 查询来保存电子邮件日志(我会遇到相同的情况问题)。有什么帮助吗?

【问题讨论】:

  • 如果我理解正确的话;只需在您的AppController 中加载Course 模型。每个控制器都可以访问它。这样,任何控制器都可以调用该函数。 $this->loadModel('Course');sendEmail 函数中应该这样做。

标签: cakephp


【解决方案1】:

我不太明白你的问题,但是:

如果可以,或者不行,请给我打电话.. 根据我的代码提出一个新的想法。我现在不知道你的数据库结构,但我认为这适用于连接查询......

$course = $this->Course
$student = $this->Student
  if(!empty($student) && !empty($course)): $status = 3 endif;   
  if(empty($student)): $status1 = 1 else: $status = 0 endif;
  if(empty($course)): $status2 = 2 else: $status = 0 endif;
    if(empty($status)):
      $array=array($status1,status2);
    else:
      $array=array($status);
    endif;
        foreach($array as $key => $value)
        if($value == 1):throw new NotFoundException(__('Invalid course')); 
        if($value == 2):throw new NotFoundException(__('Invalid student'));
        if($value == 0):throw new NotFoundException(__('Invalid course and student')); 
        if($value == 3):throw new NotFoundException(__('Valide course and student')); 
      endforeach;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-07
    • 1970-01-01
    • 2022-01-05
    • 2014-01-03
    • 2013-05-28
    • 2020-06-08
    • 2020-12-21
    • 2015-09-16
    相关资源
    最近更新 更多