Problem using cakephp 2.6 - Undefined index

0

Good morning friends, I'm learning cake programming a short time ago and I'm having some difficulties.

Follow the 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?

Well, when I click on "Notice" on the error, I check that the name and id of the store goes in the variable $loja and not the variable $recibos (which is what I am listing)

Thanks for the help right away.

Thanks.

    
asked by anonymous 29.04.2016 / 15:51

1 answer

0

Let's see some points:

  • All reference to your model should be done in the singular by CakePHP convention . Some places you should fix:

In model Loja :

public $hasOne = array('Recibo');

In the $uses variable:

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

And to perform the query:

$this->set('recibos', $this->Recibo->find('all'));
  • controller already loads its primary model , in the case of its RecibosController , the model Recibo then it is not necessary to include it in the $uses variable and neither use the loadModel() method. See here the reference in the documentation.
  • I do not quite understand what you want to do in your view , you would need a larger context. But if your intention is to list the receipts and thus display id and nome of the store, the idea is precisely this, since you have a 1: 1 ratio between store and receipt. If this is the case, it is also the same as the question of singular usage.

So:

foreach ($recibos as $recibo) {
    echo $recibo['Recibo']['loja_id'] . ' - ' .  $recibo['Loja']['nome'];
}
    
30.04.2016 / 14:34