Skip to content
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

Closed
tdicks opened this issue Oct 10, 2013 · 22 comments
Closed

MySQL hasOne relation to Mongo #49

tdicks opened this issue Oct 10, 2013 · 22 comments

Comments

@tdicks
Copy link

tdicks commented Oct 10, 2013

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:

   $user = User::whereUsername('test_username')->first();
   $img = new Image();
   $img->filename = "hello.jpg";
   $img->save();

   $meta = new ImageMeta();
   $meta->exif_maker = "Canon";
   $meta->exif_model = "EOS 500D";
   $meta->save();

   $img->meta()->save($meta);

However when I try to get the metadata back from the image:

   echo $img->meta->exif_maker;

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:

$this->hasOne('ImageMeta');

ImageMeta relation:

$this->belongsTo('Image');

Image model table schema:

        Schema::create('images', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('filename');
            $table->integer('filesize');
            $table->string('hash');
            $table->string('hash_type');
            $table->integer('user_id');
            $table->timestamps();
        });

Any ideas?

@jenssegers
Copy link
Contributor

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?

@tdicks
Copy link
Author

tdicks commented Nov 19, 2013

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?

@jenssegers
Copy link
Contributor

I think so, because $img->meta() will return an SQL relation, and not a MongoDB relation.

@tdicks
Copy link
Author

tdicks commented Nov 19, 2013

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.

@jenssegers
Copy link
Contributor

Sorry for my delayed response.

@amacgregor
Copy link

Is there any way to get this working( A relation between a Mongo Model and a MySql Model) ?

@programming-kid
Copy link

yes this would be awesome using best of both mysql and mongo with same interface would be amazing

@jenssegers
Copy link
Contributor

They are using the same "interface", but not the same objects. If you define a belongsTo or any other relation on an original sql model object, it will return a mysql relation. And if you define a relation on a mongodb model, it will return a modified object, specific for mongodb.

@ollieread
Copy link

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.

@jenssegers
Copy link
Contributor

@ollieread, then it would only work in 1 direction, from a mongo-model to original model. But not the other way around.

@ollieread
Copy link

@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?

@jenssegers
Copy link
Contributor

Sure, go ahead :)

@ollieread
Copy link

Changes

  • Jenssegers\Mongodb\Model will now return MySQL relations if the Model in question is an instance of Illuminate\Database\Eloquent\Model
  • Added Jenssegers\Mongodb\CrossModel so that MySQL models can have MongoDB relations and MySQL relations.

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

@programming-kid
Copy link

can you please show your relations in your Models
more details about your test dbs setup would be helpful

@programming-kid
Copy link

i got belongsTo relation working but could not get hasMany relation working

here is my test case

User Model
User.php - Mysql

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
Project.php - Mongo

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
please help

@ollieread
Copy link

Your model that users MySQL, the User model, are you extending ModelCross?

Here's my test code: https://gist.github.com/ollieread/8020640
Here's the result: http://laravel.ollieread.com/mongo

@programming-kid
Copy link

Yep that works like charm thanks

@ollieread
Copy link

Excellent :)

Just waiting for the pull request to be accepted :D

@jenssegers
Copy link
Contributor

Need to write unit tests before I can merge this. Will do as soon as I find the time.

@ollieread
Copy link

If I get chance I'll have a stab at the unit tests.

@programming-kid
Copy link

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

@NandoSantana
Copy link

https://gist.github.com/ollieread/

can you repost links, i have the same situation.

@mongodb mongodb locked as resolved and limited conversation to collaborators Oct 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants