mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-19 02:56:15 +00:00
Fixing erroris due to bad rebase
This commit is contained in:
parent
4b9e03c9f7
commit
8c4cad86c2
3 changed files with 47 additions and 168 deletions
|
@ -419,35 +419,6 @@ 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) {
|
||||
if (strpos($field,'__') === false) {
|
||||
continue;
|
||||
}
|
||||
list($alias,$virtual) = explode('__',$field);
|
||||
|
||||
if (!ClassRegistry::isKeySet($alias)) {
|
||||
retrun;
|
||||
}
|
||||
$model = ClassRegistry::getObject($alias);
|
||||
if (isset($model->virtualFields[$virtual])) {
|
||||
$result[$alias][$virtual] = $value;
|
||||
unset($result[0][$field]);
|
||||
}
|
||||
}
|
||||
if (empty($result[0])) {
|
||||
unset($result[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a single field of the first of query results for a given SQL query, or false if empty.
|
||||
*
|
||||
|
@ -1822,24 +1793,6 @@ class DboSource extends DataSource {
|
|||
return $virtual;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
$virtualField = $this->name("{$alias}__{$field}");
|
||||
$expression = $model->virtualFields[$field];
|
||||
$virtual[] = $expression . " {$this->alias} {$virtualField}";
|
||||
}
|
||||
return $virtual;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the fields list of an SQL query.
|
||||
*
|
||||
|
|
|
@ -1049,7 +1049,7 @@ class Model extends Overloadable {
|
|||
* @access public
|
||||
*/
|
||||
function isVirtualField($field) {
|
||||
return !empty($this->virtualFields) && array_key_exists($field,$this->virtualFields);
|
||||
return !empty($this->virtualFields) && is_string($field) && array_key_exists($field,$this->virtualFields);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1170,9 +1170,9 @@ class Model extends Overloadable {
|
|||
return $data[$name[0]][$name[1]];
|
||||
}
|
||||
}
|
||||
if (!empty($data[0])) {
|
||||
$name = key($data[0]);
|
||||
return $data[0][$name];
|
||||
if (isset($data[0]) && count($data[0]) > 0) {
|
||||
return array_shift($data[0]);
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -26,123 +26,6 @@ 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');
|
||||
$Post->virtualFields = array('two' => "1 + 1");
|
||||
$result = $Post->find('first');
|
||||
$this->assertEqual($result['Post']['two'],2);
|
||||
|
||||
$Post->Author->virtualFields = array('false' => '1 = 2');
|
||||
$result = $Post->find('first');
|
||||
$this->assertEqual($result['Post']['two'],2);
|
||||
$this->assertEqual($result['Author']['false'],false);
|
||||
|
||||
$result = $Post->find('first',array('fields' => array('author_id')));
|
||||
$this->assertFalse(isset($result['Post']['two']));
|
||||
$this->assertFalse(isset($result['Author']['false']));
|
||||
|
||||
$result = $Post->find('first',array('fields' => array('author_id','two')));
|
||||
$this->assertEqual($result['Post']['two'],2);
|
||||
$this->assertFalse(isset($result['Author']['false']));
|
||||
|
||||
$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);
|
||||
|
||||
$result = $Post->find('first',array(
|
||||
'conditions' => array('two' => 2),
|
||||
'limit' => 1
|
||||
));
|
||||
$this->assertEqual($result['Post']['two'],2);
|
||||
|
||||
$result = $Post->find('first',array(
|
||||
'conditions' => array('two <' => 3),
|
||||
'limit' => 1
|
||||
));
|
||||
$this->assertEqual($result['Post']['two'],2);
|
||||
|
||||
$result = $Post->find('first',array(
|
||||
'conditions' => array('NOT' => array('two >' => 3)),
|
||||
'limit' => 1
|
||||
));
|
||||
$this->assertEqual($result['Post']['two'],2);
|
||||
|
||||
$dbo =& $Post->getDataSource();
|
||||
$Post->virtualFields = array('other_field' => $dbo->name('Post.id') . ' + 1');
|
||||
$result = $Post->find('first',array(
|
||||
'conditions' => array('other_field' => 3),
|
||||
'limit' => 1
|
||||
));
|
||||
$this->assertEqual($result['Post']['id'],2);
|
||||
|
||||
$Post->virtualFields = array('other_field' => $dbo->name('Post.id') . ' + 1');
|
||||
$result = $Post->find('all',array(
|
||||
'fields' => array($dbo->calculate($Post,'max',array('other_field')))
|
||||
));
|
||||
$this->assertEqual($result[0][0]['other_field'],4);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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');
|
||||
$Post->virtualFields = array('two' => "1 + 1");
|
||||
$result = $Post->find('first');
|
||||
$this->assertEqual($result['Post']['two'],2);
|
||||
|
||||
$Post->Author->virtualFields = array('false' => '1 = 2');
|
||||
$result = $Post->find('first');
|
||||
$this->assertEqual($result['Post']['two'],2);
|
||||
$this->assertEqual($result['Author']['false'],false);
|
||||
|
||||
$result = $Post->find('first',array('fields' => array('author_id')));
|
||||
$this->assertFalse(isset($result['Post']['two']));
|
||||
$this->assertFalse(isset($result['Author']['false']));
|
||||
|
||||
$result = $Post->find('first',array('fields' => array('author_id','two')));
|
||||
$this->assertEqual($result['Post']['two'],2);
|
||||
$this->assertFalse(isset($result['Author']['false']));
|
||||
|
||||
$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);
|
||||
}
|
||||
|
||||
/**
|
||||
* testFetchingNonUniqueFKJoinTableRecords()
|
||||
|
@ -7279,5 +7162,48 @@ class ModelReadTest extends BaseModelTest {
|
|||
$comments = $Comment->find('first');
|
||||
$this->assertEqual($comments['Comment']['querytype'], 'first');
|
||||
}
|
||||
|
||||
/**
|
||||
* 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');
|
||||
$Post->virtualFields = array('two' => "1 + 1");
|
||||
$result = $Post->find('first');
|
||||
$this->assertEqual($result['Post']['two'],2);
|
||||
|
||||
$Post->Author->virtualFields = array('false' => '1 = 2');
|
||||
$result = $Post->find('first');
|
||||
$this->assertEqual($result['Post']['two'],2);
|
||||
$this->assertEqual($result['Author']['false'],false);
|
||||
|
||||
$result = $Post->find('first',array('fields' => array('author_id')));
|
||||
$this->assertFalse(isset($result['Post']['two']));
|
||||
$this->assertFalse(isset($result['Author']['false']));
|
||||
|
||||
$result = $Post->find('first',array('fields' => array('author_id','two')));
|
||||
$this->assertEqual($result['Post']['two'],2);
|
||||
$this->assertFalse(isset($result['Author']['false']));
|
||||
|
||||
$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…
Add table
Reference in a new issue