Possible Duplicate:
Call to a member function on a non-object
I try to call an assisting function "getColition($id)" in a model from view in CakePHP application and i get this error:
Fatal error: Call to a member function getCoalition() on a non-object
in C:\xampp\htdocs\MyCakeApp\app\View\Candidates\index.ctp on line 32
I presume this might relate to a common MVC but i cannot figure out. Application is a small "election" app. Similar issues would come with any general assisting function.
Why i cannot call a function in model? Is model a proper place for assisting functions?
/app/Model/Candidate.php
<?php
/**
* @property
*/
class Candidate extends AppModel {
var $name = 'Candidates';
// THIS I WANT TO CALL
public function getCoalition($id){
$results = $this->Candidate->query("SELECT coalitions.name from candidates, coalitions where candidates.coalition_id = coalitions.id and candidates.id =$id");
return $results;
}
}
?>
/app/Controller/CandidatesController.php
<?php
class CandidatesController extends AppController {
public $helpers = array('Html', 'Form');
public $components = array('Session');
var $name = 'Candidates';
public function index() { //index stuff... }
public function add() { //add stuff...}
public function edit($id = null) { //edit stuff...}
public function delete($id) { //delete stuff...}
}
?>
/app/View/Candidate/index.ctp
<h1>Candidates</h1>
<p><?php echo $this->Html->link("Add candidate", array('action' =>'add'), array('class' => 'btn btn-primary')); ?></p>
<hr>
<table>
<tr>
<th>Name</th>
<th>Coalition</th>
</tr>
<?php foreach ($candidates as $candidate): ?>
<tr>
<td><?php echo $candidate['Candidate']['name']; ?></td>
<td>
<?php
///////////////// THIS CREATES THE ERROR /////////////////
echo $this->Candidate->getCoalition($candidate['Candidate']['coalition_id']);
?>
</td>
<td><?php
echo $candidate['Candidate']['votes'];
echo $this->Html->link('Muokkaa', array('action' => 'edit', $candidate['Candidate']['id']));
?>
</td>
</tr>
<?php endforeach; ?>
</table>