-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MySQL hasOne relation to Mongo #49
Comments
Can you check out: https://github.com/jenssegers/Laravel-MongoDB/blob/master/tests/RelationsTest.php#L105 It seems to be working for me, or am I missing anything? |
I think the difference is I'm using MySQL for the Image and User models, and MongoDB for the ImageMeta model. Would that cause any problems? |
I think so, because |
Ah right, I was expecting it to return a generic relation which is independent of the databases used. If that's not the case, I expect I can work around it. Or alternatively just not use MySQL and have MongoDB as my main database for everything. Thanks for your help, much obliged. |
Sorry for my delayed response. |
Is there any way to get this working( A relation between a Mongo Model and a MySql Model) ? |
yes this would be awesome using best of both mysql and mongo with same interface would be amazing |
They are using the same "interface", but not the same objects. If you define a |
I'm using both MongoDB and MySQL in my application, and I thought about this issue too. Couldn't you check the connection type? So if we're trying to use a MySQL model as a relationship, wait until after the initial query then get it separately? The fact that the child is a MySQL object should make it easy there on to modify. |
@ollieread, then it would only work in 1 direction, from a mongo-model to original model. But not the other way around. |
@jenssegers I see what you're saying but that would at least solve half the problem, perhaps a second model for Eloquent models, that overrides the relationship handler if the requested object extends your Model? I imagine it'd be relatively simple, I'd be happy to take a look myself? |
Sure, go ahead :) |
Changes
I tested quite a bit on my end, but if somebody else would like to test, then please do so. I have the tests viewable here: http://laravel.ollieread.com/mongo |
can you please show your relations in your Models |
i got belongsTo relation working but could not get hasMany relation working here is my test case User Model public function projects()
{
return $this->hasMany('Project');
} Mysql schema $u = new User;
$u->user_id = Unique_user_id;
$u->username = username;
$u->password = password;
$u->save(); Project Model public function user()
{
return $this->belongsTo('User');
} Mongo Schema $u = User::find(1);
$p = Project([
'title'=>'new project',
'description'=>'demo description of project'
]);
$saved = $u->projects()->save($p); Query Project $p = Project::find('52b142934e3afb6b058b4568'); Query projects user $username = $p->user->username; // this works Query all projects belonging to user $u = User::find(1);
$projects = $u->projects; // this doesn't work
// following query is fired when querying for projects related to user
string 'select * from `users` where `id` = ? limit 1' (length=44)
string 'projects.find({"projects.user_id":10}, [])' (length=42)
// second query uses "user_id" instead of "projects.user_id" it works Am i missing anything here |
Your model that users MySQL, the User model, are you extending ModelCross? Here's my test code: https://gist.github.com/ollieread/8020640 |
Yep that works like charm thanks |
Excellent :) Just waiting for the pull request to be accepted :D |
Need to write unit tests before I can merge this. Will do as soon as I find the time. |
If I get chance I'll have a stab at the unit tests. |
This package should go in core laravel it has almost everything what eloquent has and it's more laravel like then any other mongodb package available . Laravel should have at least one mongodb driver out of the box i think this should totally go in laravel 4.2 |
can you repost links, i have the same situation. |
I've never played with MongoDB before but this is really getting me into it. I'm using MySQL to store basic picture data such as filename, filesize etc, and MongoDB to store metadata which might be different for each picture (exif, ifd0) etc.
I've successfully got the MongoDB model set up and saving:
However when I try to get the metadata back from the image:
It returns NULL. Am I doing something wrong? If I look at the ImageMeta MongoDB model, I can see an image_id key which is populated with the image_id of the Image model.
Image relation:
ImageMeta relation:
Image model table schema:
Any ideas?
The text was updated successfully, but these errors were encountered: