mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 11:28:25 +00:00
Fixing virtual fields fetching for Model::field() calls
Adding doc blocks
This commit is contained in:
parent
22e36cb057
commit
7efafc685e
2 changed files with 33 additions and 1 deletions
|
@ -369,7 +369,6 @@ class DboSource extends DataSource {
|
|||
|
||||
$first = $this->fetchRow();
|
||||
if ($first != null) {
|
||||
$this->fetchVirtualField($first);
|
||||
$out[] = $first;
|
||||
}
|
||||
while ($this->hasResult() && $item = $this->fetchResult()) {
|
||||
|
@ -391,6 +390,12 @@ class DboSource extends DataSource {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Modifies $result array to place virtual fields in model entry where they belongs to
|
||||
*
|
||||
* @param array $resut REference to the fetched row
|
||||
* @return void
|
||||
*/
|
||||
function fetchVirtualField(&$result) {
|
||||
if (isset($result[0]) && is_array($result[0])) {
|
||||
foreach ($result[0] as $field => $value) {
|
||||
|
@ -1789,6 +1794,14 @@ class DboSource extends DataSource {
|
|||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts model virtual fields into sql expressions to be fetched later
|
||||
*
|
||||
* @param Model $model
|
||||
* @param string $alias Alias tablename
|
||||
* @param mixed $fields virtual fields to be used on query
|
||||
* @return array
|
||||
*/
|
||||
function _constructVirtualFields(&$model,$alias,$fields) {
|
||||
$virtual = array();
|
||||
foreach ($fields as $field) {
|
||||
|
|
|
@ -26,6 +26,15 @@ require_once dirname(__FILE__) . DS . 'model.test.php';
|
|||
*/
|
||||
class ModelReadTest extends BaseModelTest {
|
||||
|
||||
/**
|
||||
* testVirtualFields()
|
||||
*
|
||||
* Test correct fetching of virtual fields
|
||||
* currently is not possible to do Relation.virtualField
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testVirtualFields() {
|
||||
$this->loadFixtures('Post','Author');
|
||||
$Post = ClassRegistry::init('Post');
|
||||
|
@ -48,6 +57,16 @@ class ModelReadTest extends BaseModelTest {
|
|||
|
||||
$result = $Post->find('first',array('fields' => array('two')));
|
||||
$this->assertEqual($result['Post']['two'],2);
|
||||
|
||||
$Post->id = 1;
|
||||
$result = $Post->field('two');
|
||||
$this->assertEqual($result,2);
|
||||
|
||||
ClassRegistry::flush();
|
||||
$Writing = ClassRegistry::init(array('class' => 'Post', 'alias' => 'Writing'),'Model');
|
||||
$Writing->virtualFields = array('two' => "1 + 1");
|
||||
$result = $Writing->find('first');
|
||||
$this->assertEqual($result['Writing']['two'],2);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue