fuel / orm

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

creating new relation models, which should be same, with ->from_cache(false) #441

Closed ysshir closed 3 years ago

ysshir commented 3 years ago

Thank you for fixing #435 and #438.

But there is another problem. https://github.com/fuel/orm/pull/435#issuecomment-689612208

tables..

parent
id name
1 parent1
child
id parent_id name
1 1 child1
grand_child
id child_id name
1 1 grand_child1
2 1 grand_child2
3 1 grand_child3

Models...

class Model_Parent extends Orm\Model {

    protected static $_table_name  = 'parent';
    protected static $_primary_key = ['id'];
    protected static $_properties  = [
        'id',
        'name',
    ];

    protected static $_has_one = [
        'child' => [
            'key_from' => 'id',
            'model_to' => 'Model_Child',
            'key_to'   => 'parent_id',
        ],
    ];
}
class Model_Child extends Orm\Model {

    protected static $_table_name  = 'child';
    protected static $_primary_key = ['id'];
    protected static $_properties  = [
        'id',
        'parent_id',
        'name',
    ];

    protected static $_belongs_to = [
        'parent' => [
            'key_from' => 'parent_id',
            'model_to' => 'Model_Parent',
            'key_to'   => 'id',
        ],
    ];

    protected static $_has_many = [
        'grand_children' => [
            'key_from' => 'id',
            'model_to' => 'Model_GrandChild',
            'key_to'   => 'child_id',
        ],
    ];
}
class Model_GrandChild extends Orm\Model {

    protected static $_table_name  = 'grand_child';
    protected static $_primary_key = ['id'];
    protected static $_properties  = [
        'id',
        'child_id',
        'name',
    ];

    protected static $_belongs_to = [
        'child' => [
            'key_from' => 'child_id',
            'model_to' => 'Model_Child',
            'key_to'   => 'id',
        ],
    ];
}

problems

$grand_children = \Model_GrandChild::query()
                                   ->related(['child', 'child.parent'])
                                   ->from_cache(false)
                                   ->get();

if ($grand_children[1]->child !== $grand_children[2]->child) {
    echo 'children instances should be same?';
}
WanWizard commented 3 years ago

Why?

You specify explicitly that the cache should not be checked, which causes a new object to be created for every result.

ysshir commented 3 years ago

This could be my misunderstanding. I just though I could get the result from DB for the query, even I already loaded one of the model. and the result,$grand_children[1]->child and $grand_children[2]->child are not same instance, is very strange for me.

I mean ->from_cache(false) is for the query, not for entire model creating method. that's why I forged the Model in the process_row method.

still I feel little bit kind of uncomfortable thing, But yes, now I get it.

sorry for bother you. thanks.

WanWizard commented 3 years ago

I understand your reasoning, but to make this work, from_array() should ignore the cache on creation of the first child object, but use the cache on subsequent child objects. Which means it would need to track all object creation, which would become quite complex very fast...