首页 >后端开发 >php教程 >攻克CakePHP系列三 表单数据增删改_PHP

攻克CakePHP系列三 表单数据增删改_PHP

WBOY
WBOY原创
2016-06-01 12:24:09841浏览

这里声明一点,上例中不小心把数据库表中lastupd字段错打成lastudp,本例子予以更正。

除上诉字段数据库与上例一致。

工程仍沿用上例,如下图:

代码依次为:

database.php:与上例一致。

companies_controller.php:

  1. class CompaniesController extends AppController
  2. {
  3.     var $name = 'Companies';
  4.     
  5.     function index()
  6.     {
  7.         $this->set('companies'$this->Company->findAll());
  8.     }
  9.     
  10.     function view($id = null)
  11.     {
  12.         $this->Company->id = $id;
  13.         $this->set('company'$this->Company->read());
  14.     }
  15.     
  16.     function add()
  17.     {
  18.         if (!emptyempty($this->data))
  19.         {
  20.             if ($this->Company->save($this->data))
  21.             {
  22.                 $this->flash('Your post has been saved.','/companies');
  23.             }
  24.         }
  25.     }
  26.     
  27.     function edit($id = null)
  28.     {
  29.         if (emptyempty($this->data))
  30.         {
  31.             $this->Company->id = $id;
  32.             $this->data = $this->Company->read();
  33.         }
  34.         else
  35.         {
  36.             if ($this->Company->save($this->data['Company']))
  37.             {
  38.                 $this->flash('Your post has been updated.','/companies');
  39.             }
  40.         }
  41.     }
  42.     
  43.     function delete($id)
  44.     {
  45.         $this->Company->del($id);
  46.         $this->flash('The post with id: '.$id.' has been deleted.''/companies');
  47.     }
  48. }
  49. ?>

company.php:

  1. class Company extends AppModel
  2. {
  3.     var $name = 'Company';
  4.     
  5.     var $validate = array(
  6.         'company' => VALID_NOT_EMPTY,
  7.         'price'   => VALID_NOT_EMPTY,
  8.         'change'  => VALID_NOT_EMPTY,
  9.         'lastupd' => VALID_NOT_EMPTY
  10.     );
  11. }
  12. ?>

index.thtml:

  1. Test companies

  2. foreach ($companies as $company): ?>
  3. endforeach; ?>  
  4. Id company price change last update
    $company['Company']['id']; ?>
  5. $html->link($company['Company']['company'], "/companies/view/".$company['Company']['id']); ?>
  6.     
  7. $html->link('Delete'"/companies/delete/{$company['Company']['id']}", null, 'Are you sure?')?>
  8. $company['Company']['price']; ?> $company['Company']['change']; ?> $company['Company']['lastupd']; ?>
  9. $html->link('add'"/companies/add"); ?>

view.thtml:

  1. Company: $company

['Company']['company']?>
  • Id: $company['Company']['id']?>

  • Price: $company['Company']['price']?>

  • Change: $company['Company']['change']?>

  • LastUpdate: $company['Company']['lastupd']?>


  • $html->link('edit'"/companies/edit/".$company['Company']['id']); ?>
  • add.thtml:

    1. Add Company

    2. "post" action="url('/companies/add')?>">
    3. Company:
    4. $html->input('Company/company'array('size' => '40'))?>
    5. $html->tagErrorMsg('Company/company''Company is required.') ?>
    6. Price:
    7. $html->input('Company/price'array('size' => '40'))?>
    8. $html->tagErrorMsg('Company/company''Price is required.') ?>
    9. Change:
    10. $html->input('Company/change'array('size' => '40'))?>
    11. $html->tagErrorMsg('Company/change''Change is required.') ?>
    12. Last Update:
    13. $html->input('Company/lastupd'array('size' => '40'))?>
    14. $html->tagErrorMsg('Company/lastupd''Last Update is required.') ?>
    15. $html->submit('Save') ?> $html->link('return'"/companies/index"); ?>

    edit.thtml:

    1. Edit Company

    2. "post" action="url('/companies/edit')?>">
    3. $html->hidden('Company/id'); ?>
    4. Company:
    5. $html->input('Company/company'array('size' => '40'))?>
    6. $html->tagErrorMsg('Company/company''Company is required.') ?>
    7. Price:
    8. $html->input('Company/price'array('size' => '40'))?>
    9. $html->tagErrorMsg('Company/company''Price is required.') ?>
    10. Change:
    11. $html->input('Company/change'array('size' => '40'))?>
    12. $html->tagErrorMsg('Company/change''Change is required.') ?>
    13. Last Update:
    14. $html->input('Company/lastupd'array('size' => '40'))?>
    15. $html->tagErrorMsg('Company/lastupd''Last Update is required.') ?>
    16. $html->submit('Save') ?> $html->link('return'"/companies/index"); ?>

    如此访问http://localhost/cakephp/companies即可测试代码。

  • 声明:
    本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn