We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

is there some way to partial update db field?

the db fields like that:

  id not null
  username not null
  email not null
  province default null
  city default null
  age default null

when I execute

    $user->update(['province'=>'aa', 'city'=>'bb', 'age'=>22])

all is fine.... But, after that, if I execute

    $user->update(['province'=>'NY'])

then, the fields of city and age will be set null value, I have check the log file, it realy send

SET province = 'NY', city=null, age=null

someone tell me why? Because I want to use ajax to update one column every request...

edited Apr '14

You can use dynamic updates: https://docs.phalcon.io/en/latest/reference/models.html#dynamic-update

Another issue might be that you're sending the data in your call to update. Perhaps if you just specifically set that property, then call update(), it won't do that:

$User = User::getFirst(...);
$User->province = 'NY';
$User->update();


9.3k

It does not work....and then that is not a good solution, you need to get the record/per update

How else would you do it then? If the object is alive, the update will continue trying to update fields until the dispatch ends.

Correct me where I am wrong, but here is how I see this:

  • You have a page that you enter data for an existing record.
  • You update the record with the new data
  • Dispatch ends

and repeat the above steps.

What should work is:

// Assume user.id = 1

$user = new User();
$user->id = 1;
$user->province = 'aa';
$user->city = 'bb';
$user->age = 22;
$user->update();

For the next update you can do:

// Assume user.id = 1

$user = new User();
$user->id = 1;
$user->province = 'NY';
$user->update();

Pickle's solution should also work. You get the record, update the fields and use either save or update.

You may be able to pass the user id along with your request, then just make a new User with that id, and call save() rather than update(). That may just cause Phalcon to set that one column. Phalcon does lots of work behind-the-scenes though, so I'm just guessing.



9.3k

that is very strange... i have write the hard code to test, but still auto set null in sql...



9.3k
edited Apr '14

my test code:

    // controller
    public function saveAction($id)
    {
        $user = new Users();
        $user->id = $id;
        $user->province = 'NY';
        $user->edit();
    }

    //model
    public function edit()
    {
        $this->skipAttributes(['email', 'username']);
        $this->save();
        echo implode(';', $this->getMessages());
    }

the debug.log message:

    [Tue, 08 Apr 14 01:00:02 +0800][INFO] SELECT IF(COUNT(*)>0, 1 , 0) FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_NAME`='users'
    [Tue, 08 Apr 14 01:00:02 +0800][INFO] DESCRIBE `users`
    [Tue, 08 Apr 14 01:00:02 +0800][INFO] SELECT COUNT(*) "rowcount" FROM `users` WHERE `id` = ?
    [Tue, 08 Apr 14 01:00:02 +0800][INFO] UPDATE `users` SET `province` = ?, `city` = null, `age` = null WHERE `id` = ?

Try this:

    //model
    public function edit()
    {
        $this->skipAttributes(['email', 'username', 'city', 'age']);
        $this->save();
        echo implode(';', $this->getMessages());
    }

or

    //model
    public function edit()
    {
        $this->skipAttributes(['email', 'username']);
        $this->update();
        echo implode(';', $this->getMessages());
    }

what do you get from each scenario?



9.3k

in first code, it work! the sql only set the province without other fields...and the second code, do not work...

but the first code is not flexible... that means i need to write a month for every one column in model, do you have other method to solve the problem?



9.3k

hello, does anyone tell me why??? I think that is a bug, when I update one field why you set other field to null value. that logic is unreasonable, which logic is right for insert operation, but not suitable for update... @niden @quasipickle

That's a bug!! I have the same problem with @Barbery :-( Have no way to update only some fields I wanted without set other fields to NULL? The method useDynamicUpdate(true) doesn't work and I dont want to use method skipAttributes() because it is not flexible

I agree with you, It is a waste to update the other fields. @phalcon do you plan to fixed this as a bug ?

hello, does anyone tell me why??? I think that is a bug, when I update one field why you set other field to null value. that logic is unreasonable, which logic is right for insert operation, but not suitable for update... @niden @quasipickle

How do we get around the dateCreated NULL on update dateUpdated? Even when the field is set via mySQL col settings it still overwrites the dateCreated.

Currently I have

public function beforeCreate()
{
    //Set the creation date
    $this->dateCreated = date('Y-m-d H:i:s');
}

public function beforeUpdate()
{
    //Set the modification date
    $this->skipAttributesOnUpdate(['dateCreated']);
    $this->lastUpdated = date('Y-m-d H:i:s');
}

But this means I have to specify and capture all columns I wish not to update.

edited Feb '18

For update only custom fields

public function edit($fields)
{
    $metadata = $this->getModelsMetaData();
    $attributes = $metadata->getAttributes($this);
    $this->skipAttributes(array_diff($attributes, $fields));
    return $this->save();
}

Usage:

$model->edit(['lastUpdated', 'name']);