我正在使用 Phalcon 3.0.1 构建一个小型项目,只是为了学习如何在两个表之间使用 ORM 模型关系,并且刚开始就遇到了从相关表连接数据的第一个问题。这是我对这些表的 SQL 代码:
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 有一个类别的外键 (categories_id)。现在,尝试在我的类别模型中初始化关系,例如:
public function initialize()
{
$this->hasManay('id', 'Jobs', 'categories_id', ['alias' => 'Jobs']);
}
现在调用 find 方法时Categories::find()
不会从 Jobs 表中返回相关数据。我还注意到我可以在hasMany()
方法中放入任何愚蠢的东西,它甚至不会捕获异常。看起来它完全被忽略了。我什至可以做类似下面的事情,它不会崩溃。
public function initialize()
{
$this->hasManay('stupid', 'Doesnt_exist_table', 'more_stupid', ['alias' => 'Jobs']);
}
我还想概述一下我的模型中的所有属性都是公开的。我已经浏览了所有文档、其他 stackoverflow 问题、github 示例,我相信应该可以。任何帮助都将不胜感激,因为我对此很生气。