Problem using cakephp 2.6 - Undefined index

Asked

Viewed 131 times

0

Good morning friends, I am learning to program on cake recently and I am having some difficulties.

Follow models and controllers.

Recibo.php (Model)

class Recibo extends AppModel{
    public $name = 'Recibo';
    public $belongsTo = array('Loja');  
    public $nomeloja = 'nome';
    public $nomefuncionario = 'nome';
}

Loja.php (model)

class Loja extends AppModel {
        public $name = 'Loja';
        public $hasOne = array('Recibos');      
        public $nomeloja = 'nome';
        public $displayField = 'nome';
}

RecibosController.php

class RecibosController extends AppController {
public $helpers = array ('Html','Form');
public $name = 'Recibos';
public $components = array('Session');
public $uses = array('Recibos', 'Loja','Funcionario');  

        public function index()
        {
            $this->loadModel('Recibos');
            $lojas = $this->Loja->find('all');
            $this->set('loja',$lojas);
            $this->set('recibos', $this->Recibos->find('all'));             
        }

Field where I display the receipt and would like to display the name of the store in front.

echo $recibo['Recibos']['loja_id'] . '&nbsp-&nbsp' .  $recibo['Loja']['nome']; 

What am I missing?

Because, when I click on "Notice" in the error I verify that the name and id of the store goes in the variable $loja and not in the variable $recibos (which is the one I’m listing)

Thanks in advance for the help.

Thanks.

1 answer

0


Let’s see some points:

  • Every reference to his model should be done in the singular by cakephp convention. Some places you should fix:

In the model Loja:

public $hasOne = array('Recibo');

In the variable $uses:

public $uses = array('Recibo', 'Loja', 'Funcionario');

And to carry out the consultation:

$this->set('recibos', $this->Recibo->find('all'));

  • All controller already carries its model primary, in the case of its RecibosController, the model Recibo is already loaded, so it is not necessary to include it in the variable $uses or use the method loadModel(). See here the reference in the documentation.

  • I don’t quite understand what you want to do in your view, would need a larger context. But if your intention is to list receipts and so display id and nome of the store, the idea is just this, since you have a 1:1 relationship between store and receipt. If this is the case, it is also the same issue of single use.

Thus:

foreach ($recibos as $recibo) {
    echo $recibo['Recibo']['loja_id'] . ' - ' .  $recibo['Loja']['nome'];
}
  • Cool my namesake Paulo. That’s right. I’m just starting out in cakephp, this is my second project. Thanks for your help.

Browser other questions tagged

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