Call to a member function find () on a non-object

2

I've been breaking my head here for some time and have not yet understood the error. I'm learning about CakePHP. I made the MVC from the Regionals table manually, and for others, I used the bake. The problem is that when I try to add a new congregation, this title error is returned. I searched elsewhere, but I have not 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'));
        }
    }
}

Thanks for any help.

    
asked by anonymous 16.01.2015 / 20:15

2 answers

0

I discovered the problem.

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

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

Then I consulted my model Congregacao.php, and there it is, in line 46:

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

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

$regionals = $this->Congregacao->Regionals->find('list', array(
            'fields' => array('id', 'descricao_regionals')
        ));
    
26.01.2015 / 17:20
0

It would be useful to have more information about the error to point you in a direction. Initially you are trying to call a method from 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

No 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 about xdebug:

link

Check the error detail and see if I can help you further.

Embrace

    
16.01.2015 / 21:47