coen-hyde / Shanty-Mongo

Shanty Mongo is a mongodb library for the Zend Framework. Its intention is to make working with mongodb documents as natural and as simple as possible. In particular allowing embedded documents to also have custom document classes.
Other
200 stars 52 forks source link

errors to query documents with fields consisting of numberic characters #79

Open Oleg-Pro opened 12 years ago

Oleg-Pro commented 12 years ago

Querying of documents with fields consisting of numberic characters fails. For example, if I have a fields that consists only of number something like "1" : 'value' and I try to use: return MyDocument::all($condition, $fields); I get an error: An error occurred Application error Exception information:

Message: field names must be strings Stack trace:

0 C:\library\Shanty\Mongo\Collection.php(376):

MongoCollection->find(Array, Array)

1 C:\git_reps\mailable\application\models\Subscriber1.php(191):

Shanty_Mongo_Collection::all(Array, Array)

2 C:\git_reps\mailable\application\models\Subscriber1.php(203):

Model_Subscriber1::getCursor(Array, Array, Array)

3 C:\git_reps\mailable\application\controllers\ListsController.php(639):

Model_Subscriber1::getPaginator(Array, Array, Array)

4 C:\library\Zend\Controller\Action.php(513): ListsController->view1Action()

5 C:\library\Zend\Controller\Dispatcher\Standard.php(295):

Zend_Controller_Action->dispatch('view1Action')

6 C:\library\Zend\Controller\Front.php(954):

Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))

7 C:\library\Zend\Application\Bootstrap\Bootstrap.php(97):

Zend_Controller_Front->dispatch()

8 C:\library\Zend\Application.php(366):

Zend_Application_Bootstrap_Bootstrap->run()

9 C:\git_reps\mailable\public\index.php(25): Zend_Application->run()

10 {main}

Request Parameters:

array ( 'controller' => 'lists', 'action' => 'view1', 'module' => 'default', 'id' => '52',

It throws error on 376 line in Collection.php: $cursor = static::getMongoCollection(false)->find($query, $fields);

It looks like if to replace it with: $cursor = static::getMongoCollection(false)->find($query)->fields($fields); it works with fields consisting only of number too, maybe corrects the problem. Maybe it's connected with some bug in php driver: https://jira.mongodb.org/browse/PHP-338. Please let me know about your ideas. Maybe to fix it like this?

gwagner commented 12 years ago

A fix for this would be prepend your numeric fields with something like field_{numeric index here}.

Since your dealing in document objects, your attributes should have some sort of meaning other than purely numeric indexes.

Oleg-Pro commented 12 years ago

Mongodb allows to use such fields. Sometimes it's convinient to use such fields.

Oleg-Pro commented 12 years ago

Right now, I fixed it changing $cursor = static::getMongoCollection(false)->find($query, $fields); on line 76 of Collectin.php with this one: $cursor = static::getMongoCollection(false)->find($query)->fields($fields);

gwagner commented 12 years ago

I guess it depends on your development situation. If your a lone programmer that will never have anyone ever look at your code and you will never forget why you have a 1 there, then yes your right, it can be convenient to have your object attributes be numeric indexes. I cant really think of any other reason why you would want to put data into a a DB without any contextual representation of why it is there or what it is.

gwagner commented 12 years ago

I pushed your fix into my branch for general consumption

Oleg-Pro commented 12 years ago

One of situation when it's useful this one: converting Entity Attribute Values model in rdbms, leaving descriptions of fields in rdbs, something like fields_id field_name type. And storing all values in mongodb collection, to prevent all difficulties with gathering all atrributes of objects. In this case attributes could be stores like {field_id: values} In this case getting values of attributes can be much easier, getting them all from mongodb collection.

Oleg-Pro commented 12 years ago

5 апреля 2012 г. 18:10 пользователь gwagner reply@reply.github.com написал:

I guess it depends on your development situation.  If your a lone programmer that will never have anyone ever look at your code and you will never forget why you have a 1 there, then yes your right, it can be convenient to have your object attributes be numeric indexes.  I cant really thing of any other reason why you would want to put data into a a DB without any contextual representation of why it is there or what it is.


Reply to this email directly or view it on GitHub: https://github.com/coen-hyde/Shanty-Mongo/issues/79#issuecomment-4978480

gwagner commented 12 years ago

I guess it is a 6 of one, half a dozen of the other type of situation. Since your doing a numeric link between field name in mongo and RDBMS, you could also do a field name link between RDBMS (again depending on your RDBMS setup if your have a proper short name such a field_name_abcde) which would take care of the bug in the PHP Mongo Driver, and take care of giving you a textual representation in your data set (for administration and bug tracking later).

Either way, thanks for the code that fixed your problem! Hopefully it can be useful to the community as a whole!

Oleg-Pro commented 12 years ago

Fields_id is autoincremented integer field. Yes, it's possible to have some prefix for it, in this it's necessary to convert them from one format to another. It seems more convinient to avoid this conversion.