Let's create a public function called create
that will handle the creation of posts for our application.
1. Open
classes/post.php
, and scroll to the bottom. Here, we'll create a new public function calledcreate
.public function create() { }
2. Let's start by getting a new instance of Bones, and then setting the variables of the current
post
object.public function create() { $bones = new Bones(); $this->_id = $bones->couch->generateIDs(1)->body->uuids[0]; $this->date_created = date('r'); $this->user = User::current_user(); }
3. Finally, let's put the document to CouchDB, using Sag.
public function create() { $bones = new Bones(); $this->_id = $bones->couch->generateIDs(1)->body->uuids[0]; $this->date_created = date('r'); $this->user = User::current_user(); $bones->couch->put($this->_id, $this->to_json()); }
4. Let's wrap the call to CouchDB with a
try...catch
statement, and...