White list for Model updating

Hello,

please can anybody tell me how to use white list while updating or creating the Model?

For example, let's say we have Model for sql table news with three columns:

id, title, contents

$entity = new \News();
$entity->id = 10;
$entity->title = uniqid('New title ');

// Few examples of $whiteList tryings

//$whiteList = 'title';
//$whiteList = 'news.title';
$whiteList = get_class(entity).'.title';

$entity->update(null,$whiteList);

No matter how i try to pass title to the white list so it should(?) ignore rest of the columns while updating, update is generating error: "contents is required".

Of course I can dynamically create PHQL query for update, but I thought it should be possible in the ORM.

I don't think that's how the whitelist works. The whitelist doesn't get around validation - which is causing your problem. Basically you've set up your model or (likely) database to require that "contents" be provided. A whitelist won't allow you to ignore that.

edited Feb '17

Here is how I always used it.

$obj = ModelName::findFirst(25); // Just get the record with ID 25 for the test.
$result = $obj->save(
    $this->request->getPost(),
    ['title', 'slug'] // Allowed columns for the update
);
edited Feb '17

Whitelist should be an array with names of fields you want to be possible to update.

You have contents is require because by default phalcon don't allow not null values to be saved. You just need to:

Model::setup(
    [
        'notNullValidations' => false,
    ]
);
edited Feb '17

Thank You guys for answers but every presented solution is not what I need or looking for:

@Dylan Anderson - that means it's basically useless for me

@Nikolay Mihaylov - sorry but i do everything dynamically / on abstraction layer ( no lines of code like Robot::fetch(25) ) and it's not too good to fetch whole record first (while I don't need it) just to do update of one field

@Wojciech Ślawski - no this is wrong too, as I belive it works statically at initialisation stage. What if i need to swith notNullValidations to true in the same http request? Also - i need not null validation for the selected fields I want to update.

Now i just create dynamically PHQL to update and/or create records, it's easy and do exactly what I need.

Edit: well it doesn't matter, looks like I maybe need different ORM anyway, or create plain SQL queries, as low level SQL logging shows even if i use PHQL with selected columns for update, this ORM fetches whole single record before it finally updates it, and its full update on all fields not just those in the PHQL statement...

edited Feb '17

Just add presenceof on them and disable notNullValidations. You need to set dynamic update to only update changed fields in query. No need to use raw sql. Phalcon ORM is really great and good. All orm fetches whole record before updating it obviously.

edited Feb '17

Dynamic update seems to work, it updates only changed fields, it means when table has big records, thanks.

I work often on high volume of data and must keep logic on db side in the triggers and stored procedures.

In such case i need raw SQL or elastic ORM allowing to do mass update omitting ORM logic / fetching every updated record, so sometimes raw SQL is usefull.

Yes this why it's ORM object-relational-mapping

edited Feb '17

Yes mapping between object and relations in database, simple as that, nothing more.

I don't need or want to keep data logic on php side, and replace db triggers by php methods, I want to keep it on DB side at data layer and use ORM for simple CRUD operations, nothig more.

After you will deal with billions of records and tens of gigabytes of data you will know what im taking about :)

Then don't use ORM if you want it cuz simply you don't know what orm is :P