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

5年以前  |  阅读数:1095 次  |  编程语言:PHP 

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

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

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

代码依次为:

database.php:与上例一致。

companies_controller.php:

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

company.php:

  1. <?php
  2. class Company extends AppModel
  3. {
  4. var $name = 'Company';
  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. <?php foreach ($companies as $company): ?>
  3. <?php endforeach; ?>
  4. Id company price change last update
  5. <?php echo $html->link($company['Company']['company'], "/companies/view/".$company['Company']['id']); ?>
  6. <?php echo $html->link('Delete', "/companies/delete/{$company['Company']['id']}", null, 'Are you sure?')?>
  7. <?php echo $html->link('add', "/companies/add"); ?>

view.thtml:

  1. Company:

  2. Id:

  3. Price:

  4. Change:

  5. LastUpdate:


  6. <?php echo $html->link('edit', "/companies/edit/".$company['Company']['id']); ?>

add.thtml:

  1. Add Company

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

edit.thtml:

  1. Edit Company

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

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

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例