The Nibble PHP Framework uses RedBeanPHP ORM. RedBeanPHP’s unique method of on the fly table modifications through Beans lends itself perfectly to the plug-in structure of the Nibble PHP Framework. RedBeanPHP allows a row from a table to be called and instantiated as an object. If the developer then adds a variable (which represent the table columns) to the object that does not yet exist, RedBeanPHP adds the variable as a new column with a type based on the value assigned to the variable.
RedBeanPHP has a very comprehensive set of Documentation that explains how to use the classes. Nibble PHP Framework does not add any functionality to RedBeanPHP so all of the methods described in the documentation can be used with no modification to the way they are called. An example, from the blog Bite actions class, of using RedBeanPHP in the Nibble PHP Framework:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
$user = \R::load('user', isset($this->u->current->id) ? $this->u->current->id : 2); $blog = \R::dispense("blog"); $blog->title = $_POST['title']; $blog->content = $_POST['content']; $blog->status = 0; if (isset($this->u->current->id)) { foreach ($this->u->current->permissions as $permisson) if ($this->settings["automatically_publish_{$permisson['name']}_posts"] == 'Y') $blog->status = 1; } else { if ($this->settings["automatically_publish_guest_posts"] == 'Y') $blog->status = 1; } $blog->created_at = time(); $blog->updated_at = $blog->created_at; \R::link($blog, $user); \R::store($blog); |
In the example above, a user Bean is loaded, a blog Bean is then dispensed and populated, and, finally the blog Bean is linked to the user Bean (the blog gets the user id added to it as a foreign key) and saved. As seen above it is a very simple process to add rows to tables and update or add columns.
Database schema file, schema.php
Each Bite requires a schema file named schema.php. This file sits in the Bite directory just like the config.php file. Schema.php is run the first time the Bite is activated and should build any tables that the Bite requires. The simplest way to do this is dispensing a new Bean for each table, populating and linking the beans as necessary and then saving them. Here is an example of a schema file from the demo Bite blog:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php $user = R::load('user', 1); $blog = R::dispense("blog"); $blog->title = 'Blog title'; $blog->content = 'Blog content/text, this is in the form of wiki syntax to be cleared at the render stage.'; $blog->status = 1; $blog->created_at = time(); $blog->updated_at = $blog->created_at; $comment = R::dispense("comment"); $comment->content = 'Test comment associated'; $comment->created_at = time(); $comment->updated_at = $comment->created_at; $reply = R::dispense('reply'); $reply->created_at = time(); $reply->content = 'Linking a reply!'; R::link($comment, $blog); R::link($comment, $user); R::link($reply, $comment); R::link($reply, $user); R::link($blog, $user); R::store($comment); R::store($reply); R::store($blog); |
As the example shows, the entire table structure for the blog Bite is set up using 4 Beans in a simple and easy to understand manner and without huge SQL statements.