Skip to content

HasMany relationship not working #68

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

Closed
amacgregor opened this issue Nov 19, 2013 · 4 comments
Closed

HasMany relationship not working #68

amacgregor opened this issue Nov 19, 2013 · 4 comments

Comments

@amacgregor
Copy link

I'm trying to get a one to many relationship working. I tried doing a couple of things:

$container->items()->save($entity) ; // This does absolutely nothing

$entity->container()->associate($container); // Partially worked by creating the parent_id but the parent model doesn't now about the children.

Please advice.

@jenssegers
Copy link
Contributor

I did not test the methods you are using. I tested relations with the more manual approach by passing id's: https://github.com/jenssegers/Laravel-MongoDB/blob/master/tests/RelationsTest.php

I will try to find out why items()->save(..) is not working. Do you have more documentation on this way of saving relations?

jenssegers added a commit that referenced this issue Nov 19, 2013
@jenssegers
Copy link
Contributor

I added your way of saving models to the tests and did not encounter any problems: https://github.com/jenssegers/Laravel-MongoDB/blob/master/tests/RelationsTest.php#L105

@amacgregor
Copy link
Author

This might a compatibility issue with this bundle https://github.com/creolab/laravel-modules?source=cc

My current code looks something like this

        $container = \App\Modules\Catalog\Models\Attribute\Container::find('528bed717c89f55850000029');
        echo get_class($container) . ' - ' . $container->entity_id . "<br/>";

        $items = $container->items();

        foreach($items as $item)
        {
            echo get_class($item) . ' - ' . $item->name . "<br/>";
        }

Container class

class Container extends Eloquent {

    protected $connection = 'mongodb';
    protected $collection = 'attribute_container';

    public function items()
    {
        return $this->hasMany('App\Modules\Catalog\Models\Attribute\Entity');
    }

} 

Entity class

class Entity extends Eloquent {
    protected $connection = 'mongodb';
    protected $collection = 'attribute_container_item';

    public function container()
    {
        return $this->belongsTo('App\Modules\Catalog\Models\Attribute\Container');
    }
} 

So I do see the container id being added to the entity model, but when I try to retrieve the items nothing comes back.

@amacgregor
Copy link
Author

Closing this ticket, I was incorrectly calling

$container->items();

This works:

$container->items;

mnphpexpert added a commit to mnphpexpert/laravel-mongodb that referenced this issue Sep 2, 2024
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

2 participants