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

Many-to-many relationships? #17

Closed
ssfinney opened this issue Jul 30, 2014 · 17 comments
Closed

Many-to-many relationships? #17

ssfinney opened this issue Jul 30, 2014 · 17 comments

Comments

@ssfinney
Copy link

I know I can do the below example for a one-to-many relationship in TestDummy.

Post:
  title: Hello World $string
  body: $text
  published_at: $date
  author_id:
    type: Author

Author:
    name: John Doe $integer

But how do I do a many-to-many relationship? This requires defining a pivot table, which I can't define in the fixtures.yml file.

@MercerAsian
Copy link

Is there any answer/update to this? I haven't been able to find a solution.

@quickliketurtle
Copy link

What's an example of what you are trying to do?
Using the Post example, something like Posts and Tags?

I was playing around with this, What syntax would you want to see?
I'm thinking something like this:

Post:
  title: Hello World $string
  body: $text
  published_at: $date
  author_id:
    type: Author
  pivot:
    type: Tag

@ssfinney
Copy link
Author

Yes, that's exactly what I want to do. Posts and Tags is a great example. For another example, think of Users and Roles.

I love the syntax!

👍

@aaronbullard
Copy link

Was curious if this is something under development? Or is there another solution that I'm missing?

@MercerAsian
Copy link

@aaronbullard You can still just create both models and then use Eloquent's attach() method.

@beedge72
Copy link

This seems like a fairly neccessary feature. Is there any update?

@quickliketurtle
Copy link

Hi All, I started working on this last night. See https://github.com/quickliketurtle/TestDummy/tree/pivot

It's functional with Factory::create().

With build() it doesn't look like relationships were utilized so this change will not effect the build() method.

I still need to add some tests for this, need some more time to figure out the best way to test it.

Still a work in progress, but suggestions / feedback are welcome.

@quickliketurtle
Copy link

UPDATE: Looks like the comment i replied to was deleted. Anyway i'll need to account for full namespace in fixtures.yaml file.


May be just the pasted code but the spacing on the pivot property look off.
i was seeing that error before writing the code to look for the pivot key
word.

However i did not test with namespaces so the type: \Role will error since
it is using that to build the save method. I'll add some code to account
for that.

In your example does the Role model site at the root of the namespace? or
is it in the Excel\Users namespace?

On Sun, Sep 28, 2014 at 2:54 AM, Kevin Bradshaw notifications@github.com
wrote:

@quickliketurtle https://github.com/quickliketurtle I have the
following relationship
Many Users have Many Roles
which Ive represented as follows:

Excel\Users\User:
username: $string$integer
email: $string$integer@example.com
password: $string
timezone: $string
broker_id: $integer
created_at: $date
updated_at: $date
pivot:
type: \Role

\Role:
name: $string
created_at: $date
updated_at: $date

does that look correct? When I run my functional test based on this
relationship I get an error:

Couldn't sign in as member :
ErrorException: preg_replace(): Parameter mismatch, pattern is a string while replacement is an array


Reply to this email directly or view it on GitHub
#17 (comment).

@beedge72
Copy link

@quickliketurtle Yeah, Sorry I deleted my post when I realised the reason for the error I was receiving was not related to your branch (I was still accidentally using Master branch)
Im pretty sure the spacing on my .yml file is ok though (just tabbed the type value underneath pivot). but I agree, any solution would need to accommodate namespacing to keep it consistent with how it currently works

@quickliketurtle
Copy link

Made a modification to account for namespaces with the pivot type. @beedge72 Let me know if this works with your current example.
Still need some tests...

@yecine06
Copy link

yecine06 commented Jan 4, 2015

with the relase of the version 2 is there a new way to handle pivots ?

@abstractFlo
Copy link

@yecine06

+1 for that question

@yecine06
Copy link

@JeffreyWay how to do suggest handling this ?

@andrewmclagan
Copy link

Not possible to create a pivot factory? Factory::create('PivotTable')

@yecine06
Copy link

@jeffreywy You closed thé issue but You did'nt answer it

How should we handle this

@andrewmclagan
Copy link

I have not tried it... but like I suggested:

Many-to-many relationships are simply defined by an intermediate table, called a pivot table.

This table can pretty much be accessed like any regular model can be in Laravel. So I'm assuming you can do something like Factory::create('PivotTable')

Just do a little bit of investigating from this point. Im sure its possible.

@philipmclifton
Copy link

I came up with a good solution to this but I had to modify TestDummy.

I added the ability to provide a second closure to a factory, this closure is run after a model is created and the model is passed into it.

$factory('App\Models\Application', 'base_app', function ($faker) {
    return [
        'id'        => 1,
        'name'      => 'name',
        'slug'      => 'slug',
        'website'   => $faker->url,
        'author_id' => 'factory:App\Models\User'
    ];
}, function($application){
    \Laracasts\TestDummy\Factory::create('App\Models\ApplicationUser', [
        'author_id'        => $application->author_id,
        'application_id' => $application->getKey(),
    ]);
});

In the example you can see that once its built the application and the user (author) it then runs the ApplicationUser model and creates the pivot between the tables.

This solves the pivot issue and many others.

https://github.com/philipmclifton/TestDummy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants