我正在使用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示例,我相信这应该工作。任何帮助都会受到赞赏,因为我很生气。
答案 0 :(得分:0)
测试用例:
class Jobs extends Phalcon\Mvc\Model
{
public function initialize()
{
$this->belongsTo('categories_id', 'Categories', 'id');
}
}
class Categories extends Phalcon\Mvc\Model
{
public function initialize()
{
$this->hasMany('id', 'Jobs', 'categories_id');
}
}
class TestController extends Phalcon\Mvc\Controller
{
public function testAction()
{
// get the first job
$job = Jobs::findFirst();
// get the (single) category that is related to this job
$jobCategory = $job->getRelated('Categories');
// get the first category
$category = Categories::findFirst();
// get all the jobs related to this category
$jobsInThisCategory = $category->getRelated('Jobs');
}
}
只有在对象上->getRelated()
时,才会调用相关记录
因此,如果您将关系定义为
$this->hasMany('stupid', 'Doesnt_exist_table', 'more_stupid', ['alias' => 'Jobs']);
您致电$category->getRelated('Jobs')
,您将收到错误,因为该表Doesnt_exist_table
不存在。
答案 1 :(得分:0)
<section class="slideshow">
<div class="forsale">
<a href="#"><span class="half"><span class="line-hide">te koop</span></span></a>
</div>
<img src="http://lorempixel.com/100/500/sports/1" alt="">
</section>
<?php
namespace App\Models;
use Phalcon\Mvc\Model;
class Categories extends Model
{
// ....
public function initialize()
{
$this->hasMany('id', __NAMESPACE__ . '\Articles', 'categories_id', array(
'alias' => 'Categories',
'reusable' => true
));
}
}
在控制器中:
use Phalcon\Mvc\Model;
class Articles extends Model
{
// ....
public function initialize()
{
$this->belongsTo("categories_id", __NAMESPACE__ . "\Categories", "id", array(
'alias' => 'Categories',
'reusable' => true
));
}
public function getCategories($parameters = null)
{
return $this->getRelated('Categories', $parameters);
}
}
在视图中(伏特):
public function indexAction()
{
$articles = Articles::find([
'order' => 'id DESC',
'limit' => 2
]);
$this->view->setVar('articles',$articles);
}