0

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>
Community
  • 1
  • 1
Thomson256
  • 217
  • 3
  • 7

2 Answers2

4

You cannot use Models in the view. What you need to do is set a view var from the Controller.

Looking closer at what you're doing, you just need to increase the recursive level so it returns Colition data with the Candidates records or use Containable.

$this->Candidate->find('all', array('conditions' => ..., 'recursive' => 2));

or

add public $actsAs = array('Containable'); to AppModel and

$this->Candidate->find('all', array('conditions' => ..., 'contain' => array('Colition'));

What is the relationship between Candidate and Colition?

tigrang
  • 6,767
  • 1
  • 20
  • 22
  • I set `$this->set('controller',$this);` on controller's edit & index functions and have some assisting functions on the Controller which i call in view `echo $this->Form->input('coalition', array('options' => $controller->getCoalitionsDropdownListContent(), ... ));`. – Thomson256 Jun 04 '12 at 07:16
  • I couldn't get those Cake's find(...) functions working :\ But with this knowledge of where not to use models and set the var in Controller i got problems solved, many thanks! – Thomson256 Jun 04 '12 at 07:20
0

is table/field

coalitions.name from candidates, coalitions

exist on database?

Redbox
  • 1,457
  • 5
  • 17
  • 22