fuel / orm

Fuel PHP Framework - Fuel v1.x ORM
http://fuelphp.com/docs/packages/orm/intro.html
151 stars 95 forks source link

relations multiple empty bug #439

Closed hotlabs closed 3 years ago

hotlabs commented 4 years ago

Im have many to many relation; and i want include 2 level relations

protected static $_many_many = array('genres')

Model_***::query() ->related('genres') ->related('genres.translation') ->get();

SQL build - Correct but if there is no connection yet, then I get an empty object

array(1) { [""]=> object(Model_Genre)

this empty result need fix -

hotlabs commented 4 years ago
            // skip empty results
            if ($isnull)
            {
                if( ! $model['relation']) 
                {
                    continue;
                }

change to // skip empty results if ($isnull) { continue;

query.php ~ 158 - 161 lines

WanWizard commented 4 years ago

Is this tested with the current HEAD of the 1.9/dev branch? There were quite a few changes last week.

WanWizard commented 4 years ago

@AdamSGit Mind looking at this? It is related to the code you added last week...

AdamSGit commented 4 years ago

Sure thing.

hotlabs commented 4 years ago

bag in "fuel/orm": "dev-1.9/develop", this need fix this error on nested links ->related('genres') ->related('genres.translation') - two level relation this return empty object parent relation - [""]=> object(Model_Genre)

WanWizard commented 4 years ago

There are more issues with the code changes.

I've updated one of the apps we have in maintenance tonight, and it now crashes on the lastest ORM updates. It fetches a record with a few relations, 2 belongs_to and 1 has_many.

For some reason, 1 of the 2 belongs_to are correctly assigned, the second one ends up as an EAV value (so it is not detected as being a relation), which causes a crash on save, as EAV values must be scalar, and not an object.

I'll try to find time to dive into it tomorrow.

hotlabs commented 4 years ago

and this easy code not work $post = Model_Post::query()->related('articles') ->where('articles.published', 1) ->get_one();

code from docs

return null

AdamSGit commented 4 years ago

I'll have a look at it in the weekend as well.

@hotlabs : What do you expect with this code ? Is your database populated with published articles that have a relation with post ?

hotlabs commented 4 years ago

yes table full

WanWizard commented 4 years ago

Where these objects loaded before, in a different query? If so, I just pushed a fix for that.

WanWizard commented 4 years ago

Just did

    $session = Model\Session::query()->related('registration')
        ->where('registration.id', '=', 92)
        ->get_one();

In an app I was working on, and I get 1 session object (the first) back, and one related registration object with ID 92. If I remove the where, I get the same session object, but then with all 12 related registration objects.

So I'd say "works as advertised"?

WanWizard commented 4 years ago

NB: Pay attention to the question you ask. get_one() means give me the first session, not "give me the first record that matches", the ORM is not a simple query builder.

So if I do:

        $session = Model\Session::query()->related('registration')
            ->where('registration.id', '=', 4329873623978462394)
            ->get_one();

i get null back, since the first session doesn't have a registration with that id value.

WanWizard commented 4 years ago

NB 2: This would give you what you want:

        $session = Model\Session::query()->related('registration')
            ->where('registration.id', '=', 262144)
            ->rows_limit(1)
            ->get_one();

which returns me session object with id 38448, and one related registration object, with id 262144.

rows_limit() applies the limit to the entire result, where limit() (which is implied by get_one() only applies to the parent record.

WanWizard commented 4 years ago

I think I misunderstood your report. Just pushed a fix for this issue.

Could you verify and confirm the fix?

AdamSGit commented 4 years ago

Hadn't the time to look at it in the past few days.

What is the current status of the issue ? And about the change in general. Are some issues left ?

WanWizard commented 4 years ago

I think this one is fixed, just waiting for confirmation.

WanWizard commented 3 years ago

@hotlabs any feedback on the latest updates?

WanWizard commented 3 years ago

Closed due to no feedback.