2013-05-17 42 views
0
Chart hasMany Series 
Series belongsTo Chart 
Series hasAndBelongsToMany Industry 
Series hasAndBelongsToMany Sector 
Industry hasMany Sector 
Sector belongsTo Industry 

我试图让用户保存图表以及多个系列。cakephp字段命名约定 - 保存多个模型

如何命名输入字段以及调用哪些保存方法以保存所有模型?

回答

0

通常惯例,一次保存多个相关模型值是如下:

$data = array(
    'User' => array('userFieldName' => 'fieldValue'), 
     'Chart' => array(
      array(
       'chartFieldName' => "chartFieldValue", 
       'Series' => array(
        array(
         'firstSerieItemField' => "Value", 
        ), 
        array(
         'secondSerieItemField' => "Value", 
        ) 
        ... 
        ...//More Series Data 
       ) 
      ) 
     ) 
    ); 

$this->User->saveAll($data); 

注:不要忘记分配

模型之间的关系,但你可以在查看页面中正确定义字段值来实现它。请记住遵循以下模式:

topMdel["topModelFieldName"]; 
topModel["FirstAssociateModel"]["FirstAssociateModelFieldName"]; 
topModel["FirstAssociateModel"]["SecondAssociateModel"]["SecondAssociateModelFieldName"]; 

要更清楚。你的情况:

有关的usermodel数据字段:

User["user_name"]; 
User["user_email"]; //etc. 

有关图表模型数据字段:

User["Chart"]["chart_name"]; 
User["Chart"]["chart_value"]; //etc. 

希望帮助。 适用系列型号数据区:

User["Chart"]["Series"][0]["series_1_name"]; 
User["Chart"]["Series"][1]["series_2_name"]; 
User["Chart"]["Series"][2]["series_2_name"];