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!