Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

ORM Exception not working - columnMap model update

Hi Team,

I have a problem with $model->update when using columnMap on models. Exceptions/Validation seems not working during update of data, but I have no problem with $model->save/create. I tried to update a non-existent column/property, even wrong fields but $model->update() is returning TRUE which should be false since these fields are not on the model.

Here is my model:

class Account extends Model {

public $id;

public $firstName;

public $lastName;

public $email;

public function columnMap()
{
    return [
        'id' => 'id',
        'first_name' => 'firstName',
        'last_name' => 'lastName',
        'email' => 'email',
    ];
}

}

On My controller:

//this is working

$account = new Account();

$account->firstName = $firstName; //post data

$account->lastName = $lastName; //post data

$account->email = $email; //post data

var_dump($account->create()); //save - return TRUE

var_dump($account->getMessages());

//this is working with error messages because first_name is not firstName (map)

$account = new Account();

$account->first_name = $firstName; //post data

$account->last_name = $lastName; //post data

$account->email = $email; //post data

var_dump($account->create()); //save - return FALSE

var_dump($account->getMessages());

However I expect the same behaviour on model update but not working..

//this is working because colums are correct

$account = Account::findFirst(2);

$account->firstName = $firstName; //post data

$account->lastName = $lastName; //post data

$account->email = $email; //post data

var_dump($account->update()); //update/save - return TRUE OK HERE

var_dump($account->getMessages());

//this is not working because colums are not correct but it RETURNS TRUE

$account = Account::findFirst(2);

$account->first_name = $firstName; //invalid col

$account->last_name = $lastName; //invalid col

$account->email_error = $email; //email_error is not valid

var_dump($account->update()); //update/save - return TRUE but no update on the database

var_dump($account->getMessages());

I found a related query here but its PHQL - PHQL seems working here

https://forum.phalconphp.com/discussion/4355/phql-and-columnmap https://github.com/phalcon/cphalcon/issues/10344 https://github.com/dreamsxin/cphalcon/commit/91520ad9869fcacb28bf44e2ded686287e91f2bf

Help!

Thanks,



3.2k
edited May '17

Hi,

You mapping and your examples 1 and 3 seem Ok

$account = Account::findFirst(2);
$account->first_name = $firstName; //invalid col
$account->last_name = $lastName; //invalid col
$account->emailerror = $email; //emailerror is not valid
var_dump($account->update()); //update/save - return TRUE but no update on the database
var_dump($account->getMessages());

it's logic because each setter return an error Your account instance is not modified and you save the same values

the update work but you have no difference to compare

If you want to see this Try a var_dump($account) after find and another before save; Try with one or 2 good setter



397

But it should not return 'True' as result because the 'first_name', 'last_name', 'emailerror' are not part of the model properties.

I want to know if the update is successful or not (num_rows_affected). I am using

if ($account->save()) { //ok here } else { //flash the error }

Thanks,



42.4k
Accepted
answer

The culprit is that the update is changing nothing, since you are not really updating any mapped columns. A model instance can have extra fields (first_name), and those will be ignored when writing/reading DB.

This is expected behaviour, YOU should be aware of the proper field/column names ;]



397

Nice... I didn't know that extra fields / non-existent fields are ignored automatically. I was expecting for an error there. (though I am just testing the behaviour).

So basically, the ORM/Model will return True either.

Thanks,

The culprit is that the update is changing nothing, since you are not really updating any mapped columns. A model instance can have extra fields (first_name), and those will be ignored when writing/reading DB.

This is expected behaviour, YOU should be aware of the proper field/column names ;]