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

Add support for OneToMany/ManyToOne/ManyToMany relationships #6

Merged
merged 1 commit into from
Jun 17, 2020

Conversation

kbond
Copy link
Member

@kbond kbond commented Jun 16, 2020

use function Zenstruck\Foundry\create;
use function Zenstruck\Foundry\factory;

// ManyToOne
create(Post::class, [
    'category' => $category, // $category is instance of Category
]);
create(Post::class, [
    // Proxy objects are converted to object before calling Post::setCategory()
    'category' => create(Category::class, ['name' => 'My Category']),
]);
create(Post::class, [
    // Factory objects are persisted before calling Post::setCategory()
    'category' => factory(Category::class, ['name' => 'My Category']),
]);

// OneToMany
create(Category::class, [
    'posts' => [
        $post, // $post is instance of Post, Category::addPost($post) will be called during instantiation

        // Proxy objects are converted to object before calling Category::addPost()
        create(Post::class, ['title' => 'Post B', 'body' => 'body']),

        // Factory objects are persisted before calling Category::addPost()
        factory(Post::class, ['title' => 'Post A', 'body' => 'body']),
    ],
]);

// ManyToMany
create(Post::class, [
    'tags' => [
        $tag, // $tag is instance of Tag, Post::addTag($tag) will be called during instantiation

        // Proxy objects are converted to object before calling Post::addTag()
        create(Tag::class, ['name' => 'My Tag']),

        // Factory objects are persisted before calling Post::addTag()
        factory(Tag::class, ['name' => 'My Tag']),
    ],
]);

@kbond kbond mentioned this pull request Jun 16, 2020
24 tasks
@weaverryan
Copy link
Contributor

Would the model factory usage look like (this includes #5) this?

// previously use CategoryFactory to create some categories

PostFactory::create([
    'categories' => CategoryFactory::randomSet(3)
]);

And if so, this wouldn't work before this PR because it would have tried setting Proxy objects? And this PR unwraps them?

@kbond
Copy link
Member Author

kbond commented Jun 16, 2020

Would the model factory usage look like (this includes #5) this?

Correct, and this:

PostFactory::createMany(4, [
    'categories' => CategoryFactory::randomSet(3)
]);

Would create 4 Posts with the same 3 categories. You would have to wrap the attributes in a closure to get a different set of categories for each of the 4 posts:

PostFactory::createMany(4, function () {
    return [
        'categories' => CategoryFactory::randomSet(3)
    ];
});

this wouldn't work before this PR because it would have tried setting Proxy objects?

Correct, only ManyToOne worked before this PR.

Base automatically changed from refactor-instantiator to refactor June 17, 2020 00:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants