Two classes
class Status extends \Phalcon\Mvc\Model {
public $id;
public $title;
public function initialize()
{
$this->hasMany( "id", "Tickets", "status_id");
}
}
class Tickets extends \Phalcon\Mvc\Model {
public $id;
public $status_id;
public function initialize()
{
$this->belongsTo('status_id', 'Status', 'id',
array('alias' => 'Status', 'foreignKey' => true));
}
}
But when I do
$tick = new Tickets();
$status = new Status();
$tick->status = $status;
$tick->save();
I receive an error Recoverable fatal error: Object of class Status could not be converted to string in Before save I can receive echo $tick->status->title; all works fine at this step.
CREATE TABLE `status` (
`id` int(11) NOT NULL,
`title` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `tickets` (
`id` int(11) NOT NULL,
`title` varchar(100) NOT NULL,
`text` text NOT NULL,
`createdon` datetime DEFAULT NULL,
`closedon` datetime DEFAULT NULL,
`editedon` datetime DEFAULT NULL,
`status_id` int(11) DEFAULT NULL,
`user_id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `status`
ADD PRIMARY KEY (`id`),
ADD KEY `id` (`id`);
ALTER TABLE `tickets`
ADD PRIMARY KEY (`id`),
ADD KEY `status_id` (`status_id`),
ADD KEY `user_id` (`user_id`);
ALTER TABLE `tickets`
ADD CONSTRAINT `tickets_ibfk_1` FOREIGN KEY (`status_id`) REFERENCES `status` (`id`),
ADD CONSTRAINT `tickets_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`);
COMMIT;
Mysql 5.6.35 + PHP-FPM 7.1 + NGINX 1.10.1 + Phalcon 3.2.0
And when I retrive records all works,
$tick = findFirstById(17);
echo $tick->getStatus()->getTitle();
or
echo $tick->status->title;