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

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

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

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

代码依次为:

database.php:与上例一致。

companies_controller.php:

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

company.php:

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

index.thtml:

  1. <h1>Test companies</h1>
  2. <table>
  3. <tr>
  4. <th>Id</th>
  5. <th>company</th>
  6. <th>price</th>
  7. <th>change</th>
  8. <th>last update</th>
  9. </tr>
  10. <?php foreach ($companies as $company): ?>
  11. <tr>
  12. <td><?php echo $company['Company']['id']; ?></td>
  13. <td>
  14. <?php echo $html->link($company['Company']['company'], "/companies/view/".$company['Company']['id']); ?>
  15.     
  16. <?php echo $html->link('Delete', "/companies/delete/{$company['Company']['id']}", null, 'Are you sure?')?>
  17. </td>
  18. <td><?php echo $company['Company']['price']; ?></td>
  19. <td><?php echo $company['Company']['change']; ?></td>
  20. <td><?php echo $company['Company']['lastupd']; ?></td>
  21. </tr>
  22. <?php endforeach; ?>  
  23. </table>
  24. <p>
  25. <?php echo $html->link('add', "/companies/add"); ?>
  26. </p>

view.thtml:

  1. <h1>Company: <?php echo $company['Company']['company']?></h1>
  2. <p><small>Id: <?php echo $company['Company']['id']?></small></p>
  3. <p>Price: <?php echo $company['Company']['price']?></p>
  4. <p>Change: <?php echo $company['Company']['change']?></p>
  5. <p>LastUpdate: <?php echo $company['Company']['lastupd']?></p>
  6. <br/>
  7. <p>
  8. <?php echo $html->link('edit', "/companies/edit/".$company['Company']['id']); ?>
  9. </p>

add.thtml:

  1. <h1>Add Company</h1>
  2. <form method="post" action="<?php echo $html->url('/companies/add')?>">
  3. <p>
  4. Company:
  5. <?php echo $html->input('Company/company', array('size' => '40'))?>
  6. <?php echo $html->tagErrorMsg('Company/company', 'Company is required.') ?>
  7. </p>
  8. <p>
  9. Price:
  10. <?php echo $html->input('Company/price', array('size' => '40'))?>
  11. <?php echo $html->tagErrorMsg('Company/company', 'Price is required.') ?>
  12. </p>
  13. <p>
  14. Change:
  15. <?php echo $html->input('Company/change', array('size' => '40'))?>
  16. <?php echo $html->tagErrorMsg('Company/change', 'Change is required.') ?>
  17. </p>
  18. <p>
  19. Last Update:
  20. <?php echo $html->input('Company/lastupd', array('size' => '40'))?>
  21. <?php echo $html->tagErrorMsg('Company/lastupd', 'Last Update is required.') ?>
  22. </p>
  23. <p>
  24. <?php echo $html->submit('Save') ?> <?php echo $html->link('return', "/companies/index"); ?>
  25. </p>
  26. </form>

edit.thtml:

  1. <h1>Edit Company</h1>
  2. <form method="post" action="<?php echo $html->url('/companies/edit')?>">
  3. <?php echo $html->hidden('Company/id'); ?>
  4. <p>
  5. Company:
  6. <?php echo $html->input('Company/company', array('size' => '40'))?>
  7. <?php echo $html->tagErrorMsg('Company/company', 'Company is required.') ?>
  8. </p>
  9. <p>
  10. Price:
  11. <?php echo $html->input('Company/price', array('size' => '40'))?>
  12. <?php echo $html->tagErrorMsg('Company/company', 'Price is required.') ?>
  13. </p>
  14. <p>
  15. Change:
  16. <?php echo $html->input('Company/change', array('size' => '40'))?>
  17. <?php echo $html->tagErrorMsg('Company/change', 'Change is required.') ?>
  18. </p>
  19. <p>
  20. Last Update:
  21. <?php echo $html->input('Company/lastupd', array('size' => '40'))?>
  22. <?php echo $html->tagErrorMsg('Company/lastupd', 'Last Update is required.') ?>
  23. </p>
  24. <p>
  25. <?php echo $html->submit('Save') ?> <?php echo $html->link('return', "/companies/index"); ?>
  26. </p>
  27. </form>

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

广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!