Skip to content
Tomáš Tatarko edited this page Nov 9, 2013 · 2 revisions

Every programmer need only two things:

  1. New, innovative technologies.
  2. Simple manual how to use them.

You can find both here. MongoDB is modern and powerful NoSQL database that are document-oriented. MongoAR on the other hand is effective tool how to work with records stored there. Last, but no least is this Wiki that will help you to learn how to use both powerful tools.

Content

  • First steps - understand the basic of MongoAR in few seconds.
  • Query builder - learn how to select and fetch records from database.

First steps

To get started you have connect to MongoDB and send active connection to ActiveRecord class. It couldn't be simpler than...

$mongo = new MongoClient;
$mongoDB = $mongo->test;
MongoAR\ActiveRecord::setDatabase($mongoDB);

That's all. Since now MongoAR will be able to work properly. But you now, the main advantage of Active Record pattern is that all the tables have their own model - class. In MongoAR you are it's all about extending MongoAR\ActiveRecord class. Imagine that you have an array of movies meta data and want to store them in Movie table. The following code will do it in just a few lines.

/**
 * Instances of this class will represent movie documents
 */
class Movie extends MongoAR\ActiveRecord
{
}

Now fill table in database with some test data...

$movies =
[
	[
		'title' => 'RoboCop',
		'rating' => 3.5,
		'tags' => [
			'action',
			'crime',
			'sci-fi'
		]
	],
	[
		'title' => 'RoboCop',
		'rating' => 3.5,
		'tags' => [
			'action',
			'crime',
			'sci-fi'
		]
	],
];

foreach($movies as $movie)
{
	$model = new Moview($movie);
	$model->save();
}
Clone this wiki locally