Here is a quick example on how to use RapidDataMapper with CodeIgniter:
What I'm going to do is the following:
An article viewer on which people can comment.
Currently there is no admin for it where you can remove comments and create/update/delete pages, that I will do tomorrow.
InstallationFollow the installation instructions provided
in the manual, the CI specific installation instructionsThe database tablesCode:
CREATE TABLE `articles` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) DEFAULT NULL,
`slug` varchar(255) DEFAULT NULL,
`content` text,
PRIMARY KEY (`id`)
);
CREATE TABLE `comments` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`author` varchar(255) DEFAULT NULL,
`content` text,
`article_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `articles` (`id`,`title`,`slug`,`content`)
VALUES
(1,'Index','index','Content');
The data objectsCreate the file application/data_model/article.php and enter this:
Code:
<?php
class Article
{
public $id;
public $title;
public $slug;
public $content;
public $comments = array();
}
Create the file application/data_model/comment.php and add this:
Code:
<?php
class Comment
{
public $id;
public $author;
public $content;
public $article_id;
public $article = null;
}
The descriptors, describing how they map to the databaseCreate application/data_model/articledescriptor.php:
Code:
<?php
class ArticleDescriptor extends Db_Descriptor
{
function __construct()
{
$this->setClass('Article');
$this->add($this->newPrimaryKey('id'));
$this->add($this->newColumn('title'));
$this->add($this->newColumn('slug'));
$this->add($this->newColumn('content'));
$this->add($this->newRelation('comments'));
// Use the Sluggable behaviour to automate slug generation
$this->applyPlugin(new Db_Plugin_Sluggable(array('title' => 'slug')));
}
}
Create the application/data_model/commentdescriptor.php:
Code:
<?php
class CommentDescriptor extends Db_Descriptor
{
function __construct()
{
$this->setClass('Comment');
$this->add($this->newPrimaryKey('id'));
$this->add($this->newColumn('author'));
$this->add($this->newColumn('content'));
$this->add($this->newColumn('article_id'));
$this->add($this->newRelation('article'));
}
}
ControllerBecause this is a very small application, I won't create a model for it, instead I add all the database interaction in the controller:
Code:
<?php
class Page extends Controller
{
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->helper('form');
}
function _remap($page_title = 'index')
{
$p = Db::find('article')->related('comments')->where('slug', $page_title)->getOne();
if( ! $p)
{
show_404();
}
// Because this is an example, I'm going to use $_POST directly
if( ! empty($_POST['content']) && ! empty($_POST['author']))
{
$c = new Comment();
$c->content = $_POST['content'];
$c->author = $_POST['author'];
// Add the comment
$p->comments[] = $c;
Db::save($p);
}
$this->load->view('article', array('page' => $p));
}
}
View:And the matching view:
Code:
<h1><?php echo $page->title; ?></h1>
<p>
<?php echo $page->content ?>
</p>
<?php foreach($page->comments as $comment): ?>
<p>
<strong><?php echo $comment->author ?></strong>: <?php echo $comment->content ?>
</p>
<?php endforeach; ?>
<?php echo form_open('/page/'.$page->slug); ?>
<?php echo form_input('author'); ?>
<?php echo form_input('content'); ?>
<?php echo form_submit('', 'Add') ?>
<?php echo form_close(); ?>
Usage:Point your browser to /page/index and start commenting!

Hope this clears some of the confusion around the complexity of RapidDataMapper.
I know RapidDataMapper can be complex, but the complexity comes when you want to do complex things
