I have a "Persona" (Person) model, which has fields "nombres",
"apellidos" and "razon_social" (names, surnames and company_name). I
wanted to create good-looking select lists that showed the
company_name or (if it was not set) surnames, names. I then created a
virtual field "nombre_completo" (full_name) using afterFind after some
tutorials suggested. I then generated those lists with generateList.
It worked.
As I had not enough to do a few days ago, I happened to notice that
generateList is deprecated, and set about to use find('list') for this
task, and I cannot make it work.
I recently reopened bug #4456 https://trac.cakephp.org/ticket/4456
about this, but as Gwoo suggests there, I'll try solving the issue
here.
The details:
Persona.php model:
class Persona extends AppModel {
var $name = 'Persona';
var $primaryKey = 'idpersona';
var $displayField = 'nombre_completo';
var $hasMany = array(
'Direccion' => array(
'foreignKey' => 'personas_idpersona',
),
);
function afterFind($results) {
foreach($results as $llave => $valor) {
if (isset($results[$llave]['Persona'])) {
if ($results[$llave]['Persona']['activo'] == 1) {
$results[$llave]['Persona']['nombre_completo'] =
($valor['Persona']['razon_social'] != '') ?
($valor['Persona']['razon_social']) :
($valor['Persona']['apellidos'] . ', ' . $valor['Persona']
['nombres']);
}
}
}
return $results;
}
}
The Direccion model:
class Direccion extends AppModel {
var $name = 'Direccion';
var $primaryKey = 'iddireccion';
var $belongsTo = array(
'Persona' => array(
'className' => 'Persona',
'foreignKey' => 'personas_idpersona',
),
[...]
>From direcciones_controller.php (addresses_controller), add() method:
$personas = $this->Direccion->Persona->find('list',
array(
'conditions' => array('Persona.activo' => '1'),
'order' => array('Persona.idpersona', 'Persona.apellidos'),
)
);
$this->set('personas', $personas);
And yet, the view (direcciones/add):
SQL Error: 1054: Unknown column 'Persona.nombre_completo' in 'field
list'
for the query
SELECT `Persona`.`idpersona`, `Persona`.`nombre_completo`
FROM `personas` AS `Persona`
WHERE `Persona`.`activo` = 1
ORDER BY `Persona`.`idpersona` ASC, `Persona`.`apellidos` ASC
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---