[Extension] Mongoyii

Indeed let me think about this: https://github.com/Sammaye/MongoYii/issues/125 I need to think of what it could break and why it was done like that etc etc.

This trials the fix: https://github.com/Sammaye/MongoYii/releases/tag/2.3.0

Thank you very much for your quick and constructive response!

Maybe you could suggest to me something for another difficulty with dubdocuments? Because I really start to rethink of using them.

For example I have a subdocument named "en" with title, text, and some additional attributes, which are not displayed in the edit form.

When I populate model with POST data


$model->attributes = $_POST['News'];

I lose all attributes from subdocument which were not added to the form.

I can:

  1. Add all data to the form in hidden fields, but it’s auxiliary data, which I do not want to show on a page.

  2. Change names of form fields, so POST data won’t replace subdocument, but I will have to rename attributes in model’s labels, validation rules, manually assign attribute values in form and before saving, etc.

Or there is an easier way, which I don’t see?

If you wish for a more complicated subdocument I would consider using an actual class instead of the subdocument validator.

The validator can only do so much automatically before it starts to flounder. It simply cannot understand the context of complex work flows, I left this understanding upto using actual class based subdocuments.

So I would split you subdocuments out into classes because I would say they are comlpex enough, the subdocument validator is good at automating very simple and basic subdocuments but anything more and you wanna use something a but more complicated.

I actually use class for some subdocuments. But the problem is the same that the method of setting attributes is replacing subdocument (object or array) with incomplete data from a form. It assigns values not to the separate subfields in subdocument, but takes an array from POST data and assigns it to subdocument attribute.

It is? Is this using the subdocument validator or actually doing it manually via building the array and appending its elements?

Because that’s what I meant.

I mean there is one idea if making the subdocumentvalidator aware of the subdocuments current entries if that is what your using.

I am however unsure how easy or secure or stable it would be to add the change to the subdocumentvalidator though, when you start getting into this sort of territory you should be doing the subdocuments manually really.

Only you know the context

I also hit a snag with pogrammably setting the labels for subdocuments. Yii uses the function attributeLabels() and only that function, unfortunately in PHP you cannot actually programmably define the contents of a function.

There is a couple of ways around it but both are hacky and could be nasty:

  • Override Yii itself potentially causing breakages in the future by changing getAttributeLabel()

  • create a map inside of the created model, this would require huge amount of recursion.

There is another way. In the model you can define:

		array('profile','subdocument','type'=>'one','rules'=>array(


			array('title','length','max'=>12,'tooLong'=>'Second title is bad'),


			array('url','url')		


		)),

And in the attribute labels you can define:

array(

'profile[title]'=>'Second title'

)

And that would actually do everything you want.

The latest push should support that

I am new to Yii, so excuse me for a stupid question.

Try test connection. Wrote


Yii::app()->mongodb

and got


include(Mongo.php): failed to open stream: No such file or directory

What I’ve done incorrect?

Are you sure you have installed the php driver? http://php.net/manual/en/mongo.installation.php

Yes…But i wrote "xtension" in php.ini

Thanks! You save a lot of my nerve ))

Hello, Sammaye!

Could you please add the populateRecords() method to the EMongoDocument class? It can be quite useful after MongoDB aggregation to convert an array of records from the result to an array of objects of specific class.

There is EMongoDocument::populateRecord() for 1 record, but in Yii’s CActiveRecord class there is also the mothod for array of records. Example from Yii:




public function populateRecords(array $data, $callAfterFind = true, $index = null)

{

    $records = array();

    foreach ($data as $attributes) {

        if (($record = $this->populateRecord($attributes, $callAfterFind)) !== null) {

            if ($index === null) {

                $records[] = $record;

            } else {

                $records[$record->$index] = $record;

            }

        }

    }

    return $records;

}



Done and added to 2.4.0 cheers: https://github.com/Sammaye/MongoYii/releases/tag/2.4.0

Thank you! Already in use )

Hello, Sammaye.

Have you thought to implement migration algorithm for MongoDB in your extension?

MongoDB is structureless, but there are still indices that need to be ensured. And there might be a necessity to remove old attributes or add new ones in bulk.

That sounds like a whole new extension in itself in many ways.

I havent thought about it becaue many times it is considered a bad idea to merely migrate, you should redesign for MongoDB.

Hi elke,

i already had a lot of thoughts regarding this topic. I opened an issue on github some days ago (github.com/Sammaye/MongoYii/issues/146), if you got some other ideas, don’t hesitate to leave your thoughts there or contact me. Im really interested in how other people face this issue.

Hi, redfox,

I’ve read the issue on github and I’m not sure I like the technique of getters for changed attributes. It’s a definite way to a large, unmanageable legacy code, difficult to maintain in the future by people who won’t know a thing about old db structure.

And there is still modification of indices, which cannot be added to the model.

During development I add new indices for new queries, and the person, who rolls out a new version, must have instructions which indices to ensure or delete. I also see it as a migration and would like to save this instructions beside the code in repo.

I think that I will implement something similar to Yii’s migration algorithm. I’ve just started to use MongoDB with Yii, so my project is not big yet, but I already feel a problem here.

I used to allow modifcation of indeces through the application but then I suffered a serious loss of data and uptime due to a newbie who inadvertantly messed up and made a very large index for the cluster rebuild itself.

So I am firmly against inplementing index functions in the models, it just asking for trouble with future generations of programmers.

I’m a bit of a newbie with Yii but have used multiple other frameworks on the past.

I’m trying to use two MongoDB collections in a relationship but aren’t sure quite how to go about it. I’ve spent most of the day trying different things…to no avail.

I have 2 collections.

products (which should have multiple offers)

offers (which has one product)

Going off the Docs as I understand them:


    public function relations() {

        return array(

            'offers' => array('many', 'Offer', 'product_id'),

        );

    }

is in my products model.

Calling for example $products = Product::model()->find()->limit(50);

returns 50 products but no related offer data. I tried using with etc. etc.

According to the docs I need an on clause:

Does anyone have an example of an on clause and where it needs to go?

Thanks to anyone who can show me some light.

Cheers,

Taff