Just in case full source to stackoverflow question: https://stackoverflow.com/questions/40817270/phalcon-3-0-model-relationships
I'm building a tiny project using Phalcon 3.0.1 just to learn how to use ORM model relationships between two tables and just on the start encountered first problem with joining data from related tables. Here's my SQL code for those tables:
CREATE TABLE `jobs` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(100) NOT NULL,
`categories_id` int(11) unsigned NOT NULL,
`location` varchar(255) NOT NULL,
`description` text NOT NULL,
`how_to_apply` text NOT NULL,
`author` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`),
KEY `fk_categories` (`categories_id`),
CONSTRAINT `jobs_ibfk_1` FOREIGN KEY (`categories_id`) REFERENCES `categories` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COMMENT='utf8_general_ci'
CREATE TABLE `categories` (
`id` int(11) unsigned NOT NULL,
`category_name` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='utf8_general_ci'
Jobs has a foreign key for categories (categories_id)
. Now, trying to init relationship in my categories model like:
public function initialize()
{
$this->hasMany('id', 'Jobs', 'categories_id', ['alias' => 'Jobs']);
}
Also I've tried put full namespace (App\Core\Models\Jobs
) for hasMany and still is the same.
Now when calling find method Categories::find() is not returning related data from Jobs table. I've also notice that I can put anything stupid inside hasMany() method and it even won't catch exception. It looks like it is completely ignored. I can even do something like below and it won't crash.
public function initialize()
{
$this->hasMany('stupid', 'Doesnt_exist_table', 'more_stupid', ['alias' => 'Jobs']);
}
I would like to also outline that all properties in my models are public. I've gone trough all the docs, other stackoverflow questions, github examples and I believe that should work. Any help will be appreciated as I'm going mad about it.