2013-01-16 40 views
1

背景CakePHP的 - 一种观点认为,多重嵌套形式,多表

他们给我写一个小企业的在线数据库的任务。这个数据库将包括大量的信息以及他们的董事和分支机构的信息。由于任何企业都可以拥有无​​限数量的董事和分支机构,因此我需要创建一个不仅限于一名董事和/或分支机构的数据库。

我有什么

目前,我有3个表。

smmes [id, company_name, trading_name, business_address, registration_number, tax_reference, vat_number, bbbee_status, employees, awards, created, modified] 
ownerships [id, smme_id, name, surname, gender, age, race, disability, qualification, created, modified] 
branches [id, smme_id, location, contact_number, contact_person, created, modified] 

注意:smme_id是分支机构或董事所属smmes公司的ID。

而且我对SMME有一个看法。

什么是我的问题

我很新的CakePHP的(其实,这是我的第一个应用程序我创建使用CakePHP)。我想知道如何制作一个表单,用户可以输入所有这些细节,然后从一个视图添加所有导演和分支的详细信息。我宁愿他们没有各种意见去创造所有的细节。除此之外,这个视图应该使用正确的smme_id将所有数据保存到正确的表中。

这是可能的,或者我应该离开cakePHP并手动编写它。

回答

1

您可以在您的控制器中按需加载模型,然后将模型特定数据(从发布表单接收)传递到加载模型的保存方法。

public function detail(){ 

    if($this->request->is('post')): // update only when form is posted 

     $this->loadModel('ownerships'); 
     $owner_name= $this->request->data['Ownername']; 
     $ownerships_data = array('Ownership' = > array(
          'name' = > $owner_name 
          //add other keys from posted form 
          ) 
         ); 
     $this->Ownership->saveAll($ownerships_data); 
    // load other models for saving posted data in related tables 
    endif; 
} 

同样载入其他模型并将数组中的字段作为数组传递给它的存储方法。 假设URL格式为http://example.com/director/detail。所以你想提出上述方法(称为在MVC术语行动)在app/controllers/directors_controller.php

一般来说,如果URL格式为http://somesite.com/abc/xyz它会寻找xyz行动 app/controllers/abcs_controller.php

你可以阅读更多关于蛋糕大会的信息here

+0

感谢您的回答。如上所述,我对cakePHP非常陌生。这会去哪个文件? – Albert

+0

您的控制器 – icebreaker