Note that there are some explanatory texts on larger screens.

plurals
  1. PORelationship (foreignkey) CakePhp
    primarykey
    data
    text
    <p>i have a problem creation the relation (foreign Key) between mi models (tables), i followed the cake documentation but doesn't work</p> <p>the relationship is Marca 1-N Modelos</p> <p>model Marca.php</p> <pre><code>&lt;?php class Marca extends AppModel { var $name = 'Marca'; public $displayField = 'nombre'; var $hasMany = array( 'Modelo' =&gt; array( 'className' =&gt; 'Modelo', 'foreignKey' =&gt; 'marca_id' ) ); } ?&gt; </code></pre> <p>model Modelo.php</p> <pre><code>&lt;?php class Modelo extends AppModel { var $name = 'Modelo'; var $belongsTo = array( 'Marca' =&gt; array( 'className' =&gt; 'Marca', 'foreignKey' =&gt; 'marca_id' ) ); } ?&gt; </code></pre> <p>ModelosController</p> <pre><code>&lt;?php class ModelosController extends AppController{ var $name = 'Modelos'; var $helpers = array('Html','Form'); //var $scaffold; function index(){ $modelos = $this-&gt;Modelo-&gt;find('all',array( 'fields'=&gt;array( 'Modelo.nombre', 'Modelo.valido', 'Modelo.id', 'Modelo.marca_id'), 'order'=&gt;'Modelo.nombre ASC') ); $this-&gt;set('modelos',$modelos); } function add(){ if (!empty($this-&gt;data)){ $this-&gt;Modelo-&gt;create(); if ($this-&gt;Modelo-&gt;save($this-&gt;data)){ $this-&gt;Session-&gt;SetFlash('Modelo Nuevo Creado'); $this-&gt;redirect(array('action'=&gt;'index'),null,true); }else{ $this-&gt;Session-&gt;SetFlash('No se ha Agregado el Modelo, intente de Nuevo'); } } // $this-&gt;loadModel('Marca'); $marcas = $this-&gt;Modelo-&gt;Marca-&gt;find('list'); &lt;&lt;-----this is 31 line, where is the error debug($marcas); $this-&gt;set(compact('marcas')); } function edit($id = null){ if (!$id){ $this-&gt;Session-&gt;SetFlash('Modelo Incorrecto'); $this-&gt;redirect(array('action'=&gt;'index'),null,true); } if (empty($this-&gt;data)){ $this-&gt;data = $this-&gt;Modelo-&gt;find('first',array('conditions'=&gt;array('Modelo.id'=&gt; $id))); }else{ if ($this-&gt;Modelo-&gt;save($this-&gt;data)) { $this-&gt;Session-&gt;SetFlash('El Modelo se ha Guardado'); $this-&gt;redirect(array('action'=&gt;'index'),null,true); }else{ $this-&gt;Session-&gt;SetFlash('La Modelo No fue Guardado, intente de Nuevo'); } } } } ?&gt; </code></pre> <p>the error throws is:</p> <p>Error: Call to a member function find() on a non-object File: /var/www/tlfmovil/app/Controller/ModelosController.php<br> Line: 31</p> <p>i think the error is because the relation is not set up correctly, please helpe me.</p> <p>Sorry for my bad english!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload