Model load and save

if loaded now update , if not found save new record
how to fix it
$model->addCondition(‘model’, $sub_type);
$model->addCondition(‘model_main’, $type);
$model->tryLoadBy(‘model_id’, $id)->save();

I have an error :
Application Error: Notice:
Indirect … modification of overloaded element of Model_x has no effect

I might not understand you correctly as the question is unclear, but normally:

  1. specify that id is stored inside “model_id” field

    class MyModel extends SQL_Model {
    public $id_field=‘model_id’;

  2. load model with load($id) if you expect model to be there. Exception can be handled on application level.

  3. Otherwise use tryLoad and $model->loaded() to see if it was loaded successfully:

  4. finish saving.

Looks like you are somewhat trying to load models from database at runtime.

But what is unclear to me is … Why you are putting conditions when you have primery_key … Just load it with primarykey

sorry my mistake
ploblem is
$this->addHook(‘beforeSave’, function($m) {
$m[‘count’] = $m[‘count’]++; --> can’t use
// fixed to $m[‘count’] = $m[‘count’]+1; --> working

That’s a weird limitation of PHP. Can’t use ++ with arrayAccess (because $m is the object, not actually an array)