http://groups.google.com/group/cake-php/browse_thread/thread/c5709f46064b4106
This thread ended with [josh southern] discovering that the
$displayField var being defined causes afterFind to behave peculiarly,
but [nate] dismisses this posibility on the grounds that they are
supposably unrelated. However, this is certainly not the case. I have
experimented and saw that josh's observation was indeed correct, but
only when the $displayField is set to a non-existant "virtual" field.
The reason being is as follows: When you call generateList() without
explicitly specifying a keyPath or valuePath, the default fields to
search for are primaryKey and displayField. But if displayField is a
non-existant field in the model, the findAll returns FALSE instead of
an array of results, and hence the symptoms of undefined indexes or
invalid foreach warnings when using the afterFind method. If you do set
either a keyPath or valuePath however, it will search for all fields,
and then the afterFind callback can properly utilize the extra fields
to assemble the virtual field. So the solution to the problem is
either A) always define a keyPath or valuePath in the call to
generateList() and never defined a fake displayField, or B) use the
following beforeFind() callback to make sure requested fields exists
else we return then all, allowing us to utilize a fake displayField:
[code]
class Person extends AppModel {
var $name = 'Person';
var $displayField = 'full_name';
function afterFind($results) {
foreach ($results as $key => $val) {
if ( array_key_exists('first_name',$val[$this->name]) &&
array_key_exists('last_name',$val[$this->name]) ) {
$results[$key][$this->name]['full_name'] =
$val[$this->name]['first_name'] . ' ' . $val[$this->name]['last_name'];
}
}
return $results;
}
function beforeFind(&$queryData) {
//NOTE: This function required when setting $displayField to a
// virtual field that doesn't exist, to prevent from
causing
// empty results.
$fields = array();
if (is_array($queryData['fields'])) {
$fields = $queryData['fields'];
} else {
if ($queryData['fields'] != null) {
if (strpos($queryData['fields'], ',')) {
$fields = explode(',',
$queryData['fields']);
} else {
$fields = array($queryData['fields']);
}
$fields = array_map('trim', $fields);
}
}
$count = count($fields);
// Now to check for non-existant fields that might cause
problems.
if ($count >= 1 && $fields[0] != '*') {
foreach ($fields as $field) {
if ( !isset($this->_tableInfo->value[$field]) )
{
$queryData['fields'] = '*';
#$queryData['fields'] = null;
break;
}
}
}
return true;
}
}
$this->Person->generateList();
//$this->Person->generateList(null, null, null, null,
"{n}.Person.full_name");
[/code]
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" 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
-~----------~----~----~----~------~----~------~--~---