如何在CakePHP上使用没有数据库的模型并具有关联? [英] How to use models without database on CakePHP and have associations?

查看:136
本文介绍了如何在CakePHP上使用没有数据库的模型并具有关联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型中有字段 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.

请帮助。提前感谢!

推荐答案

我建议从社区使用 ArraySource - 保持CakePHP 数据源插件

I would suggest using ArraySource from the community-maintained CakePHP Datasources plugin:

  • CakePHP 1.3.x - see master branch
  • CakePHP 2.x - see 2.0 branch

下载整个插件并将内容解压缩到 app / plugins / datasources

Download the entire plugin and extract the contents to app/plugins/datasources.

定义 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆