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?