Call to a Member Function find() on a non-object

Asked

Viewed 1,347 times

2

I’ve been racking my brain here for a while and I still don’t understand the mistake. I’m learning about Cakephp. I did the Regionals table MVC manually, and for the others, I used the Bake. The problem is that when I try to add a new congregation, this title error is returned. I’ve searched elsewhere, but I haven’t found a solution yet.

Controller Congregacaoscontroller.php

/**
 * Components
 *
 * @var array
 */
    public $components = array('Paginator');

/**
 * index method
 *
 * @return void
 */
    public function index() {
        $this->Congregacao->recursive = 0;
        $this->set('congregacaos', $this->paginate());
    }

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
    public function view($id = null) {
        if (!$this->Congregacao->exists($id)) {
            throw new NotFoundException(__('Invalid congregacao'));
        }
        $options = array('conditions' => array('Congregacao.' . $this->Congregacao->primaryKey => $id));
        $this->set('congregacao', $this->Congregacao->find('first', $options));
    }

/**
 * add method
 *
 * @return void
 */
    public function add() {
        if ($this->request->is('post')) {
            $this->Congregacao->create();
            if ($this->Congregacao->save($this->request->data)) {
                $this->Session->setFlash(__('The congregacao has been saved'), 'flash/success');
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The congregacao could not be saved. Please, try again.'), 'flash/error');
            }
        }
        $regionals = $this->Congregacao->Regional->find('list');
        $this->set(compact('regionals'));
    }

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
    public function edit($id = null) {
        $this->Congregacao->id = $id;
        if (!$this->Congregacao->exists($id)) {
            throw new NotFoundException(__('Invalid congregacao'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Congregacao->save($this->request->data)) {
                $this->Session->setFlash(__('The congregacao has been saved'), 'flash/success');
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The congregacao could not be saved. Please, try again.'), 'flash/error');
            }
        } else {
            $options = array('conditions' => array('Congregacao.' . $this->Congregacao->primaryKey => $id));
            $this->request->data = $this->Congregacao->find('first', $options);
        }
        $regionals = $this->Congregacao->Regional->find('list');
        $this->set(compact('regionals'));
    }

/**
 * delete method
 *
 * @throws NotFoundException
 * @throws MethodNotAllowedException
 * @param string $id
 * @return void
 */
    public function delete($id = null) {
        if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
        $this->Congregacao->id = $id;
        if (!$this->Congregacao->exists()) {
            throw new NotFoundException(__('Invalid congregacao'));
        }
        if ($this->Congregacao->delete()) {
            $this->Session->setFlash(__('Congregacao deleted'), 'flash/success');
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Congregacao was not deleted'), 'flash/error');
        $this->redirect(array('action' => 'index'));
    }
}

Model Congregacao.php

<?php
App::uses('AppModel', 'Model');
/**
 * Congregacao Model
 *
 * @property Regionals $Regionals
 * @property Membro $Membro
 */
class Congregacao extends AppModel {

/**
 * Validation rules
 *
 * @var array
 */
    public $validate = array(
        'nome' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'regionals_id' => array(
            'numeric' => array(
                'rule' => array('numeric'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
    );

    //The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
    public $belongsTo = array(
        'Regionals' => array(
            'className' => 'Regionals',
            'foreignKey' => 'regionals_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

/**
 * hasMany associations
 *
 * @var array
 */
    public $hasMany = array(
        'Membro' => array(
            'className' => 'Membro',
            'foreignKey' => 'congregacao_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

}

Model Regional.php

    public $validate = array(
        'descricaoRegional' => array(
            'rule' => 'notEmpty'
        )
    );
}

Controller Regionalscontroller.php

    public function index() 
    {
        $this->set('regionals', $this->Regional->find('all'));
    }

    public function view($id = null)
    {
        $this->Regional->regionals_id = $id;
        $this->set('regional', $this->regional->read());
    }

     public function add() 
     {
        if ($this->request->is('post')) 
        {
            if ($this->Regional->save($this->request->data)) 
            {
                //$this->Session->setFlash("A regional foi adicionada com sucesso");
                $this->Session->setFlash(__("A regional foi adicionada com sucesso."), 'flash_success');
                $this->redirect(array('action' => 'index'));
            }
        }
    }

    public function edit($id = null) 
    {
        $this->Regional->id = $id;
        if ($this->request->is('get')) 
        {
            $this->request->data = $this->Regional->read();
        } 
        else
        {
            if ($this->Regional->save($this->request->data)) 
            {
                //$this->Session->setFlash("A regional $id foi atualizada.");
                $this->Session->setFlash(__("A regional $id foi atualizada."), 'flash_success');
                $this->redirect(array('action' => 'index'));
            }
        }
    }

    public function delete($regionals_id) 
    {
        if (!$this->request->is('post')) 
        {
            throw new MethodNotAllowedException();
        }
        if ($this->Regional->delete($regionals_id)) 
        {
            //$this->Session->setFlash("A regional $regionals_id foi deletada.");
            $this->Session->setFlash(__("A regional $regionals_id foi deletada."), 'flash_danger');
            $this->redirect(array('action' => 'index'));
        }
    }
}

Grateful for any help.

2 answers

0

It would be useful to have more information about the mistake to point you in a direction. Initially you are trying to call a method a variable that is not an object... To try to help you, if you are using apache, think about activating the Xdebug mod, it will give you more information about the error.

Xampp for windows:

Add these lines to your php.ini:

[XDebug]
zend_extension = "C:\xampp\php\ext\php_xdebug.dll"
xdebug.cli_color = 1
xdebug.var_display_max_children = -1
xdebug.var_display_max_depth = -1
xdebug.var_display_max_data = -1

In apache / linux:

Installation:

\# pecl install xdebug

Lines to add in php.ini

[XDebug]
zend_extension="/usr/local/php/modules/xdebug.so"
xdebug.cli_color = 1
xdebug.var_display_max_children = -1
xdebug.var_display_max_depth = -1
xdebug.var_display_max_data = -1

More information about Xdebug:

http://xdebug.org/docs/install

Put in the detail of the mistake and see if I can help you more.

Hug

  • Xdebug is already enabled here. This "Call to a Member..." message is a ready exception to Cakephp. He even has a debug of his own, but I don’t know how to use it correctly. What else can I post to detail the error?

0


I found the problem.

In Congregacaoscontroller.php, line 58, it looks like this:

$regionals = $this->Congregacao->Regional->find('list');

So I consulted my model Congregacao.php, and there it is, on line 46:

public $belongsTo = array(
        'Regionals' => array(
            'className' => 'Regionals',
            'foreignKey' => 'regionals_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

That is, the correct is "Regionals", not "Regional". It was only correct in the controller and the add started to work normally.

$regionals = $this->Congregacao->Regionals->find('list', array(
            'fields' => array('id', 'descricao_regionals')
        ));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.