cakephp——在复杂关系中获取适当的模型数据

cakephp——在复杂关系中获取适当的模型数据,cakephp,model,has-many-through,cakephp-2.3,Cakephp,Model,Has Many Through,Cakephp 2.3,嗯。尽量简明扼要地阐述我的问题。我有一个庞大的项目,主要目的是从模型数据生成一个配置文件。在收集配置信息数据库时,我不知道如何以有效的方式获取适当的信息。模式:地区、部门、用户、运营商。 地区 有许多部门 | id | name | 部门 通过部门职位拥有许多用户 | id | name | district | id | tone | name | tone | length | 用户 有许多部门通过部门职位 属于承运人 | id | name | phone | email |

嗯。尽量简明扼要地阐述我的问题。我有一个庞大的项目,主要目的是从模型数据生成一个配置文件。在收集配置信息数据库时,我不知道如何以有效的方式获取适当的信息。模式:地区、部门、用户、运营商。

  • 地区
    • 有许多部门
    • | id | name |
  • 部门
    • 通过部门职位拥有许多用户
    • | id | name | district | id | tone | name | tone | length |
  • 用户
    • 有许多部门通过部门职位
    • 属于承运人
    • | id | name | phone | email | carrier | id | notify | method(tiny int)|
  • 运营商
    • 拥有许多用户
    • | id | name | mms | u网关|
  • 部门职位
    • 属于用户、部门
    • | id |用户| id |部门| id |角色|
将使用与上述所有模型关联的字段为每个地区生成配置文件。对于地区中的每个部门,将以下内容添加到字符串中,该字符串完成后将保存到配置文件中:

[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_电子邮件和amr_电子邮件的列表应该基于该部门的用户,基于
notify_方法
User字段生成。除了这些电子邮件字段之外,我还可以通过以下方式将所有内容都输入到配置文件中:

//districts controller
public function generate_config(){
    $districts = $this->District->find('All'); 
    $this->set(compact('districts'));
}  
<?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...
//                            )
//                        )
//                    )
//                ) 
//            )
//        )
//    )
// )

?>
现在,对于视图文件:

    <?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);
    ?>
我需要做的是为每个部门生成电子邮件列表,但我想不出一个合理的方法来做到这一点。我觉得如果
部门
用户
使用HABTM关系,而不是hasMany-through关系,会容易得多,我觉得我必须使用hasMany-through,因为我正在为该关系保存更多数据。有什么建议吗??请毫不犹豫地询问我是否应该更清楚地了解任何事情,但我已尝试提供所有相关信息,同时尽量减少铺天盖地的细节。谢谢你

编辑

多亏了@Drewdiddy611,我的心智才得以恢复。可控制的行为非常适合此问题。所以,希望有人能避免一个完整的项目大修——这是我刚刚要做的事情——并使用可控制的行为

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

我认为您使用基于表/数据的“hasMany-through”关系是正确的。在CakePHP2.1中,他们为HABTM关系添加了一个“唯一”键,您可以将其设置为“keepExisting”,以避免丢失数据

-查看主标题下方的“2.1中的变更”部分

也许可以试一试

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

//districts controller
public function generate_config(){
    $districts = $this->District->find('All'); 
    $this->set(compact('districts'));
}  
<?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...
//                            )
//                        )
//                    )
//                ) 
//            )
//        )
//    )
// )

?>


-Andrew

我认为您基于表/数据使用“hasMany-through”关系是正确的。在CakePHP2.1中,他们为HABTM关系添加了一个“唯一”键,您可以将其设置为“keepExisting”,以避免丢失数据

-查看主标题下方的“2.1中的变更”部分

也许可以试一试

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

//districts controller
public function generate_config(){
    $districts = $this->District->find('All'); 
    $this->set(compact('districts'));
}  
<?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...
//                            )
//                        )
//                    )
//                ) 
//            )
//        )
//    )
// )

?>


-安德鲁,你愿意嫁给我吗?我即将对我的设计进行重大修改——毫无疑问,这在时间/金钱上会非常昂贵。不仅对这个问题很有用,对很多其他问题也很有用。你愿意嫁给我吗?我即将对我的设计进行重大修改——毫无疑问,这在时间/金钱上会非常昂贵。不仅对这个问题很有用,而且对其他许多问题也很有用。