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

useDynamicUpdate(true); not working for partial update

I am trying to update a row partially .... just change its "status" column however the "update" method of the Phalcon\Mvc\Model fails because some of the other columns are "required" in the database and it seems like the "update" method inserts nulls for those.

Here is what my model looks like

class MyModel extends \Phalcon\Mvc\Model
{

    public function getSource() {
        return 'table_name';
    }

    public function initialize()
    {
        $this->setConnectionService('dbconn');
        $this->useDynamicUpdate(true);
    }

    public function myUpdate(array $data)
    {
         $this->update($data);
    }

The variable $data above in the myUpdate function contains the following

  array('id'=>2, 'status'=>0)

I am using onValidationFails hook to print_r() the messages and I get the following:

Array
(
    [0] => Phalcon\Mvc\Model\Message Object
        (
            [_type:protected] => PresenceOf
            [_message:protected] => firstname is required
            [_field:protected] => firstname 
            [_model:protected] => 
        )

    [1] => Phalcon\Mvc\Model\Message Object
        (
            [_type:protected] => PresenceOf
            [_message:protected] => lastname is required
            [_field:protected] => lastname 
            [_model:protected] => 
        )

    [2] => Phalcon\Mvc\Model\Message Object
        (
            [_type:protected] => PresenceOf
            [_message:protected] => username is required
            [_field:protected] => username 
            [_model:protected] => 
        )

    [3] => Phalcon\Mvc\Model\Message Object
        (
            [_type:protected] => PresenceOf
            [_message:protected] => password is required
            [_field:protected] => password 
            [_model:protected] => 
        )
)

What do I need to do in order to update only the status column while leaving the other ones intact?!



11.8k

In your phpmyadmin, set nullable for those fields.



13.7k
edited Sep '14

Hello,

I am having the same problem... I only want to update some of the fields in the row and leave the others intact.

$this->setup( array('notNullValidations'=>false) );

$this->useDynamicUpdate(true);

Is this possible?

Tim



43.9k

Hi,

maybe skipping the required attributes on Update action will do the trick:

   public function initialize()
   {
       $this->skipAttributesOnUpdate(array('firstname', 'lastname'));
   }

So useDynamicUpdate(true) does not work as intended?



43.9k
edited Mar '16

did you try the trick above ?

Yes I did and It works, but my point was about useDynamicUpdate(true) not working as intended. Is this considered a bug? If so please point me to the bug because I cannot seem to find one related to this issue.

Thanks.