Populating a CakePHP Form with MySQL Default Values.
Sometimes it can be handy to fill a form with defaults.
Tagged with Web Development and CakePHP
Posted on 12/9/09 by Paul Herron
Here's a quick tip for populating a form with the MySQL defaults. Take the following schema, for example, of editions of an email newsletter:
DROP TABLE IF EXISTS editions; CREATE TABLE editions ( id int(8) NOT NULL AUTO_INCREMENT, title varchar(255) DEFAULT 'Coming up this week...', created datetime DEFAULT NULL, modified datetime DEFAULT NULL, PRIMARY KEY (id) );
In this case, I'm taking the Edition.title field as one the user might want to change, but would most likely just use a default value for. Populating the form field with that default is simple using Model::create():
<?php class EditionsController extends AppController { function add() { if(empty($this->data)) { $this->data = $this->Edition->create(); } else { if($this->Edition->save($this->data)) { $this->Session->setFlash('Edition saved.'); } else { $this->Session->setFlash('Sorry. That edition could not be saved.'); } } } } ?>Leave a Comment
Article Tags
Show all articles, or just those tagged as:
- Apache (1)
- CakePHP (10)
- Domains (1)
- Freeware (1)
- Graphics (1)
- Life (1)
- Open Source (2)
- Servage (1)
- SMS (1)
- Software (3)
- Ubuntu (2)
- WAMP (2)
- Web Development (13)
- Windows (2)
- Work (2)
Feed
The articles RSS feed is available.
Elsewhom
-
kottke.org.
Jason Kottke's weblog, home of fine hypertext products -
mattheaton.com.
Bluehost Blog
Matt is CEO of Bluehost, a successful web hosting provider -
Coding My Thoughts.
Java, PHP, and some other technological mumble jumble. Also, some real-life stuff as well. -
Hackszine.com.
Hack the way you think -
Ubuntu Blog.
Not quite everything about Ubuntu, but close
