使用两个表,数据检索将转到何处(MVC 中的第一步)


Using two tables, where does the data retrieval go (First step in MVC)

我是MVC扩展的新手(甚至是基于对象的)。所以从PhP 4.*开始,我进入了OOP,MVC和Cake。

我正在建立一个网站,来自不同国家的研究所可以用来存储他们的数据(以及更多)。我现在正在建立每个机构的基本注册,并希望包括一个国家的下拉列表。

我认为有两种方法可以解决这个问题;使用国家/地区模型检索下拉列表的国家/地区表信息: $this->set('countries', ClassRegistry::init('Country')->getAllCountries()); (后跟 ''model''Country.php 中的函数)

或使用研究所控制器:
$this->set('countries', $this->Institute->Country->find('list', $params = array('fields' => array('id', 'country'))));

推荐的路线是哪条,因为两者似乎都有效?

使用第二个:

$this->set('countries', $this->Institute->Country->find('list', $params = array('fields' => array('id', 'country'))));

两者都有效,但是,您已经有了国家/地区模型的实例(可通过$this->Institute->国家/地区)访问)。那么,为什么要创建它的另一个实例呢?只是没有必要。

实际上,不需要为调用 find 方法指定字段。"id"将自动选择为第一个字段,如果您将国家/地区模型的显示字段设置为"国家/地区",则这将是 find('list') 调用中的默认用法。这样做:

// just after class Country extends AppModel {
public $displayField = 'country';

然后,您只需要使用以下代码:

$this->set('countries', $this->Institute->Country->find('list'));