top of page
Image by Tobias Fischer

Database Layer

Database layer

classes.png

The Db classes

If you like Propel, Doctrine or some other ORM, you can use it simply by putting it in the classes directory. However, Qbix comes with its own, very powerful, ORM, which was designed from the ground up to provide seamless support for your social app – across both PHP and in Node.js!

​

Here are just some of its features:

-Efficient: designed for speed and low memory use

-Expressive: lets you write what you're thinking

-Automatic: generates lots of code for you, from given data

-Secure: makes it difficult to mess up and allow SQL injection

-Flexible: supports complex relational joins on any number of fields

-Intelligent: understands transactions, autoincrementing ids, etc.

-Sharding: scale up as your user base grows (if you are able to avoid joins)

2

Easy to use

Q's database layer is developer-friendly and easy to get started with. All the plugins, such as Users and Streams, base their database model classes on it. Simply describe the database connections in local/app.json and they go to work out of the box.  Got your own database tables? Simply run the following command:

php MyApp/scripts/Q/models.php --all

and Qbix will automatically analyze your schema and generate your models, in both Node.js and PHP. You'll end up with something like this:

​

3

Designed for teams in mind

As you develop your app, you'll find yourself modifying and expanding your database schema. Every time you do this, you'll probably re-generate the models, increment your app's version and check it into version control. However, other members of your team will need to update not just the app code, but their database.  

 

Q's installer solves this problem for you. It automatically tracks versions of each app and plugin on each database connection. When you change your schema, simply save the SQL commands you used in a file, and Qbix will replay them when a team member pulls your changes, using their local settings of course. All they'll have to do is run:

php MyApp/scripts/Q/install.php --all

4

For developers

Here are some examples of how you could use your database models..

// Basic ORM usage $user = new Users_User(); $user->id = '3k4jc8'; if (!$user->retrieve()) { // SELECT user throw new Exception('User not found'); } $user->username = 'changed'; // does validation $user->save(); // UPDATE user $user->remove(); // DELETE user $user->save(); // INSERT new user // More powerful features & security $names = array('first_name', 'last_name'); $streams = Streams::select() ->where(array( 'publisher_id' => $user->id, 'names' => $names )) ->orderBy('username') ->fetchDbRows('name'); foreach ($streams as $name => $stream) { // Qbix can even write queries for you: $msgs = $stream->getRelated('messages'); // It's flexible when you need it: $msgs = $stream->getRelated('messages', true) ->limit(5) // modify the query ->orderBy('ordinal') // some more ->resume(); // now pull the trigger }

4

Limitations

The main limitation of Qbix's database library is that it currently only supports MySQL and compatible databases. Supporting another DBMS would be as simple as writing an adapter, but at the moment the MySQL adapter is the only one out there.

bottom of page