Hello,
Given the following two very basic models:
class User extends \Phalcon\Mvc\Model {
public $id;
public $password;
public function initialize() {
$this->hasOne('id', 'Person', 'user_id', [
'alias' => 'corresponding_person'
]);
}
}
// CREATE TABLE User (
// id int(10) NOT NULL AUTO_INCREMENT,
// password VARCHAR(30),
// PRIMARY KEY (id)
// );
class Person extends \Phalcon\Mvc\Model {
public $id;
public $user_id;
public $name;
public function initialize() {
$this->hasOne('user_id', 'User', 'id', [
'alias' => 'corresponding_user'
]);
}
}
// CREATE TABLE Person (
// id int(10) NOT NULL AUTO_INCREMENT,
// user_id int(10) NOT NULL,
// name VARCHAR(30),
// PRIMARY KEY (id)
// );
and this controller:
class IndexController extends ControllerBase {
public function indexAction() {
$User = new User();
$User->password = 'secret';
$User->save();
$Person = new Person();
$Person->name = 'John';
$Person->corresponding_user = $User;
if (! $Person->save()) {
die(
'Messages : ' . print_r($Person->getMessages(), true) . "\n" .
'$User->id : ' . $User->id
);
}
}
}
When I run the controller, I get:
Messages : Array
(
[0] => Phalcon\Mvc\Model\Message Object
(
[_type:protected] => PresenceOf
[_message:protected] => user_id is required
[_field:protected] => user_id
[_model:protected] =>
[_code:protected] => 0
)
)
$User->id : 4
$User
is saved, but not $Person
.
It works if I use $Person->user_id = $User->id;
but shouldn' it also work using the relationship defined in the model?
(Note: removing the relationship aliases and using $Person->User = $User
produces the same error)
Thanks in advance for any help