CakePHP Call to undefined method find()

2019-09-18 05:39发布

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!

标签: cakephp find
1条回答
趁早两清
2楼-- · 2019-09-18 06:10

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

查看更多
登录 后发表回答