purekid / mongodm Goto Github PK
View Code? Open in Web Editor NEWMongoDB ORM that includes support for references,embed and multilevel inheritance.
License: MIT License
MongoDB ORM that includes support for references,embed and multilevel inheritance.
License: MIT License
in database.php
/** Determine if use your local config file in place of this file **/
$use_local_config = true;
/** The path of your config file , change to fit for your project.
* If your local config file does not exist,this config file will be used.
* **/
$local_config_file = __DIR__."/../../../../application/config/mongodm.php";
if ($use_local_config && file_exists($local_config_file)) {
$array = require $local_config_file;
return $array;
}
can you change 'application' to 'app'?
because the laravel4 default application path is 'app'
in src/Purekid/Mongodm/Model.php, line 658
Method names should be declared in
camelCase
. You should rename this class to comply with PSR-1.
*
* @param boolean $with_namespaces with namespaces
*
* @return string Name of class with namespaces stripped
*/
public static function get_class_name($with_namespaces = true)
{
$class_name = get_called_class();
if($with_namespaces) return $class_name;
$class = explode('\\', $class_name);
Posted from SensioLabsInsight
I have a function need to return a model in json (with all fields, several of them are references). The output, however, only gives "$id" and "$ref" fields instead of the referenced object.
Here is a code of self-explanatory issue:
$data = array('a');
$athlete = new Athlete($data);
var_export($athlete->toArray());
$athlete->save();
var_export($athlete->toArray());
And the output:
array (
0 => 'a',
'_type' =>
array (
0 => 'Athlete',
),
)
array (
0 => 'a',
'_type' =>
array (
0 => 'Athlete',
),
'_id' =>
MongoId::__set_state(array(
'$id' => '5272c0b08a14b53f748b45d5',
)),
'$id' =>
MongoId::__set_state(array(
'$id' => '5272c0b08a14b53f748b45d5',
)),
Note that there are two ids after calling save() method. I noted it when tried to re-use data that I got from toArray() method to create new model:
$data = array('a');
$athlete = new Athlete($data);
$athlete->save();
$data1 = $athlete->toArray();
$athleteNew = new Athlete($data1);
Code above rises exception:
MongoCursorException: localhost:27017: document to insert can't have $ fields
Hi!
as far as i can tell, the model _id has to be a MongoId. that isn't a requirement for mongodb and there are a few cases where specifying a different type of ID makes sense.
I would love to be able to use this ODM but that limitation prevents it ;(
Guys,
preciso tratar um problema, porem com try catch o mesmo continua a explodir. Podem me ajudar?
MongoCursorException [ 11000 ]:
localhost:27017: E11000 duplicate key error index: mobeli.users.$email_1 dup key: { : null }
Try:
public function action_test() {
$signup = new \users\Model_Signup();
$signup->first_name = "João Vagner";
$signup->last_name = "Brito de Medeiros";
$signup->emails = array('default' => '[email protected]');
try {
$signup->save();
} catch (MongoCursorException $e) {
echo "Exceção pega: ", $e->getMessage(), "\n";
}
die('oi');
}
What is your preference on code style. I noticed that there are both tabs and spaces used. Two space and four space...
I propose moving to PSR-2
Here is a simple test that will fail.
<?php
namespace Purekid\Mongodm\Test;
use Purekid\Mongodm\Test\TestCase\PhactoryTestCase;
use Purekid\Mongodm\Test\Model\Pupil;
class BugTest extends PhactoryTestCase {
public function testInit()
{
$id = new \MongoId();
$data = array('_id' => $id);
$user = new Pupil($data);
$user->save();
$testUser = Pupil::id($id);
$this->assertNotEmpty($testUser);
}
}
This issue occurs when _Id
is passed to constructor. As far as I see this is related to this line of code: https://github.com/purekid/mongodm/blob/master/src/Purekid/Mongodm/Model.php#L108
i am trying to use this with gridfs but am not sure if i need to create a modal for a file or not and can't seem to use set_file
The timestamp type exists but I would like to create date fields, i.e. corresponding to MongoDate instances.
@purekid I believe we need to bump version to 2.0 or at least 1.5 here at this commit a07259. This will be a breaking change to some developers who are checking the write state of their update queries.
Another option is to rollback and remove the ability to chain these methods since they have explicit purpose to the response.
I have this question when create new model:
Argument 2 passed to Purekid\Mongodm\MongoDB::__construct() must be of the type array, null given, called in C:\xampp\htdocs\guppys\common\Library\Vendor\purekid\mongodm\src\Purekid\Mongodm\MongoDB.php on line 66 and defined
Please add an auto update hook to packagist.org
It is highly recommended to set up the GitHub service hook for all your packages. This reduces the load on our side, and ensures your package is updated almost instantly. To do so you can go to your GitHub repository, click the "Admin" button, then "Service Hooks". Pick "Packagist" in the list, and add the API key you will find on your profile, plus your Packagist username if it is not the same as on GitHub. Check the "Active" box and submit the form.
I recently met Mongo transaction trouble, I do not know how to deal with. I want to ask you is how to solve. Also, how to use mongo on your plugin findAndModifiy function?
I am not sure what I am doing wrong but this below query is not working, can you help?
if($this->input->has('filter')){
$filter = array( 'name' => array( '$regex' => $this->input->get('filter')) );
}else{
$filter = [];
}
$rawBrands = Brands::find($filter, ['name' => 1], [], 20, $skip);
I get no results, but if I executed the same query directly in mongodb I get lots of matched records, is $regex working in this lib?
The result that i get is always empty, but it should not be the case, either I should get all the brands or the brands that match the filter and yes I do have lots of documents in brand collection which match the search criteria.
Is there any way I can declare local and production config files?
For 2.0 of MongoDM I am proposing that we do some major refactoring to reorganize classes, optimize and implement more complex features.
Ideas:
Hi,
Is there support yet to drop a collection, e.g. User::drop_collection()
or something similar?
In our company we're considering an option to use mongodm in production.
We were trying to run tests and noticed that they are taking very long to pass on our server environement:
$ phpunit
PHPUnit 3.7.20 by Sebastian Bergmann.
Configuration read from /tmp/mongodm/phpunit.xml.dist
.........................
Time: 01:01, Memory: 3.75Mb
OK (25 tests, 56 assertions)
Press any key to continue...
Running phpunit with key --debug showed that it hangs up on 'TestHooks::testInit'.
At the same time it runs less than in a second on my laptop.
Any thoughts?
How would I use regex to search through records? Would it be possible to regex through multiple keys?
Need to enhance to remove the dependance of _id being a MongoId object or converting to it. Any helpers which do automatic conversation would be moved to a plugin rather than embedded into the core.
Hi,
$a references $b
a) this works well
$b->save()
$a->b = Collection::make(array(new b));
$a->save()
b) this seems to work
$b->save()
$a->b->add(new b);
$a->save();
however when I read $a later on $a->b is empty
I am reading $a with A::id(..)
What am I doing wrong in b) ?
in src/Purekid/Mongodm/Model.php, line 675
Boolean property accessor should not be
setIsXXX
norgetIsXXX
butsetXXX
andisXXX
.
*
* @param bool $is_embed
*
* @return null
*/
public function setIsEmbed($is_embed)
{
$this->_isEmbed = $is_embed;
if ($is_embed) {
unset($this->_connection);
unset($this->exist);
Posted from SensioLabsInsight
Added tags: 1.2.0 and 1.3.0
1.2.0 -> master
1.3.0 -> psr2-code-style
Hi,
If I have A reference B
and then delete B like $b->delete()
A still maintains $db, $id, $ref values in the mongodb, is this correct? or am I deleting the reference wrongly?
Are there any plans to add Geolocation data types at some stage ? It would be great to support GeoJSON types.
How are people currently handling simple location data (Points) in MongoDM currently ? Assuming a double array for latitude & longitute.
Hello,
Are you planning on implementing sort and take function?
Regards,
tusharvikky
Hi
Great work!
Any plans to release a version with pure MongoDB extension support?
Regards.
great package! is there an easy option to switch between different database using the same connection? preferably, have a $database complement $collection in the Model definition.
I have a object field which stores several different information depends on the instance.
Is there a way to modify the object's data and then save it?
Coz now it's not working as expected, like codes below.
$item->obj->value+=1;
$item->save();
this does not work
Validating data with specify rules of attribute , throw error messages when validating failed.
Inspired by: http://laravel.com/docs/validation#rule-in
protected static $attrs = array(
'age' => array('type'=>'integer','rules'=>'required|min:5|max:100'),
'gender' => array('type'=>'string','rules'=>'in:male,female')
);
In the future my team and I plan on making many improvements to this project. How available are you to maintaining it?
The MongoDB extension installed via pecl for PHP 7 has changed a bit and it doesn't work with mongodm anymore. Are there any working branches for a port to the newer mongo lib?
Class '\Mongo' not found
That class and \MongoClient
have been deprecated.
New one is MongoDB\Driver\Manager
etc.
I noticed there are numerous method conflicts with getEmbed
, setEmbed
, getConnection
, etc. These all create issues with the the magic methods found here: https://github.com/purekid/mongodm/blob/master/src/Purekid/Mongodm/Model.php#L1504-L1513
Hello,
Either we could just throw Unable to connect to server, Please contact Server Admin.
-or- something like this.
https://github.com/purekid/mongodm/blob/master/src/Purekid/Mongodm/MongoDB.php#L189
I am assuming it is used to maintain the association of an object if it doesn't have it's own collection like this.
class Person {
public $collection = 'people';
}
class Student extends Person {}
However it adds a lot of unnecessary overhead to store _type
unless someone is storing the different documents in the same collection. Let me know if this the only function because I want to build a dynamic switch (i.e. if there is no collection defined then set _type else omit)
Also I would suggest switching to a string rather than array. It is easier on the database with regards to storage and memory and would permit indexing. (i.e. Base\Person\Student
)
Fields passed ad hoc should either be omitted or meet one of the datatype criteria.
For instance if an object is passed to mongodm it should either:
Im trying to find a model based on reference, my code is below:
$user = \Models\User::id($id);
$threads = \Models\Thread::find(array('user' => $user));
And im geting the error "zero-length keys are not allowed, did you use $ with double quotes?"
Someone know what im doing wrong?
Thanks in advance and thanks for the great project.
In my Mongodb is exist App and User collection, is 1:many relationship.
now, App has 1000 Users. my register code : '$app->users->add($user)'.
is very slow! when there are more and more registered users, the system is more and more slow.
so i viewed "Purekid\Mongodm\Model" source code, and i test ‘one’ method, i find when my call ‘$app->users->add($user)’, in "Purekid\Mongodm\Model::one" method will call "self::connection()->findOne(static::$collection, $criteria, self::mapFields($fields))" to load all
user document in memory. how to solve this...
English is not my native language, may I write some grammar mistakes, please understand
Can i use embed's mongo models?
With MongoDB stopped mongodm throws an exception, which is expected.
But if we try to save something again, it crashes:
PHP Fatal error: Call to a member function selectCollection() on null in /home/willian/htdocs/test/vendor/purekid/mongodm/src/Purekid/Mongodm/MongoDB.php on line 708
<?php
require 'vendor/autoload.php';
\Purekid\Mongodm\MongoDB::setConfigBlock('default', array(
'connection' => array(
'hostnames' => 'localhost',
'database' => 'test'
)
));
class Test extends \Purekid\Mongodm\Model {
protected static $collection = "test";
}
$record = new Test();
try {
$record->save();
}
catch (\Exception $e) {
echo 'Catch exception #1 ', PHP_EOL;
}
try {
$record->save();
}
catch (\Exception $e) {
//
echo 'Catch exception #2 ', PHP_EOL;
}
I am trying to follow your example but not sure where I'm going wrong with this:
$user = new User();
$user->first_name = "tester";
$user->save();
$id = $user->_id;
$relationship = new Relationship();
$relationship->is_confirmed = true;
$relationship->save();
$user->relationships = Purekid\Mongodm\Collection::make(array($relationship));
$user->save();
Reading this exception:
MongoException: zero-length keys are not allowed, did you use $ with double quotes?
In User.php
'relationships' => array( 'model' => 'RelationShip', ' type' => 'references' )
Any hint?
Hello,
I stumbled upon Distinct. Conventional Vanilla php driver sucks. Any way I could do that via Mongodm.
Regards
There are a lot of calling like self::connection()
in \Purekid\Mongodm\Model
.
Why don't you use static::connection()
?
Because of this issue I can't override some methods in my subclass.
For example:
class BaseModel extends \Purekid\Mongodm\Model
{
// override \Purekid\Mongodm\Model::connection()
protected static function connection()
{
die(__METHOD__);
}
}
class Posts extends BaseModel
{
// ...
}
Posts::one();
in this simple example method BaseModel::connection()
will never be executed.
Moreover you use a lot of constructions like
$class = get_called_class();
$class::$config;
$class::getAttrs();
instead you should use
static::$config;
static::getAttrs();
Guys, What is the possibility or (stepping stones "Brazilian proverb"!) To implement a check for Unique. For example the input "email" should at least be unique.
A logic using postInsert or creating an attribute indicating that this Unique?
Extiste some package that works with this bundle?
?
To improve speed to updating data I propose an optional model when updating data to lazily update by use of the findAndModify
command.
Note: This option would make the assumption that you are content with the data you are saving. That is to say, when it gets executed it will replace data as specified.
<?php
// Goal:
// 1. db.Person.findAndModify({_id: ObjectId('12345')}, {$set: {name: 'John Smith', age: 30}})
// 2. Consistant API
// defaults: sort = [], update = true, new = true
$options = array(
'sort' => array(),
['update' => true | 'remove' => true],
'new' => true,
'fields' => array(),
'upsert' => false
);
Person::lazy($options); // or Person::findAndModify();
$p = Person::id('12345');
$p->name = 'John Smith';
$p->age = 30;
$p->save();
//-- OR --
// Create new methods (via trait)
$p = Person::pId('12345', $options);
$p->name = 'John Smith';
$p->age = 30;
$p->save();
The choices we have from my perspective is either create some magic to set a flag for lazy/findAndModify/etc mode or clone existing functions.
20% of all classes are larger than 500 lines. Split them up to keep less than 5% large classes.
Posted from SensioLabsInsight
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.