【发布时间】:2011-10-04 21:11:34
【问题描述】:
我正在为一家心理健康机构设计数据库,临床医生从预先格式化的选项列表中选择很多类别(就业状况、住房状况、关系状况等)。数据库,我们使用了一个类似于 stackoverflow here 中描述的选择列表,以避免为每个列表创建表。
我们计划使用 CakePHP 作为新构建的框架,我担心 CakePHP 的“自动”能力很容易保存和显示数据,不能很好地与使用选择列表结构的数据库配合使用。我正在考虑为每个列表创建一个单独的表,以利用 CakePHP 的 automagic。
有没有人有在 CakePHP 中实现选择列表的经验或有关于替代设计方法的建议?
编辑:我正在尝试使用两个表来实现这一点 -
value_lists list_values
----------- -------------------------------
id | list_name id | list_value | value_list_id
这是我的型号代码:
<?php
class BirthplaceCounty extends AppModel {
var $name = 'BirthplaceCounty';
//The Associations below have been created with all possible keys, those that are not needed can be removed
var $useTable = 'list_values';
var $displayField = 'list_value';
var $hasMany = array(
'Registration' => array(
'className' => 'Registration',
'foreignKey' => 'birthplace_county_id',
'dependent' => false,
'conditions' => array('BirthplaceCounty.value_list_id' => '8'),
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => '',
)
);
}
我的问题似乎是无论我尝试什么条件 -
$this->BirthplaceCounty->find('list');
or
$this->BirthplaceCounty->find('all');
总是返回 list_values 中的所有记录。 CakePHP 生成的 SQL 是
SELECT `BirthplaceCounty`.`id`, `BirthplaceCounty`.`list_value`
FROM `list_values` AS `BirthplaceCounty` WHERE 1 = 1
无论我如何改变条件,WHERE子句总是1 = 1。如何正确写条件?
编辑 2:好的 - 我意识到在 $hasMany 变量中设置条件不会影响 find() 函数。我创建了一个有效的新函数。
function getActual() {
$conditions = array('BirthplaceCounty.value_list_id = 5');
return $this->find('list', compact('conditions'));
}
是否只有通过重载 find() 函数才能做到这一点?
【问题讨论】:
标签: sql database-design cakephp