如何在 CakePHP 上使用没有数据库的模型并具有关联? [英] How to use models without database on CakePHP and have associations?
问题描述
我在我的一个模型中有字段 country_id
,而不是创建一个 countries
表,其中包含一个不变的国家列表,这是什么最好的方法是?
I've got the field country_id
in one of my models and instead of creating a countries
table which contains a list of countries that doesn't change, what's the best approach to this?
我正在考虑使用没有数据库表的模型,但我不知道如何实现.
I'm thinking about using a model without a database table but I don't know how to implement this.
请帮忙.提前致谢!
推荐答案
我建议使用社区维护的 CakePHP 中的 ArraySource
数据源插件:
I would suggest using ArraySource
from the community-maintained CakePHP Datasources plugin:
- CakePHP 1.3.x - 参见
master代码>分支
- CakePHP 2.x - 参见
2.0代码>分支
下载整个插件并将内容提取到app/plugins/datasources
.
Download the entire plugin and extract the contents to app/plugins/datasources
.
在app/config/database.php
中定义与数据源的连接:
Define a connection to the datasource in app/config/database.php
:
public $array = array('datasource' => 'Datasources.array');
这应该允许您通过在模型中定义记录来模拟表:
This should allow you to emulate a table by defining records in your model:
class Country extends AppModel {
public $useDbConfig = 'array';
public $records = array(
array('id' => 1, 'name' => 'Test record')
);
}
这篇关于如何在 CakePHP 上使用没有数据库的模型并具有关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!