This extension is an almost complete, ActiveRecord like support for MongoDB in Yii It originally started as a fork of MongoRecord extension written by tyohan, to fix some major bugs, and add full featured suite for MongoDB developers.
IMPORTANT Info: I've developed this extension as a hobby project. Now I had stopped using Yii in my hobby and work projects. I'm no longer maintaining this code. My codebase is available for easy fork/download on a github. Feel free to continue my work, YMDS already has active community, you can find support mostly on google groups (described bellow).
PLEASE refer to the new FULL-Documentation page
There is also an google groups for topics related to YMDS, everyone are welcome, to post threads, questions, and support requests there.
Work-around for using the OR operator with this extension provided in comments
This is the 1.4 preview release 1
The YMDS feature list has grown up recently, some of new ones are:
OR operator with criteria objectThis is a Preview Release of YMDS 1.4 it is not ready for stable usage I publish it because I need help with testing and code stabilization, anyone are welcome to download, test and submit bug fixes.
EMongoPartialDocument class, that supports full-featured partial loading of documents from DB$set operator/featureIMPORTANT: The version on GitHub is more up to date as fixes are pushed to the project. This may or may not get updated on a regular basis
In your protected/config/main.php config file. Comment out (or delete) the current 'db' array for your database in the components section, and add the following to the file:
'import' => array( ... 'ext.YiiMongoDbSuite.*', ), 'components' => array( ... 'mongodb' => array( 'class' => 'EMongoDB', 'connectionString' => 'mongodb://localhost', 'dbName' => 'myDatabaseName', 'fsyncFlag' => true, 'safeFlag' => true, 'useCursor' => false ), ),
'connectionString' => 'mongodb://username@xxx.xx.xx.xx' where xx.xx.xx.xx is
the ip (or hostname) of your webserver or host.That's all you have to do for setup. You can use it very much like the active record. Example:
$client = new Client(); $client->first_name='something'; $client->save(); $clients = Client::model()->findAll();
Just define following model:
class User extends EMongoDocument { public $login; public $name; public $pass; // This has to be defined in every model, this is same as with standard Yii ActiveRecord public static function model($className=__CLASS__) { return parent::model($className); } // This method is required! public function getCollectionName() { return 'users'; } public function rules() { return array( array('login, pass', 'required'), array('login, pass', 'length', 'max' => 20), array('name', 'length', 'max' => 255), ); } public function attributeLabels() { return array( 'login' => 'User Login', 'name' => 'Full name', 'pass' => 'Password', ); } }
And thats it! Now start using this User model class like standard Yii AR model
NOTE: For performance reasons embedded documents should extend from EMongoEmbeddedDocument instead of EMongoDocument
EMongoEmbeddedDocument is almost identical as EMongoDocument, in fact EMongoDocument extends from EMongoEmbeddedDocument and adds to it DB connection related stuff.
NOTE: Embedded documents should not have a static model() method!
So if you have a User.php model, and an UserAddress.php model which is the embedded document. Lest assume we have following embedded document:
class UserAddress extends EMongoEmbeddedDocument { public $city; public $street; public $house; public $apartment; public $zip; public function rules() { return array( array('city, street, house', 'length', 'max'=>255), array('house, apartment, zip', 'length', 'max'=>10), ); } public function attributeLabels() { return array( 'zip'=>'Postal Code', ); } }
Now we can add this method to our User model from previous section:
class User extends EMongoDocument { ... public function embeddedDocuments() { return array( // property name => embedded document class name 'address'=>'UserAddress' ); } ... }
And using it is as easy as Pie!
$client = new Client; $client->address->city='New York'; $client->save();
it will automatically call validation for model and all embedded documents! You even can nest embedded documents in embedded documents, just define embeddedDocuments() method with array of another embedded documents IMPORTANT: This mechanism uses recurrency, and will not handle with circular nesting, you have to use this feature with care :P
You easily can store arrays in DB!
Simple arrays
Arrays of embedded documents
// add a property for your array of embedded documents public $addresses; // add EmbeddedArraysBehavior public function behaviors() { return array( array( 'class'=>'ext.YiiMongoDbSuite.extra.EEmbeddedArraysBehavior', 'arrayPropertyName'=>'addresses', // name of property 'arrayDocClassName'=>'ClientAddress' // class name of documents in array ), ); }
So for the user, if you want them to be able to save multiple addresses, you can do this:
$c = new Client; $c->addresses[0] = new ClientAddress; $c->addresses[0]->city='NY'; $c->save(); // behavior will handle validation of array too
or
$c = Client::model()->find(); foreach($c->addresses as $addr) { echo $addr->city; }
This is one of the things that makes this extension great. It's very easy to query for the objects you want.
// simple find first. just like normal AR. $object = ModelClass::model()->find()
Now suppose you want to only retrieve users, that have a status of 1 (active). There is an object just for that, making queries easy.
$c = new EMongoCriteria; $c->status('==', 1); $users = ModelClass::model->findAll($c);
and now $users will be an array of all users with the status key in their document set to 1. This is a good way to list only active users. What's that? You only want to show the 10 most recent activated users? Thats easy too.
$c = new EMongoCriteria; $c->active('==', 1)->limit(10); $users = ModelClass::model->findAll($c);
It's that easy. In place of the 'equals' key, you can use any of the following operators
- 'greater' | >
- 'greaterEq' | >=
- 'less' | <
- 'lessEq' | <=
- 'notEq' | !=, <>
- 'in' |
- 'notIn' |
- 'all' |
- 'size' |
- 'exists' |
- 'type' | // BSON type see mongodb docs for this
- 'notExists' |
- 'mod' | %
- 'equals' | ==
- 'where' | // JavaScript operator
*NOTICE: the $or operator in newer versions of mongodb does NOT work with this extension yet. We will add it to the list above when it is fixed.Newer versions of mongo db will work, just not the $or operator. For examples and use for how to use these operators effectively, use the MongoDB Operators Documentation here.
Here are a few more examples for using criteria:
// first you must create a new criteria object $criteria = new EMongoCriteria; // find the single user with the personal_number == 12345 $criteria->personal_number('==', 12345); // OR like this: $criteria->personal_number = 12345; $user = User::model->find($criteria); // find all users in New York. This will search in the embedded document of UserAddress $criteria->address->city('==', 'New York'); // Or $criteria->address->city = 'New York'; $users = User::model()->findAll($criteria); // Ok now try this. Only active users, only show at most 10 users, and sort by first name, descending, and offset by 20 (pagination): // note the sort syntax. it must have an array value and use the => syntax. $criteria->status('==', 1)->limit(10)->sort(array('firstName' => EMongoCriteria::SORT_DESC))->offset(20); $users = User::model()->findAll($criteria); // A more advanced case. All users with a personal_number evenly divisible by 10, sorted by first name ascending, limit 10 users, offset by 25 users (pagination), and remove any address fields from the returned result. $criteria->personal_number('%', array(10, 0)) // modulo => personal_number % 10 == 0 ->sort(array('firstName' => EMongoCriteria::SORT_ASC)) ->limit(10) ->offset(25); $users = User::model()->findAll($criteria); // You can even use the where operator with javascript like so: $criteria->fieldName('where', ' expression in javascript ie: this.field > this.field2'); // but remember that this kind of query is a bit slower than normal finds.
You can use native PHP Mongo driver class MongoRegex, to query:
// Create criteria $criteria = new EMongoCriteria; // Find all records witch have first name starring on a, b and c, case insensitive search $criteria->first_name = new MongoRegex('/[abc].*/i'); $clients = Client::model()->findAll($criteria); // see phpdoc for MongoRegex class for more examples
for reference on how to use query array see: http://www.php.net/manual/en/mongocollection.find.php
// Example criteria $array = array( 'conditions'=>array( // field name => operator definition 'FieldName1'=>array('greaterEq' => 10), // Or 'FieldName1'=>array('>=', 10) 'FieldName2'=>array('in' => array(1, 2, 3)), 'FieldName3'=>array('exists'), ), 'limit'=>10, 'offset'=>25, 'sort'=>array('fieldName1'=>EMongoCriteria::SORT_ASC, 'fieldName4'=>EMongoCriteria::SORT_DESC), ); $criteria = new EMongoCriteria($array); // or $clients = ClientModel::model()->findAll($array);
Total 20 comments
@intel352
thank you for your reply, that is right.
Re: #7485 by @elsonwu
The performance that would occur in your fix is not ideal, as your solution would issue a count and a find for every unique validation, and a find request is already less performant than a count request.
I suggest you instead use the patch I provide here: https://github.com/intel352/YiiMongoDbSuite/commit/65f7ae77c9b4364ba22a94c322555b33b6a16222
When I update a record, such as user and I set the field email unique. I update the username and the email not modified, then it will show error. Here is my bugfix for it.
https://github.com/mintao/YiiMongoDbSuite
anyone forked this extension ?
Can this work together with CActiveDataProvider? -> Yes it does for sure; I am using MongoDB with MySQL.
Is this question make sense? -> Not really since you are using Yii where compatibility and interoperability matters.
Can this work together with CActiveDataProvider?(asked from a beginner, because I just learned how to use data provider today:) ) It seems it's not mentioned in the Manual.
Is this question make sense?
It definitely is :) Besides the fact that caching was added here and there (see my previous comment) and a new version of deleteByPk to mimic the default behavior (Yii's CActiveRecord implementation of it does not call before/afterDelete) it is being used as is and working very good.
yes it is. see Yii-powered Applications: v-ticket.at
Please let me if you have found it good enough to use in a production environment.
Thanks!
Great extension!
I hope you allow me to give you a small piece of advice :)
Determining meta data for databases is usually very slow. It's the same for mongo. It's the reason Yii caches it by default.
Depending on your server a call MongoCollection::getIndexInfo() can take 500ms or more to complete. Since you are doing it in your init() function on every page, this will cause serious issues when you are running under higher load (trust me, we experienced it :) ). Best thing is to just use the Yii::app()->cache to temporary cache the actual data so that you can just load it out of the cache for most pageviews.
We just did:
This gives you a huge performance boost for almost no work.
By defining attributes by properties, you lost one of the biggest privilegies of MongoDB, that can have non regular structure. What if add new method, that will add custom attributes in protected property $_attributes like in CActiveRecord..
On save code might look like this.
And on find:
Hopefully my advice will help you, MG.
Please read the info on top of this page, this project is free for take-over.
How to make Nested Embedded Documents???
http://pastebin.com/raw.php?i=wmBydGSR
In EMongoDocument Class , the findByAttributes and findAllByAttributes method has only one parameter, but in the comment, also I think is needed, should add the $criteria parameter. And these two methods also lost trace log code. See the original code:
may should be like this:
hi canni : thanks for your great extension!
want to know if i can use both RDBMS and the Mongodb in one system , the relation i just use method declaration but the yii 's way(which return some arrays ) , ie: in User AR class
the Address is MongoDocument . this is an one to many relation example ,other relations implement the same way . i need some advice .
and the softdocument is very useful , little advice: when a function accept an array which all elements is string ,i will use this way :
now i can use such way to call the function: $model = new MixedModel();
$model->initSoftAttributes(array('field2', 'field3')); ///or $model->initSoftAttributes('field4,field5');
I see now you use reflection of public attributes.
Hi,
I have been browsing your documentation but you seem to treat class variables all the same. There is no need to pre-define a schema. This is a problem since I have classes in my Yii project which use non-db variables.
I was wondering how do you fill the metaData array that Yii uses in SQL if mongo has no schema?
Work-around for using the OR operator is not found in the comments anymore (it got deleted?) I have been using this regex solution which works for me (when working with strings):
board:/ai|uai/ avg 0.000650s
board:/^(uai|ai)$/ avg 0.000780s
Great! I found it can't use CUniqueValidator rule so far now.
Leave a comment
Please login to leave your comment.