Beginning cloud development with cloudControl – Part 3 – MongoDB

Here we are at part three of the beginning cloud development with cloudControl series and in this part, we’re adding mongoDB support. In part one of the series, we laid the foundation for the application and got up to speed with what cloudControl is, why it works and how to get started using it.

Then in part two, we started to flesh out the application that we started building in part one and added MySQL support to it. We showed how to work with cloudControl to manage the key requirements, such as enabling MySQL support, connecting to the MySQL database and keeping the database up to date from a maintenance perspective (creating the database schema and loading data in to the database).

In this, the third part of the series, we’re replacing MySQL that we introduced in part two with mongoDB support.


Here we are at part three of the beginning cloud development with cloudControl series and in this part, we’re adding mongoDB support. In part one of the series, we laid the foundation for the application and got up to speed with what cloudControl is, why it works and how to get started using it.

Then in part two, we started to flesh out the application that we started building in part one and added MySQL support to it. We showed how to work with cloudControl to manage the key requirements, such as enabling MySQL support, connecting to the MySQL database and keeping the database up to date from a maintenance perspective (creating the database schema and loading data in to the database).

In this, the third part of the series, we’re replacing MySQL that we introduced in part two with mongoDB support.

If you’re not familiar with mongoDB, then I strongly encourage you to have a read of the

Getting Started with MongoDB article that I wrote for nettuts+ a little while back. It will show you how to get started, installing and learning the basics of this great NoSQL technology.

To make this process as simple as possible, we’re going to be using the superb Shanty Mongo library - designed for use with Zend Framework, but it works equally well standalone. As the net result is not a lot different from where we ended up at the end of part two of the series, this will be a pretty succinct and concise post. So without any further ado, let’s get started

Step 1 - Download Shanty-Mongo Zend Framework library for mongoDB

Ok, grab a copy of the shanty-Mongo library from the github repo, alternatively, clone a copy of the repo. When you have a copy ready, place the Shanty directory, in the library directory of the project in to the library directory of our project.

After this, we need to register the namespace for it with our existing Zend Autoloader. Normally this would be done in the application.ini config file, but as we’re not using the MVC approach to Zend, then we need to do this manually. So at the top of the config.php file, register the namespace. You file should similar to the output below:

[php] require_once(&‘Zend/Loader/Autoloader.php’); $autoloader = Zend_Loader_Autoloader::getInstance(); $autoloader->registerNamespace(&‘Shanty_’); // database configuration $config = new Zend_Config_Ini( APPLICATION_PATH . &’/config/application.ini’, APPLICATION_ENV ); [/php]

With that, we’re ready to start using the library.

Step 2 - Enable mongoDB support in the project

From your terminal, change to the project directory. In there, we need to enable mongoDB support. To make this simpler, I’ve created a new branch, mongodb, so please don’t get confused by the output below in the screenshots. To enable mongoDB support, we add the cloudControl add-on as follows:

[bash] cctrlapp maltbluedev/mongodb addon.add mongodb.free [/bash]

Nothing hard about this, right? But as well as enabling the add on, we need the configuration options so that we can configure our application appropriately. To do that, run the command below:

[bash] cctrlapp maltbluedev/mongodb addon mongodb.free [/bash]

That will give us output similar to that below:

Add mongodb support to cloudControl project

You can see that it’s listed the settings for:

  • password
  • hostname
  • username
  • database
  • dbsize

Step 3 – Update the Configuration file

In the configuration file, application.ini, we now need to add support for mongoDB, just like we added support for MySQL previously. So, in your favourite text editor, open it up and add the following configuration settings:

[bash] [mongodb: production] mongodb.password = pEvtfVTdRgSs9JQ mongodb.host = 127.0.0.1 mongodb.username = depd2nkk5wk mongodb.database = depd2nkk5wk [/bash]

You can see that we’ve created an environment that matches the branch we’re on, mongodb and added the settings that we retrieved from the add on previously. With that, we’re ready to start converting our code over to mongoDB, from MySQL.

Step 4 - Convert the classes over to mongoDB

Authentication

In a lot of instances where I’ve used the Shanty-Mongo library, there’s not been authentication in place, however as we’ve seen and as is appropriate, the configuration of mongoDB supplied by cloudControl requires authentication to continue. So near the top of our config.php file, where we registered the Shanty namespace, we need to add a secured connection to our mongoDB database.

To do that, we need simply call the static addConnections class, passing in our configuration object. Have a look at the code below which does just this:

[php] Shanty_Mongo::addConnections($config->mongodb); [/php]

When this is done, all connections from here on in will attempt to use the credentials we’ve specified.

The Model Class

Shanty-Mongo manages interaction with mongoDB in a rather ORM fashion. For every database, we have a class. In that class, we specify the document and collection information. For a solid introduction to using Shanty-Mongo, I encourage you to read the excellent markdown readme that comes with the library. This will give you all you need to get up to speed with the library very quickly.

Gladly, our model needs are very simple. Have a look at the class below which takes care of everything for us:

[php] class Staff extends Shanty_Mongo_Document { protected static $_db = &‘depd2nkk5wk’; protected static $_collection = &‘StaffList’;

protected static $_requirements = array( &‘firstName’ => &‘Required’, &’lastName’, ’emailAddress’ => array(&‘Required’, &‘Validator:EmailAddress’), &‘occupation’, ); } [/php]

You can see that we’ve named it after the database, Staff. We’ve specified the database from the information we retrieved from the add on configuration. We’ve specified a simple collection name of StaffList and then setup the document requirements, which closely match the database table schema we were previously using.

You can see that we’ve specified that the firstName and emailAddress fields are required, but that the lastName and occupation fields aren’t. We’ve also specified that the emailAddress fields also needs to validate against the EmailAddress validator. This seems only right so that junk information is not entered and we can maintain a sense of proper data integrity.

As a quick note, we didn’t need to specifically mention the lastName and occupation field here. But for the sake of complete transparency, it seemed appropriate. MongoDB won’t mind and will add the fields as needed.

The Add Form

Nothing changes with the generation of the form, so we can leave that alone. What does change is the process of saving the information after a successful validation. So remove the old code that references the database and replace it with the following:

[php] $staffMember = new Staff(); $staffMember->firstName = $form->getValue(&‘firstName’); $staffMember->lastName = $form->getValue(&’lastName’); $staffMember->emailAddress = $form->getValue(’emailAddress’); $staffMember->occupation = $form->getValue(&‘occupation’); $status = $staffMember->save(); [/php]

What we’ve done is to create a new Staff object and set the properties of it with the information supplied in the successfully validated form. After that, we just call the save method and the information is persisted to the database. Simplicity is bliss.

The Edit Form

As with the add form, nothing changes with the generation of the form, so leave that be. However, we change the code that runs after a successful validation and the code that retrieves the information about the staff member when the page is loaded.

So remove the old $form->setDefaults code and replace it with the code below:

[php] $staffMember = Staff::find($_GET[&‘id’]); $form = getManageForm(); $form->setDefaults(array( &‘firstName’ => $staffMember->firstName, &’lastName’ => $staffMember->lastName, ’emailAddress’ => $staffMember->emailAddress, &‘occupation’ => $staffMember->occupation, &‘id’ => $_GET[&‘id’] )) ->setAction(&’/edit.php?id=’.$_GET[&‘id’]); [/php]

This attempts to instantiate an object based on the id value retrieved from the GET super global array. This id value won’t be a simple integer as with MySQL auto-increment values. Instead it will be a MongoID value. You can read more about those in the PHP mongoDB documentation.

Ok, now we need to replace the code that updates the information in our mongodb database after a successful form validation. Replace the database code from part two with the code below:

[php] $staffMember = Staff::find($_GET[&‘id’]); $staffMember->firstName = $form->getValue(&‘firstName’); $staffMember->lastName = $form->getValue(&’lastName’); $staffMember->emailAddress = $form->getValue(’emailAddress’); $staffMember->occupation = $form->getValue(&‘occupation’); $status = $staffMember->save(); [/php]

This code will attempt to instantiate an object, based on the id value and then update the properties of it and call the save method to persist the information. How simple can it be.

The Delete Form

The delete form only requires a minor change. As with the add and edit forms, replace the code after successful form validation with the code below:

[php] $staffMember = Staff::find($_GET[&‘id’]); $staffMember->delete(); [/php]

As with the edit form, this instantiates an document with the id value from the GET super global then calls the delete method on it to remove the document from the database.

The List Form

At the top of the form, call the following method, which will retrieve a collection of all the documents in the staff database:

[php] $staffList = Staff::all(); [/php]

After that we can iterate over the collection as we did previously and display the members in our staff database.

Step 5 - Disable MySQL support

Ok, we’re almost done, just a bit more to go. Now, as we no longer need MySQL support, we need to remove it. So run the following command:

[bash] cctrlapp maltbluedev/mongodb addon.remove mysql.free [/bash]

Step 6 - Deploying the Changes

As with all code, we need to deploy the changes. So commit the changes to the branch and then run the push and deploy changes with the cctrlapp command line utility:

[bash] cctrlapp maltbluedev/mongodb push cctrlapp maltbluedev/mongodb deploy [/bash]

After about a minute, all the changes will be deployed and ready to go. When that’s done, we can now view our updated application at: http://mongodb.maltbluedev.cloudcontrolled.com/list.php

Winding Up

So, you can see just how easy it is to integrate mongodb into your application with cloudControl. It’s even easier than with MySQL. We simply need to enable the addon, migrate our code and deploy our changes to the project. Perfectly simple and practical, requiring next to no knowledge of mongoDB to get up and running quickly. It’s yet another win for cloudControl.


You might also be interested in these tutorials too...

Mon, Jan 9, 2012

Beginning cloud development with cloudControl – Part 4 – Memcache

Here we are at part four of the Beginning Cloud Development with cloudControl series and in this part, we’re adding Memcached support. In part one of the series, we laid the foundation for the application and got up to speed with what cloudControl is, why it works and how to get started using it.

Then in part two, we started to flesh out the application that we started building in part one and added MySQL support to it. We showed how to work with cloudControl to manage the key requirements, such as enabling MySQL support, connecting to the MySQL database and keeping the database up to date from a maintenance perspective (creating the database schema and loading data in to the database).

Then in the third part of the series, we replaced MySQL with mongoDB support. Now, in this, the third part of the series, we’re going to finish up with adding Memcached support. As the core of the work’s already been done in the application, this tutorial will be shorter and simpler than the previous three. So, get out the code that you worked through from part 2, or download a copy of it from the github repository and let’s get started.

Fri, Dec 16, 2011

Beginning cloud development with cloudControl - Part 2 - MySQL

In part one of the series we got a birds eye view of a great cloud development solution for PHP - cloudControl. We looked at the concept of what it is, what you can do with it and ran through a basic deployment with a rather basic application. If you missed the first part, I strongly encourage you to read it before continuing on with part two. When you’re done, come on back and work through it here.

In this, part two of the series, things start to get more serious. In this part, we’re going to start to flesh out the application started in part one, adding in MySQL support - showing just how simple cloudControl makes this for us.

Thu, Dec 8, 2011

Beginning cloud development with cloudControl

##.

Don’t forget to grab the other parts as well:


So you’ve heard all about the cloud. In the current day and age, you’d have to be living under a rock to have not encountered it by now. Whether it’s the ads from Amazon, Rackspace or Microsoft, the extensions to your favourite framework, such as SimpleCloud in Zend Framework or iCloud from Apple – The Cloud is Everywhere!

But these aren’t the only options available to us as developers. In this series I want to introduce you to and help you get started with another cloud service - cloudControl.

Tue, May 22, 2012

Zend Form Mastery with Zend Config – Part 3 Standard Form & Element Options

– Part 3 Standard Form & Element Options This is a Multi Part Series. Check out the other parts: Part 1 - Custom Filter Paths Part 2 - Core Form Configuration Part 4 - Configuring Zend Validators Options We’ve looked at custom form filters and we’ve looked at the core form configuration. But what about the other form properties? What about: setting an element as readonly an element as required ignoring an element and love them or hate them, what about decorators Element Options Outside of the options that we’ve looked at previously, there are a number of other options that we can consider implementing when configuring a Zend Form.


Want more tutorials like this?

If so, enter your email address in the field below and click subscribe.

You can unsubscribe at any time by clicking the link in the footer of the emails you'll receive. Here's my privacy policy, if you'd like to know more. I use Mailchimp to send emails. You can learn more about their privacy practices here.

Join the discussion

comments powered by Disqus