这里声明一点,上例中不小心把数据库表中lastupd字段错打成lastudp,本例子予以更正。
除上诉字段数据库与上例一致。
工程仍沿用上例,如下图:
代码依次为:
database.php:与上例一致。
companies_controller.php:
set('companies', $this->company->findall()); } function view($id = null) { $this->company->id = $id; $this->set('company', $this->company->read()); } function add() { if (!emptyempty($this->data)) { if ($this->company->save($this->data)) { $this->flash('your post has been saved.','/companies'); } } } function edit($id = null) { if (emptyempty($this->data)) { $this->company->id = $id; $this->data = $this->company->read(); } else { if ($this->company->save($this->data['company'])) { $this->flash('your post has been updated.','/companies'); } } } function delete($id) { $this->company->del($id); $this->flash('the post with id: '.$id.' has been deleted.', '/companies'); } } ?>
company.php:
valid_not_empty, 'price' => valid_not_empty, 'change' => valid_not_empty, 'lastupd' => valid_not_empty ); } ?>
index.thtml:
test companies id company price change last update
link($company['company']['company'], /companies/view/.$company['company']['id']); ?> link('delete', /companies/delete/{$company['company']['id']}, null, 'are you sure?')?>
link('add', /companies/add); ?>
view.thtml:
company: id:
price:
change:
lastupdate:
link('edit', /companies/edit/.$company['company']['id']); ?>
add.thtml:
add company post action=url('/companies/add')?>>
company: input('company/company', array('size' => '40'))?> tagerrormsg('company/company', 'company is required.') ?>
price: input('company/price', array('size' => '40'))?> tagerrormsg('company/company', 'price is required.') ?>
change: input('company/change', array('size' => '40'))?> tagerrormsg('company/change', 'change is required.') ?>
last update: input('company/lastupd', array('size' => '40'))?> tagerrormsg('company/lastupd', 'last update is required.') ?>
submit('save') ?> link('return', /companies/index); ?>
edit.thtml:
edit company post action=url('/companies/edit')?>> hidden('company/id'); ?>
company: input('company/company', array('size' => '40'))?> tagerrormsg('company/company', 'company is required.') ?>
price: input('company/price', array('size' => '40'))?> tagerrormsg('company/company', 'price is required.') ?>
change: input('company/change', array('size' => '40'))?> tagerrormsg('company/change', 'change is required.') ?>
last update: input('company/lastupd', array('size' => '40'))?> tagerrormsg('company/lastupd', 'last update is required.') ?>
submit('save') ?> link('return', /companies/index); ?>
如此访问http://localhost/cakephp/companies即可测试代码。
http://www.bkjia.com/phpjc/319619.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/319619.htmltecharticle这里声明一点,上例中不小心把数据库表中lastupd字段错打成lastudp,本例子予以更正。 除上诉字段数据库与上例一致。 工程仍沿用上例,如...