Here’s the documentation I’ve read and here’s what I’m trying. First I set up my Model associations:
<?php
class Proveedor extends AppModel {
public $hasMany = 'Libro';
public $displayField = 'Nombre';
}
<?php
class Libro extends AppModel {
public $belongsTo = 'Proveedor';
}
Next, I created my controller and my action function:
public function filter($proveedorName = null) {
// proveedorName is a string: wrox, apress, oreilly, etc.
// I need to find all libros that have their 'Proveedor' as $proveedorName.
$this->set('libros', $this->Libro->find('all',
array('conditions' => array('Libros.Proveedor.Name' => $proveedorName))));
$this->set('proveedores', $this->Libro->Proveedor->find('list'));
}
This is the error message that’s popping up:
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Libros.Proveedor.Name' in 'where clause'
My use case is quite simple, from a dropdown list select the proveedor and have the website return a collection of books (libros) that come from that supplier (proveedor).
It’s as if you visit: www.google.com/cars/filter/nissan – you want to see all Nissan cars.
Same principal here: www.google.com/libros/filter/apress – I want to fetch all books (libros) from proveedor (supplier) Apress.
For illustrative purposes, in C# using Entity Framework and Linq I can go:
var books = db.Libros.Where(x => x.Proveedor.Name == "Apress");
Can I do something similar with CakePHP?
This is wrong:
Should be:
And you database fields should be lower cased (name) and underscored, not upper cased (Name) or anything else. Follow the conventions, it will make things a lot more easy.