当前位置:Gxlcms > PHP教程 > 攻克CakePHP系列三表单数据增删改_PHP教程

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

时间:2021-07-01 10:21:17 帮助过:11人阅读

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

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

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

代码依次为:

database.php:与上例一致。

companies_controller.php:

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

company.php:

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

index.thtml:

  1. Test companies

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

view.thtml:

  1. Company: echo $company['Company']['company']?>

  2. Id: echo $company['Company']['id']?>

  3. Price: echo $company['Company']['price']?>

  4. Change: echo $company['Company']['change']?>

  5. LastUpdate: echo $company['Company']['lastupd']?>


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

add.thtml:

  1. Add Company

edit.thtml:

  1. Edit Company

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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/319619.htmlTechArticle这里声明一点,上例中不小心把数据库表中lastupd字段错打成lastudp,本例子予以更正。 除上诉字段数据库与上例一致。 工程仍沿用上例,如...

人气教程排行