Skip to content

Latest commit

 

History

History
132 lines (90 loc) · 3.37 KB

README.md

File metadata and controls

132 lines (90 loc) · 3.37 KB

PDOMongo

PDOMongo or PDO Mongo. PDO (PHP Data Object) for MongoDB.

  • Help me improve this small piece of software! Every help will be great and welcome.

Why should I use it?

PDOMongo is a small and straightforward piece of software. It encapsulates the Mongo connection, collection and database (CCD) and gives you a CRUD for your application. PDOMongo is also usefull because you can get any of the CCD at any time you want, to perform special actions that PDOMongo by itself doesn't supports.

How it works

PDOMongo is simple and straightforward as you can see below:

<?php
	use PDOMongo;
	/*
	 * If you aren't using namespaces:
	 * require_once(__DIR__.'\PDOMongo.php');
	 */

	// Create a PDOMongo with default values
	$pdomongo = new PDOMongo(); 
	
	// Set the database
	$pdomongo->setDatabase('test');
	
	// Set the collection
	$pdomongo->setCollection('things');
?>

Another example is shown below. To understand how to use other options, read the PDOMongo class construction method.

<?php
	use PDOMongo;
	/*
	 * If you aren't using namespaces, use:
	 * require_once(__DIR__.'\PDOMongo.php');
	 */

	$host = 'www.myDatabase.myHost.com';
	$database = 'mongoDatabase';
	
	// Create PDOMongo with custom values;
	$pdomongo = new PDOMongo($host,$database);
	
	// Set Collection
	$pdomongo->setCollection('things');
?>

Getters and Setters

<?php
	// PDOMongo created with defaults values;
	$pdo = PDOMongo();
	
	//Setters
	$pdo->setDatabase('myDatabase');
	$pdo->setCollection('myCollection');
	
	$returnConnection = $pdo->getConnection();
	$returnDatabase   = $pdo->getDatabase();
	$returnCollection = $pdo->getCollection();
?>

Manipulating Records

<?php
	// PDOMongo created with custom values;
	$pdo = PDOMongo("https://myMongoDB.myHost.com","myDatabase","Admin","Password");
	$pdo->setCollection("myCollection");
	
	$element = array(
			'name' => 'PDOMongo',
			'detail' => 'Its a PDO to manipulate MongoDB for lazy people',
			'author' => 'pablohenrique'
		);
	
	$pdo->insert($element);
	
	$newElement = $element;
	$newElement['author'] = 'PabloHenrique';
	
	
	//Updating information.
	$pdo->update($element, $newElement);
	
	
	//Getting some record. This can return multiple values.
	$returnObjects = $pdo->get('name', 'PDOMongo');
	
	
	// use the '_id_' operator only if the id was generated by mongo. Example: 
	$returnObject = $pdo->get('_id_', 'd1827dsa9d8a67qwe8q09ueq817'); 
	
	
	// Getting an element using an id when it wasn't generated by Mongo:
	$returnObject = $pdo->get('_id', '1'); 
	
	
	// use an array when multiples values are needed to find a specific record.
	$returnObject = $pdo->get(array(
			'name' => 'PDOMongo', 
			'author' => 'pablohenrique'
			));
			
			
	// Getting all the records from a collection
	$returnObjects = $pdo->getAll();
	
	
	//the delete method can thrown an exception.
	$pdo->delete('id', 'd1827dsa9d8a67qwe8q09ueq817', TRUE); //if TRUE, remove JustOne.
	
	
	// Deleting a record using multiples arguments as an array:
	$pdo->delete(array(
		'name' => 'PDOMongo', 
		'detail' => 'Its a PDO to manipulate MongoDB for lazy people'
		));
?>

Thanks

Thanks for the PHP developers who created the Mongo classes for PHP.

endorse