[Extension] Mongoyii

It uses much the same setup as Yii but nested within the root document in array form. Hmmm, it seems that the error summary cannot just take a plain array which kinda sucks.

I might need to write an additional little function some where that can do what CHtml cannot.

CHtml::errorSummary() is designed to take instances of CModel, and EMongoModel extends from that class, hence it has getErrors() which is what errorSummary() use. What is the problem, can you not give it an instance of EMongoModel?

Also, I’d like to point out that EMongoModel shouldn’t override getErrors() since it doesn’t do anything that the parent equivalent doesn’t do. Removing this (and potentially other overriding methods that does the same thing as parent) can clean up the class a bit :)

The problem is that subdocuments are not enclosed in EMongoModel classes, they are simply arrays and the error summary functions do not recognise array nesting for errors.

Yea, however, if you check the comments above it you will realise that I added a new method which modified a private variable in the parent. PHP OO accession works in such a way that if you want to modify a parent variable directly that is of private scope you cannot. You have to duplicate all the code associated with that var whose functionality you wish to keep.

Gotha. Too bad. Hate redundancy :slight_smile:

Indeed I hated the thought of it too :( if only Yii had getters and setters for direct manipulation of the errors var :(

Can’t you use a combination of getting and setting/complementing the errors from the parent?

The only way to add errors to the var atm from the parent instance is to assume you are adding a message to a field and nothing else I don’t think, unless you mean something else?

To let people know, I will most likely start to remake MongoYii for Yii2 in the coming weeks/months (depending on how long it takes for the Yii team to say that the API etc is not going to change).

So I have full intention of porting MongoYii in its current form, hopefully without breaking too much, to Yii2

Is just me that the MongoCursor always returns empty whichever would be the collection?

I’m doing this:




<?php

echo '<pre>';


var_dump( Yii::app()->mongodb->historical->find() );


echo '</pre>';

?>



I’m sure the collection name historical and the db in the config are totally correct, and of course the collection has results.

Due to how MongoDB works that won’t get a cursor but instead a MongoCollection object. This is fundamentally how the PHP driver works. If you want a MongoCursor to return you will need to use a read function like find or findOne

I am afraid that’s not happening to me.

Var_dump returning the following:


object(MongoCursor)#44 (0) {

}



And by analysing my code you can see that I’m using the method find() to return it.

Yes it will only return a MongoCursor, what exactly are you trying to return? If you are trying to return all results from that collection then you can try iterator_to_array(&#036;cursor)

That made it! I didn’t even know about what iterators where. Gonna read more about it.

But, in another way how could I get that data without doing this? (as it was not mentioned elsewhere, I suppose there is another way of getting this data into a Yii view for example)

Thanks!

(by the way, I would recommend you to add a small note about it in the project Wiki and/or Github to help people that were looking specifically for this)

The driver docs do a very good job of explaining all of its own features, if you read: http://php.net/manual/en/mongo.tutorial.php , more specifically: http://www.php.net/manual/en/mongo.tutorial.cursor.php it should explain all the things not related to the MongoYii extension itself.

Thanks a lot :)

Hello.

I’m trying to migrate from MySQL (ActiveRecord) to MongoDB using MongoYii and I found that saveAttributes($attributes) method does not work like it does in AR. It should “save a selected list of attributes”, where $attributes are “attributes to be updated”.

But in MongoYii, saveAttributes() erases all other attributes which are not passed to this method.




class EMongoDocument extends EMongoModel{

    public function saveAttributes($attributes) {

        ...

        return $this->updateByPk($this->{$this->primaryKey()}, $values);



In my opinion, it should work like in update() method:




class EMongoDocument extends EMongoModel{

    public function update($attributes=null) {

        ...

        $this->lastError = $this->updateByPk($this->{$this->primaryKey()}, array('$set' => $attributes));



What do you think?

Ouch that is definitely a bug

I have added version 2.2.4 to the github repo which should incldue this fix now, thanks for reporting

Thank you for a quick fix!

Hi, Sammaye

I’m trying to update model by pushing an array, example




$attributes = array(

    'test' => 'message',

);

House::model()->updateByPk(

                new MongoId($id),

                array(

                    '$push' => array('history'=>$attributes)

                )

            );



But it always pushes this array 3 times. What’s wrong with this update query?