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

Storing records

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; 

The error you're getting could happen if you have a status column in the tickets table, or you had such a column and it's still cached in the meta data. Are you using a different MetaData adapter than Phalcon\Mvc\Model\MetaData\Memory ?



3.4k

Column in the tickets table named "status_id" , not "status". Never use MetaData adapters, maybe it is possible to clean this cached data?

You need to find out if you're using a MetaData adapter, and if it's the Memory one or not. The Memory adapter is usually set up like this:

use Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter;

$di->setShared('modelsMetadata', function () {
    return new MetaDataAdapter();
});

See documentation for more examples.

Hi @danseciu the Phalcon\Mvc\Model\Metadata\Memory is by default itsn't necesary setup



5.1k
edited Jul '17

it's the alias name

You can not use the same name for the alias and the model


        $this->belongsTo('status_id', 'Status', 'id', 
            array('alias' => 'Status', 'foreignKey' => true));


3.4k

It works, thank you phil67000!

I define alias ticketStatus and create private $ticketStatus and setters/getters for them and all works perfectly.



5.1k

no need to add private $ticketStatus and setters/getters

juste use :


$tick->ticketStatus->title;

it's magic ;-)