【问题标题】:Database Pick List in CakePHP?CakePHP 中的数据库选择列表?
【发布时间】: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


    【解决方案1】:

    我认为在 CakePHP 中使用它没有任何问题。我理解@Anh Pham 说这可能“很麻烦”,但老实说,如果您的网站比博客教程稍微复杂一点,那么在 Cake 中几乎所有与数据库相关的内容都会成为麻烦。

    Cake 是我选择的 PHP 框架,但我相信您必须使用它一段时间并尝试了解“魔法”是如何工作的。当你这样做时,更容易决定什么时候应该让 Cake 自己处理一些数据库操作,以及如何以一种优雅的方式诱使它做你想做的事情。

    “选择列表”结构可以与 Cake 的模型关联配合得很好,只需在声明关联时添加正确的 conditionsforeignKey 即可。

    更新

    我可以从最新的编辑中看出您走在正确的轨道上。我相信您现在明白了为什么您的 $this-&gt;BirthplaceCounty-&gt;find('list') 调用会返回整个表:模型不知道您要按 value_list_id = 5 (5 还是 8?)进行过滤,该条件仅适用于它与其他模型的关联.

    无论如何,除了重载find(),您还有其他选择:您可以在模型的beforeFind 回调中定义默认条件,如下所示:

    function beforeFind($queryData) {
        $defaultConditions = array("BirthplaceCounty.value_list_id = 5");
        if(!empty($queryData['conditions'])) {
            // Make sure 'conditions' is an array, so we can array_merge
            $queryData['conditions'] = is_array($queryData['conditions']) : $queryData['conditions'] : array($queryData['conditions']);
            // Merge conditions passed to find with the default
            $queryData['conditions'] = array_merge($defaultConditions, $queryData['conditions']);
        } else {
            $queryData['conditions'] = $defaultConditions;
        }
        // Return the modified $queryData to be used by find
        return $queryData;
    }
    

    【讨论】:

    • 感谢您的回复 - 您能否更具体地说明您将使用哪些“条件”和“外键”?您需要为每个列表创建一个单独的模型吗?
    • 用重载 find 方法的替代解决方案更新了我的答案。
    【解决方案2】:

    创建单独的表格将是蛋糕的做法。换一种方式会很麻烦。

    为了保持低开销,您可以使用 here 中的 ArraySource,这样您就不必为这些东西查询数据库,同时仍然拥有使用模型的所有便利。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多