【问题标题】:cakephp -- getting appropriate model data in complex relationshipscakephp -- 在复杂关系中获取合适的模型数据
【发布时间】:2013-03-25 19:33:01
【问题描述】:

好的。试图尽可能简洁地表达我的问题。我有这个庞大的项目,主要目的是从模型数据生成配置文件。在收集数据库以获取配置信息时,我不知道如何以有效的方式获取适当的信息。模型:地区、部门、用户、运营商。

    • 有很多部门
    • | id | name |
  • 部门
    • hasMany User 通过 DepartmentPosition
    • | id | name | district_id | tone_name | tone_length |
  • 用户
    • hasMany Department 通过 DepartmentPosition
    • 属于承运人
    • | id | name | phone | email | carrier_id | notify_method (tiny int) |
  • 运营商
    • hasMany 用户
    • | id | name | mms_gateway |
  • 部门职位
    • 属于用户、部门
    • | id | user_id | department_id | role |

使用与上述所有模型相关联的字段为每个区域生成一个配置文件。对于一个地区的每个部门,以下内容将添加到一个字符串中,完成后将保存到配置文件中:

[tone_name] //comes from department name
    tone = 123 //from department tone
    tone_length = 4 //from department tone_length
    mp3_emails = person@gmail.com, person2@gmail.com ... //comes from user email
    amr_emails = 5551239999@vzwipix.com //concat of user phone and carrier mms_gateway

基本上,一个部门的 mp3_emails 和 amr_emails 列表应该根据该部门的用户生成,基于 notify_method 用户字段。我已经能够使用以下内容将这些电子邮件字段以外的所有内容放入配置文件中:

//districts controller
public function generate_config(){
    $districts = $this->District->find('All'); 
    $this->set(compact('districts'));
}  

现在,对于视图文件:

    <?php
    $output = '';
    $br = " \r\n";
    $unrelated = array('id', 'name', 'district_id', 'tone_name');
    foreach($districts as $district){
        $name = $district['District']['name'];
        $output .= "#{$name} configuration".$br;
        $departments = $district['Department'];
        foreach($departments as $department){
            $output .= "[".$department['tone_name']."]".$br;
            foreach($department as $key => $value){
                if(!empty($value)&&(!in_array($key, $unrelated))){
                    $output .= $key." = ".$value.$br;   
                }

            }
            $output .= $br;
        }
    }
    echo nl2br($output);
    ?>

输出类似这样的东西

#District 1 config
[Department1]
    tone = 123
    tone_length = 4

[Department2]
    tone = 24.7
    tone_length = 2

我需要做的是为每个部门生成电子邮件列表,但我想不出一个明智的方法来做到这一点。如果departmentsusers 使用HABTM 关系,而不是hasMany through,我觉得会容易得多,我觉得我必须使用它,因为我正在为关系保存额外的数据。有什么建议??不要犹豫,问我是否应该对任何事情更清楚,但我试图提供所有相关信息,同时尽量减少压倒性的细节。谢谢!!!

编辑

感谢@Drewdiddy611,我的理智得以幸免。可包含的行为非常适合这个问题。所以,希望有人能避免彻底的项目大修——我离做这件事还差一点——并使用可包含的行为。

只需将以下内容添加到我的模型中: public $actsAs = array('Containable'); 然后我能够运行查找查询,实现如下建议的可包含行为,我能够在每个District/Department 上为我的迭代添加更大的深度。这非常简单,因为返回的数组就像下面选择的答案中写的一样!!!谢谢!

【问题讨论】:

    标签: cakephp model has-many-through cakephp-2.3


    【解决方案1】:

    我认为您根据您的表格/数据使用“hasMany through”关系是正确的。在 cakephp 2.1 中,他们为 HABTM 关系添加了一个“唯一”键,您可以将其设置为“keepExisting”,它应该可以规避丢失的数据。

    http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasmany-through - 查看主标题下方的“在 2.1 中更改”部分。

    不妨试试看?

    您还可以查看可包含的行为。 然后您可以执行以下操作:

    <?php
    //model
    var $actsAs = array('Containable');
    
    //controller
    $this->District->contain(array(
        'Department' => array(
            'DepartmentPosition' => array(
                'User' => array(
                    'Carrier'
                )
            )
        )
    ));
    $districts = $this->District->find('all');
    $this->set(compact('districts'));
    
    // I believe this will be the outcome after the find.
    ///////////////////////////////////////////////////////
    // array(
    //     [0] => array(
    //        'District' => array(...),
    //        'Department' => array(
    //            [0] => array(
    //                '...' => '...', // repeated...
    //                'DepartmentPosition' => array(
    //                    [0] => array(
    //                        '...' => '...', // repeated...
    //                        'User' => array(
    //                            '...' => '...', // repeated...
    //                            'Carrier' => array(
    //                                '...' => '...', // repeated...
    //                            )
    //                        )
    //                    )
    //                ) 
    //            )
    //        )
    //    )
    // )
    
    ?>
    

    -安德鲁

    【讨论】:

    • 你愿意嫁给我吗?我即将对我的设计进行大刀阔斧的改变——毫无疑问,这在时间/金钱方面会非常昂贵。太有用了,不仅对这个问题,对许多其他问题也是如此。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-31
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多