Skip to content

Commit

Permalink
allowing vritual fields to work whether specified with a model alias or
Browse files Browse the repository at this point in the history
not. by concequence allows find('list', array('fields' => array('id',
'virtual'))) to work
  • Loading branch information
AD7six authored and markstory committed Jan 19, 2010
1 parent d60a66f commit 7943f3d
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions cake/libs/model/datasources/dbo_source.php
Expand Up @@ -106,7 +106,7 @@ class DboSource extends DataSource {

/**
* Constructor
*
*
* @param array $config Array of configuration information for the Datasource.
* @param boolean $autoConnect Whether or not the datasource should automatically connect.
* @access public
Expand Down Expand Up @@ -668,7 +668,7 @@ function fullTableName($model, $quote = true) {
* Creates new records in the database.
*
* @param Model $model Model object that the record is for.
* @param array $fields An array of field names to insert. If null, $model->data will be
* @param array $fields An array of field names to insert. If null, $model->data will be
* used to generate field names.
* @param array $values An array of values with keys matching the fields. If null, $model->data will
* be used to generate values.
Expand Down Expand Up @@ -853,7 +853,7 @@ function __filterResults(&$results, &$model, $filtered = array()) {
* Queries associations. Used to fetch results on recursive models.
*
* @param Model $model Primary Model object
* @param Model $linkModel Linked model that
* @param Model $linkModel Linked model that
* @param string $type Association type, one of the model association types ie. hasMany
* @param unknown_type $association
* @param unknown_type $assocData
Expand Down Expand Up @@ -1840,7 +1840,7 @@ function __scrubQueryData($data) {
* @param mixed $fields virtual fields to be used on query
* @return array
*/
function _constructVirtualFields(&$model,$alias,$fields) {
function _constructVirtualFields(&$model, $alias, $fields) {
$virtual = array();
foreach ($fields as $field) {
$virtualField = $this->name("{$alias}__{$field}");
Expand Down Expand Up @@ -1876,10 +1876,20 @@ function fields(&$model, $alias = null, $fields = array(), $quote = true) {
return $fields;
}
$virtual = array();
if ($model->getVirtualField()) {
$keys = array_keys($model->getVirtualField());
$virtualFields = $model->getVirtualField();
if ($virtualFields) {
$keys = array_keys($virtualFields);
foreach($keys as $field) {
$keys[] = $model->alias . '.' . $field;
}
$virtual = ($allFields) ? $keys : array_intersect($keys, $fields);
}
foreach($virtual as &$field) {
if (strpos($field, '.')) {
$field = str_replace($model->alias . '.', '', $field);
$fields = array_diff($fields, array($model->alias . '.' . $field));
}
}
$count = count($fields);

if ($count >= 1 && !in_array($fields[0], array('*', 'COUNT(*)'))) {
Expand Down

0 comments on commit 7943f3d

Please sign in to comment.