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

setting One related model to the newly creating model will update whole that

I have two models . Office:

class Office extends Model
{

    public $id;

    public $parent_id;

    public $name;

    public $is_delegate;

    public function initialize()
    {
        $this->belongsTo(
            'parent_id',
            'Gordan\Models\Office',
            'id',
            ['alias'=>'parent']
        );
        $this->hasMany(
            'id',
            'Gordan\Models\Office',
            'parent_id',
            ['alias'=>'children']);
    }

    public function afterFetch()
    {
        $this->is_delegate = (bool) $this->is_delegate;
    }

And Person:

class Person extends Model 
{
    public $id;

    public $firstname;
    //other fields

    public $office_id;

    public function initialize()
    {
        $this->useDynamicUpdate(true);
        $this->belongsTo('office_id', 'Mine\Models\Office', 'id', ['alias'=>'office']);
    }
}

Offices are created and exist in the database. When I want to create a Person and assign office to it,I use this:

    //User select office from drop-down, Don't think about security measures please.
   $officeName = $this->request->getPost('office');
   $office = Office::findFirst("name ='" . $officeName."'");
   $person = new Person();
   $person->office = $office;
   if($person->save() === true) {
       echo 'ok';
   }else {
       foreach($person->getMessages() as $message) {
           echo $message->getMessage() . PHP_EOL;
       }
   }

The problem is, it doesn't show ok,It shows:

is_delegate is required

why it happens?



5.1k
Accepted
answer
edited Aug '17

You don't need to affect offce object in the person object

just affect the office id


$person->office_id = $office.id;

you have to affect a value in $is_delegate too,

  • in controller : '$person->is_delegate = false;`

  • or in the model : public $is_delegate = false;`
edited Aug '17

But it is in the docs: Storing related records

Is documentation wrong or that's because example in the docs is a newly created model and not an existing row in the database?



5.1k
edited Aug '17

I never use it but first your error is not here

see your error message

is_delegate is required

You must assign values for all database not null items

And are you sure of your relationships

  • Office model have no relationship with Person

  • Office has 2 relationships with himself

Can be, to do this you must have the relationship declared in the 2 models

Well I want my relationship be unidirectional,not Bidirectional, that's why I didn't declare the relationship in the office and declare it just in Person model. You say all of my relationships should be bidirectional in order to work?



5.1k

I do not know which of the 2 models makes the relationship i suspect by Office because the action to add Person object is made in Office and Office contains no information to make it