Using MongoDB with PHP without ODM - php

I have simple MongoDB collection and I am using SLIM micro-framework to develop a RESTful mobile back-end. I don't need to implement an ODM ( Object Document Mapper ) since its very tiny collection and it wont expand in the future, but at the same time, i need to manage the MonogDB connection as we do in Singleton design pattern so I don't create instances every time I need to query MongoDB.
The question is: Does PECL MongoDB extension provided by PHP manages the connection pooling and handle this issue, or I have to write a Singleton class and totally depend on it to manage the MongoDB instances. thank you.

It does the pooling for you. See http://php.net/mongo.connecting . Sounds like you'll also want persistent connections.
What's "single-tone"?

Related

Redis as NoSQL Key/Value Database in CakePHP 3

I'am wondering what is the best way to use Redis NoSQL Database in CakePHP not for Caching but as regular Key/Value Store?
There is a Plugin for Redis 2.4.2 or higher, but the Plugin does not work with CakePHP 3.x: Redis CakePHP 2.4.2 Plugin
What is the best way to use Redis in CakePHP 3 as regular Key/Value Database? How costly is it to upgrade the CakePHP 2.4.2 Redis-Plugin to a CakePHP 3.x Plugin. Is the migration of the Plugin the best way or is it better to create a new Datasource? (The current Redis-Plugin extends the Datasource-class.)
Take a close look at what that datasource does, it's pretty much just a proxy to a Redis class instance, other than that it only handles connecting and disconnecting.
So besides having auto connect/disconnect functionality, there is pretty much no point in implementing such a datasource, and you could also just use Redis directly instead.
https://github.com/phpredis/phpredis#classes-and-methods
If you'd wanted to use it as a datasource, ie have it accessible via the connection manager, then you'll have to implement \Cake\Datasource\ConnectionInterface.
If you are looking for a datasource that acts as an ORM, that's a complete different story, and a rather complex task, see for example the CakePHP ElasticSearch Datasource plugin.

Which MongoDB library to use for a stand-alone PHP project with Symfony?

I am leading a new project where we're convinced that MongoDB is the right choice for database. We have decided that the architecture would be SOA, so the web part will be developed using Symfony and the service part will be developed using light-weight REST framework Tonic.
Now, in the service part, we'll be communicating with MongoDB and for that, we have looked into a number of available MongoDB libraries: Doctrine MongoDB ODM, Mondango, ActiveMongo, MongoRecord, etc. However, we are not sure which one to pick.
I was wondering if anyone can share their experience with these libraries so that we can make the right choice. Here are some of the properties we consider the library should have:
Plain PHP classes for defining documents (instead of array/config files)
Support for references
Efficiency in operations
Easy to understand API
Looking forward to your views!
Personally I would go with Doctrine2 ODM. Seen as you have already decided on Symfony as your framework for doing the heavy lifting the pair are well aligned as far as I understand. You should be able to use this https://github.com/doctrine/DoctrineMongoDBBundle to integrate the two pretty quickly.
The doctrine setup ticks all the boxes you've set for your project goals and is fairly easy to get working with. Most importantly, it's an active project so bug fixes/features/documentation updates happen fairly regularly.
We use a similar setup, except zend framework instead of Symfony, and we're very happy with the results.
Hope this helps.

Codeigniter and MongoDB

I am testing out PHP (Codeigniter) and Mongo (library by alexbilbie).
I a new to MongoDB so I got a few starting questions.
Is the library by Alex ready for production use? Can I use all mongodb features?
Is Alex library the best for Mongodb to Codeigniter use?
Do I have to use chained queries like this?
$this->mongo_db->where(array('firstname' => 'Michael'))->get('users');
I want each "part" on a separate row like this:
$this->mongo_db->select('*');
$this->mongo_db->from('users');
$this->mongo_db->where(array('firstname' => 'Michael'));
$query = $this->db->get();
Thank you!
$this->mongo_db->where(array('firstname' => 'Michael'))->get('users');
This is very similar to how the driver operates which is likely why it was chosen. The syntax you're looking for is basically an SQL variant. While possible, this is not generally how it's done with MongoDB as MongoDB doesn't use SQL.
Is the library by Alex ready for production use? Can I use all mongodb features?
In terms of production-readiness, you'll have to run your own sanity checks. MongoDB is backed by a well-funded company (10gen) and the PHP driver is maintained by them. So you should be good there.
For the library, all of the commits have been performed by one maintainer. It's probably in production being used by him, but the code has zero unit tests which is not a great sign.
The wiki would seem to imply that not all features are covered. He even has "maybe" written beside things like "add user".
Is Alex library the best for Mongodb to Codeigniter use?
This is very subjective, how many MongoDB+Codeigniter libraries have you found?

PHP ORM with NOSQL and RDBMS support

Are there any PHP ORM projects which supports MySQL and NoSQL databases such as MongoDB?
I am currently using RedBean to do MySQL ORM, however I woud like to introduce MongoDB to the applicatoin and perhaps even replace some of the MySQL with MongoDB in the future.
An ORM that can allow me to easily transition between both would be nice. However, I tend to not like ORM that requires too much configuration (i.e. YAML, XML etc). RedBean is very nice in that it allows one to easily get things working without too much configuration.
Yes, doctrine supports various RDBMS and NoSQL storages as well
Docrine 2 contains both an ORM and ODM for NoSQL databases (MongoDB and CouchDB are supported), and its theoretically possible to hook up Doctrine to anything else.
We're working on some adapter-based ORM called UniMapper in our company.
It provides a uniform API for accessing stuff from different kinds of databases, protocols, and 3rd party APIs. You can even associate between entities from different storages. And it should be even faster than Doctrine. Try it give us some feedback :-).
MongoDB will be supported very soon, but you can write extension on your own, it's very easy.
However the quick start is not complete, you can ask me or somebody from our team.

Zend Framework EAV Implementation

I was wondering if there were any good pre-built ways to implement an EAV design pattern in Zend Framework?
I am trying to decide if I should create my own implementation or use one that has already been built.
Use Doctrine with MongoDB Document Mapper http://www.doctrine-project.org/
Zend Framework 2.0 has automated install for Doctrine with Composer, so it means Zend People also offer this as an Option apart from the built-in Zend_Db.
Using EAV on a document-oriented NoSQL database system like MongoDB or CouchDB is way much better than implementing a Hack over Traditional Relational Databases like MySql ( something that Magento did ). NoSQL Databases are better at implementing Sparse Matrix type Data.
Check out Zend Framework EAV on Google Code.
Digitalus CMS uses the EAV pattern, and is based on MySQL. It is true that Mongo or Couch might be more appropriate, but MySQL is often an easier choice because you don't have to install anything. I have tried using SimpleDB, but the local MySQL version is 4x faster. SQLite also works very well.
There are a lot of opponents to this approach, and their claims are largely founded. Any time I need to be able to query data I use a standard relational approach, but EAV shines when you are working with very loosely structured data like the content on a web page.
Digitalus uses an approach where there is the base CMS item model that handles all of the EAV logic. This model also handles a write-through cache so the system ultimately serves content as fast as a flat file system.
All the purists are probably technically correct, but fast and easy have a place in my toolbox.

Categories