Skip to content

Commit

Permalink
Merge pull request #1566 from sjinks/issue-1534
Browse files Browse the repository at this point in the history
Fix #1534
  • Loading branch information
Phalcon committed Nov 18, 2013
2 parents d43e90e + 087116c commit 6589765
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 28 deletions.
42 changes: 30 additions & 12 deletions ext/mvc/model.c
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,8 @@ PHP_METHOD(Phalcon_Mvc_Model, _exists){
zval *value = NULL, *escaped_field = NULL, *pk_condition = NULL, *type = NULL;
zval *join_where, *dirty_state, *schema, *source;
zval *escaped_table, *null_mode, *select, *num;
zval *row_count;
zval *row_count, *v;
int seen_rawvalues = 0;
HashTable *ah0;
HashPosition hp0;
zval **hd;
Expand Down Expand Up @@ -1361,27 +1362,40 @@ PHP_METHOD(Phalcon_Mvc_Model, _exists){
phalcon_increment(number_empty);
}

phalcon_array_append(&unique_params, value, PH_SEPARATE);
v = value;
} else {
phalcon_array_append(&unique_params, null_value, PH_SEPARATE);
v = null_value;
phalcon_increment(number_empty);
}

PHALCON_INIT_NVAR(escaped_field);
phalcon_call_method_p1(escaped_field, connection, "escapeidentifier", field);

PHALCON_INIT_NVAR(pk_condition);
PHALCON_CONCAT_VS(pk_condition, escaped_field, " = ?");

if (Z_TYPE_P(v) == IS_OBJECT && instanceof_function(Z_OBJCE_P(v), phalcon_db_rawvalue_ce TSRMLS_CC)) {
PHALCON_CONCAT_VSV(pk_condition, escaped_field, " = ", v);
seen_rawvalues = 1;
}
else {
PHALCON_CONCAT_VS(pk_condition, escaped_field, " = ?");
phalcon_array_append(&unique_params, v, 0);
v = NULL;
}

if (!phalcon_array_isset(bind_data_types, field)) {
PHALCON_INIT_NVAR(exception_message);
PHALCON_CONCAT_SVS(exception_message, "Column '", field, "' isn't a part of the table columns");
PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message);
return;
}

PHALCON_OBS_NVAR(type);
phalcon_array_fetch(&type, bind_data_types, field, PH_NOISY);
phalcon_array_append(&unique_types, type, PH_SEPARATE);
if (v) {
PHALCON_OBS_NVAR(type);
phalcon_array_fetch(&type, bind_data_types, field, PH_NOISY);
phalcon_array_append(&unique_types, type, PH_SEPARATE);
}

phalcon_array_append(&where_pk, pk_condition, PH_SEPARATE);

zend_hash_move_forward_ex(ah0, &hp0);
Expand Down Expand Up @@ -1458,6 +1472,10 @@ PHP_METHOD(Phalcon_Mvc_Model, _exists){
PHALCON_INIT_VAR(num);
phalcon_call_method_p4(num, connection, "fetchone", select, null_mode, unique_params, unique_types);

if (seen_rawvalues) {
phalcon_update_property_this(this_ptr, SL("_uniqueKey"), PHALCON_GLOBAL(z_null) TSRMLS_CC);
}

PHALCON_OBS_VAR(row_count);
phalcon_array_fetch_string(&row_count, num, SL("rowcount"), PH_NOISY);
if (zend_is_true(row_count)) {
Expand Down Expand Up @@ -3098,7 +3116,7 @@ PHP_METHOD(Phalcon_Mvc_Model, _doLowInsert){
PHALCON_MM_GROW();

phalcon_fetch_params(1, 4, 0, &meta_data, &connection, &table, &identity_field);

null_value = PHALCON_GLOBAL(z_null);

PHALCON_INIT_VAR(bind_skip);
Expand Down Expand Up @@ -3572,10 +3590,10 @@ PHP_METHOD(Phalcon_Mvc_Model, _doLowUpdate){
*/
PHALCON_INIT_VAR(conditions);
array_init_size(conditions, 3);
phalcon_array_update_string(&conditions, SL("conditions"), &unique_key, PH_COPY | PH_SEPARATE);
phalcon_array_update_string(&conditions, SL("bind"), &unique_params, PH_COPY | PH_SEPARATE);
phalcon_array_update_string(&conditions, SL("bindTypes"), &unique_types, PH_COPY | PH_SEPARATE);
phalcon_array_update_string(&conditions, SL("conditions"), &unique_key, PH_COPY);
phalcon_array_update_string(&conditions, SL("bind"), &unique_params, PH_COPY);
phalcon_array_update_string(&conditions, SL("bindTypes"), &unique_types, PH_COPY);

/**
* Perform the low level update
*/
Expand Down
31 changes: 16 additions & 15 deletions unit-tests/DbDescribeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -518,21 +518,22 @@ public function testDbMysql()

//List tables
$expectedTables = array (
0 => 'albums',
1 => 'artists',
2 => 'customers',
3 => 'm2m_parts',
4 => 'm2m_robots',
5 => 'm2m_robots_parts',
6 => 'parts',
7 => 'personas',
8 => 'personnes',
9 => 'prueba',
10 => 'robots',
11 => 'robots_parts',
12 => 'songs',
13 => 'subscriptores',
14 => 'tipo_documento',
'albums',
'artists',
'customers',
'issue_1534',
'm2m_parts',
'm2m_robots',
'm2m_robots_parts',
'parts',
'personas',
'personnes',
'prueba',
'robots',
'robots_parts',
'songs',
'subscriptores',
'tipo_documento',
);

$tables = $connection->listTables();
Expand Down
77 changes: 76 additions & 1 deletion unit-tests/ModelsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@

use Phalcon\Mvc\Model\Message as ModelMessage;

class Issue_1534 extends \Phalcon\Mvc\Model
{
}

class ModelsTest extends PHPUnit_Framework_TestCase
{

Expand Down Expand Up @@ -75,11 +79,25 @@ public function testModelsMysql()

$di = $this->_getDI(function(){
require 'unit-tests/config.db.php';
return new Phalcon\Db\Adapter\Pdo\Mysql($configMysql);
$db = new Phalcon\Db\Adapter\Pdo\Mysql($configMysql);
/*
$em = new \Phalcon\Events\Manager();
$em->attach('db', function($event, $connection) {
if ($event->getType() == 'beforeQuery') {
echo $connection->getSQLStatement(), PHP_EOL;
print_r($connection->getSQLVariables());
}
});
$db->setEventsManager($em);
*/
return $db;
});

$this->_executeTestsNormal($di);
$this->_executeTestsRenamed($di);

$this->issue1534($di);
}

public function testModelsPostgresql()
Expand Down Expand Up @@ -116,6 +134,63 @@ public function testModelsSqlite()
$this->_executeTestsRenamed($di);
}

protected function issue1534($di)
{
$db = $di->getShared('db');
$this->_prepareDb($di->getShared('db'));

$this->assertTrue($db->delete('issue_1534'));

$product = new Issue_1534();
$product->language = new \Phalcon\Db\RawValue('default(language)');
$product->name = 'foo';
$product->slug = 'bar';
$product->brand = new \Phalcon\Db\RawValue('default');
$product->sort = new \Phalcon\Db\RawValue('default');
$this->assertTrue($product->save());
$this->assertEquals(1, Issue_1534::count());

$entry = Issue_1534::findFirst();
$this->assertEquals('bb', $entry->language);
$this->assertEquals('0', $entry->sort);
$this->assertTrue($entry->brand === NULL);

$this->assertTrue($entry->delete());

$product = new Issue_1534();
$product->language = 'en';
$product->name = 'foo';
$product->slug = 'bar';
$product->brand = 'brand';
$product->sort = 1;
$this->assertTrue($product->save());
$this->assertEquals(1, Issue_1534::count());

$entry = Issue_1534::findFirst();
$entry->brand = new \Phalcon\Db\RawValue('default');
$entry->sort = new \Phalcon\Db\RawValue('default');
$this->assertTrue($entry->save());
$this->assertEquals(1, Issue_1534::count());

$entry = Issue_1534::findFirst();
$this->assertEquals('0', $entry->sort);
$this->assertTrue($entry->brand === NULL);

/* FIXME: this does not work yet
$entry->language = new \Phalcon\Db\RawValue('default(language)');
$entry->language = 'es';
$this->assertTrue($entry->save());
$this->assertEquals(1, Issue_1534::count());
$entry = Issue_1534::findFirst();
$this->assertEquals('es', $entry->language);
$this->assertEquals('0', $entry->sort);
$this->assertTrue($entry->brand === NULL);
*/

$this->assertTrue($db->delete('issue_1534'));
}

protected function _executeTestsNormal($di){

$this->_prepareDb($di->getShared('db'));
Expand Down
12 changes: 12 additions & 0 deletions unit-tests/schemas/mysql/phalcon_test.sql

Large diffs are not rendered by default.

0 comments on commit 6589765

Please sign in to comment.