如何在cakephp 3中制作模型内的默认函数?

时间:2015-09-26 06:02:20

标签: php cakephp-3.0

我想在模特中制作自定义功能 -

我的控制器 -

<?php

namespace App\Controller;

use Cake\Event\Event;
use Cake\ORM\TableRegistry;
use Cake\I18n\Time;
use Cake\Auth\DefaultPasswordHasher;
use Cake\ORM\Entity;

class AdminsController extends AppController
{
    public function planSessions()
    {
        $this->loadmodel('Plans_sessions');
        $sessions = $this->Plans_sessions->getPlanSessions();
        $this->set('sessions',$sessions);
    }
}

我的模特 -

<?php
namespace App\Model\Table;

use Cake\ORM\Table;

class Plans_sessionsTable extends Table
{
    public function getPlanSessions()
    {
        return $this->query("select * from `plans_sessions` where category LIKE '%shop%' ");
    }
}

但它收到错误 - 未知方法“getPlanSessions”

1 个答案:

答案 0 :(得分:0)

您应遵循命名约定:

http://book.cakephp.org/3.0/en/intro/conventions.html#model-and-database-conventions

表类名称是复数和CamelCased。 People,BigPeople和ReallyBigPeople都是传统模型名称的例子。

$this->PlansSessions->getPlanSessions();