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

How to set Dynamic update to be true for all models?

I want to set all the models to useDynamicUpdate. I didn't find the global setting in model Features.

And Is there any advantages when the dynamicUpdate is set to be false?



6.9k

And is there any method like useDynamicCreate() ? Because most column values is also not necessary when creating an object.



51.1k

You can create a BaseModel and all the other models can extend the Base one

class MyBaseModel extends \Phalcon\Mvc\Model
{
    public function initialize()
    {
        $this->useDynamicUpdate(true);
    }
}
class ExampleModel extends MyBaseModel
{
    // use parent::initialize if you need a initialize() method here
    public function initialize()
    {
        parent::initialize();

        // Your code 
        // ....
    }
}

No, there is no useDynamicCreate() method. But you can skip columns. Check https://docs.phalcon.io/en/latest/reference/models.html#skipping-columns



85.5k

I want to set all the models to useDynamicUpdate. I didn't find the global setting in model Features.


class BaseModel extends Phalcon\MVC\Model {
    public function initialize()
    {
        $this->useDynamicUpdate(true);
    }
}

// next model

Class MyFirstmodel extends **BaseModel** {
    public function initialize()
    {
        parent::initialize();
    }

    //bla bla bla 
}

// and so on and so on

And is there any method like useDynamicCreate()

this one i dont understand. Lets say we have a table with 3 fields -> id ( int AI ), name ( varchar 255, default null ), deleted ( bool) default false )

if you do


$rec = new \MyTableModel();
$rec->name = 'koko';

the insert would be:

1 | koko | false

so.. it will write values you defined, but it would also respect how you sctuctured your databases



6.9k

Thanks! And why is the default value of DynamicUpdate to be false. Is there any benefits?

You can create a BaseModel and all the other models can extend the Base one

class MyBaseModel extends \Phalcon\Mvc\Model
{
   public function initialize()
   {
       $this->useDynamicUpdate(true);
   }
}
class ExampleModel extends MyBaseModel
{
  // use parent::initialize if you need a initialize() method here
   public function initialize()
   {
       parent::initialize();

      // Your code 
      // ....
   }
}

No, there is no useDynamicCreate() method. But you can skip columns. Check https://docs.phalcon.io/en/latest/reference/models.html#skipping-columns

Hi,

it's not enabled by default, because if you want to use the dynamic update, you need to keep model snapshot

https://github.com/phalcon/cphalcon/blob/master/phalcon/mvc/model/manager.zep#L669

That means you need more cpu time on select & doubles your ram usage.