CakePHP调用未定义的方法find() [英] CakePHP Call to undefined method find()

查看:320
本文介绍了CakePHP调用未定义的方法find()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我是CakePHP的新手,尝试设置一个类似于CakePHP博客教程的小项目。



我做了什么:


  • 创建数据库表sessions li>
  • 在Controller文件夹中创建一个SessionsController.php
  • 在Model文件夹中创建一个Session.php
  • 在View中的Sessions文件夹
  • 在Sessions文件夹中创建一个index.ctp


    问题是:

    当试图访问
    会话时,我会得到这个错误:

    错误:调用未定义的方法SessionComponent :: find()

    File:C:\xampp\htdocs\JFKTransparency\app\Controller\SessionsController.php
    Line:12



    这是我的SessionsController.php:


      class SessionsController extends AppController {
    public $ helpers = array('Html','Form');
    public $ layout ='jfklayout';
    public function index(){
    $ this-> loadModel('Councilor');
    $ this-> set('councilors',$ this-> Councillor-> find('all',array('order'=> array('Councillor.id'=>'asc ))));
    $ this-> set('sessions',$ this-> Session-> find('all'));如果(!$ id){
    ($ id = null){
    )公共函数视图($ id = null){
    $抛出新的NotFoundException(__('无效的职位'));
    }

    $ session = $ this-> Session-> findById($ id);
    if(!$ session){
    throw new NotFoundException(__('Invalid post'));
    }
    $ this-> set('session',$ session);




    $ b这是我的Session.php:

     <?php 

    class Session extends AppModel {

    public $ hasmany = array ('Decision'=> array('className'=>'Decision','foreign_key'=>'session_id'));




    $ p $这是我的index.ctp:

     < h1> Ratssitzungen< / h1> 

    < table>
    < tr>
    < th> Id< / th>
    < th>日期< / th>
    < th>标题< / th>
    < th>描述< / th>

    <?php foreach($ session as $ session):?>
    < th>
    < div class =councilor>
    <?php echo $ this-> Html-> image($ councilor ['Councilor'] ['imageurl'],array('alt'=> $ councilor ['Councilor'] ['' first_name'],'border'=>'1'))?>>
    < p><?php echo $ councilor ['Councilor'] ['first_name']。 。 $委员[委员] [姓氏]; ?>< / p为H.
    < / div>
    < / th>
    <?php endforeach; ?>
    < / tr>

    <?php foreach($ session as $ session):?>
    < tr>
    < td><?php echo $ session ['Session'] ['id']; ?>< / TD>
    < td><?php echo $ session ['Session'] ['session_date']; ?>< / TD>
    < td><?php echo $ session ['Session'] ['title']; ?>< / TD>
    < / tr>
    < tr COLSPAN =3>
    < td>
    <?php echo h($ session ['Session'] ['description']); ?>
    < / td>
    < / tr>
    <?php endforeach; ?>
    <?php unset($ session); ?>
    < / table>

    我知道这应该工作,因为我可以从CakePHP教程的PostsController访问会话就好了:

      $ this-> loadModel('Session'); 
    $ this-> set('sessions',$ this-> Session-> find('all'));

    这个代码可以在PostsController中使用。有没有人有线索?



    感谢!

    解决方案

    在考虑Models / Controllers / Components / Helpers / etc名字,你必须记住,除非你想遇到意想不到的错误,否则最好不要使用一组保留字。一般来说,你不得不介意语言的特定保留字和框架字。这不仅适用于PHP和CakePHP,也适用于Java和X框架,Python和Django等等。



    现在,错误是使用名称会话的模型,因为蛋糕使用会议的组件和帮手。所以,理智的事情要做:只是改变名称。

    为了将来的参考,我会留下链接在这里的保留字在PHP和蛋糕(我可以错过了一些)。



    PHP的关键字



    对于蛋糕,您必须留意蛋糕的类(不要命名您的任何模型/组件/等,像那些)和常量和全局函数。

    Cake的类(你的错误在这里,在文件夹控制器/组件)/ SessionComponent



    全局常量


    So I am new to CakePHP and tried to set up a little project analogous to the CakePHP blog tutorial.

    What I did:

    • Created a database table "sessions"
    • Created a SessionsController.php in the Controller folder
    • Created a Session.php in the Model folder
    • Created a Sessions folder in View
    • Created a index.ctp in the Sessions folder

    What my problem is:

    When trying to access Sessions, I will get this error:

    "Error: Call to undefined method SessionComponent::find()
    File: C:\xampp\htdocs\JFKTransparency\app\Controller\SessionsController.php Line: 12"

    This is my SessionsController.php:

    class SessionsController extends AppController {
        public $helpers = array('Html', 'Form');
        public $layout = 'jfklayout'; 
        public function index() { 
            $this->loadModel('Councillor');
            $this->set('councillors', $this->Councillor->find('all', array('order'=> array('Councillor.id' => 'asc'))));
            $this->set('sessions', $this->Session->find('all'));  <<<<------FAILS HERE
        }
        public function view($id = null) {
            if (!$id) {
               throw new NotFoundException(__('Invalid post'));
            }
    
            $session = $this->Session->findById($id);
            if (!$session) {
                throw new NotFoundException(__('Invalid post'));
            }
            $this->set('session', $session);
        } 
    }
    

    This is my Session.php:

    <?php
    
    class Session extends AppModel {
    
        public $hasmany = array('Decision' => array('className' => 'Decision','foreign_key' => 'session_id'));
    
    } 
    

    This is my index.ctp:

    <h1> Ratssitzungen </h1>
    
    <table>
        <tr>
            <th>Id</th>
            <th>Date</th>
            <th>Title</th>
            <th>Description</th>
    
            <?php foreach ($sessions as $session): ?>
                <th>
            <div class="councillor">
                <?php echo $this->Html->image($councillor['Councillor']['imageurl'], array('alt' => $councillor['Councillor']['first_name'], 'border' => '1')) ?>
                <p><?php echo $councillor['Councillor']['first_name'] . " " . $councillor['Councillor']['last_name']; ?></p>
            </div>
        </th>
    <?php endforeach; ?>
    </tr>
    
    <?php foreach ($sessions as $session): ?>
        <tr>
            <td><?php echo $session['Session']['id']; ?></td>
            <td><?php echo $session['Session']['session_date']; ?></td>
            <td><?php echo $session['Session']['title']; ?></td>  
        </tr>
        <tr COLSPAN="3">
            <td>
                <?php echo h($session['Session']['description']); ?>
            </td>
        </tr>
    <?php endforeach; ?>
    <?php unset($session); ?>
    </table>
    

    I know this should work because I can access the Sessions from the PostsController from the CakePHP tutorial just fine:

    $this->loadModel('Session');
    $this->set('sessions', $this->Session->find('all')); 
    

    This code works from PostsController. Does anyone have a clue?

    Thanks!

    解决方案

    When thinking about Models/Controllers/Components/Helpers/etc names within your project, you have to keep in mind that there's a set of reserved words that's best not to use unless you want to encounter unexpected errors. Generally speaking, you have to mind the language's specific reserved words, and the framework ones. That applies not only for PHP and Cakephp, but Java and X framework, python and Django, and all others.

    Now, for this, the error was the use of the name "Session" for models, since Cake uses "Sessions" for its component and helper. So, sane thing to do: just change the name.

    And for future references, I'll leave links here for the reserved words in php, and cake (I may have missed a few).

    PHP keywords

    For cake, you'll have to keep an eye on cake's classes (don't name any of your models/components/etc, like those), and constant and global functions.

    Cake's classes (your error falls here, in the folder Controller/Component)/SessionComponent

    Global constants

    这篇关于CakePHP调用未定义的方法find()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆