fuel / orm

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

[ORM bug dev-1.9]Orm fetches wrong data when I use multiple related. #445

Closed allgrowsm closed 3 years ago

allgrowsm commented 3 years ago

Hello,

I found bug like ORM fetching wrong data.

I have following tables and ORM relation is has_many from users to user_A and user_B.

-users
 id

-user_A
 id
 user_id

-user_B
 id
 user_id

I also have following model. This is just an example of model.

Model_User extends Orm\Model {
  protected static $_has_many = array(
      'user_a' => array(
        'key_from' => 'id',
        'model_to' => 'Model_User_A',
        'key_to' => 'user_id',
        'cascade_save' => true,
        'cascade_delete' => false,
    ),
      'user_b' => array(
        'key_from' => 'id',
        'model_to' => 'Model_User_B',
        'key_to' => 'user_id',
        'cascade_save' => true,
        'cascade_delete' => false,
    ),
 )
 public static function gerUserRelatedData()
 {
  return self::query()
    ->related('user_a',array('join_type'=>'inner'))
    ->related('user_b',array('join_type'=>'inner'))->get(); 
 }
 public static function gerUserRelatedOne()
 {
  return self::query()
    ->related('user_a',array('join_type'=>'inner'))->get(); 
 }
}  

// This fetches wrong related data.
$model =Model_User::gerUserRelatedData();
foreach($model as $m)
{
 $a = array()
 foreach($m->user_a as $user_a)
 {
   $a[] = $user_a;
 }
 $b = array();
 foreach($m->user_b as $user_b)
 {
   $b[] = $user_b;
 }
  print_r($a);
  print_r($b);
}

// This fetches correct related data.
$model =Model_User::gerUserRelatedOne();
foreach($model as $m)
{
 $a = array()
 foreach($m->user_a as $user_a)
 {
   $a[] = $user_a;
 }
 $b = array();
 foreach($m->user_b as $user_b)
 {
   $b[] = $user_b;
 }
  print_r($a);
  print_r($b);
}

When you execute above source code, you can see gerUserRelatedData() fetches wrong related data. On the other hand, gerUserRelatedOne() fetches correct data somehow.

Can you please take a look at orm?

Best regards,

WanWizard commented 3 years ago

Have you checkted the generated SQL using the debugger, and run that SQL directly? Does that produce the data you expect?

It would be interesting to know if the problem is in the query generator, or in the hydration of the result.

allgrowsm commented 3 years ago

@WanWizard

Thank you for reply :) The generated SQL query is okay. It joins the correct tables.

It seems like all loop has 1st loop data like following.

For example....

■1st loop result ABC 123 ■2nd loop result EFG 456

ABC 123 ■3d loop rsult HIJK 78910

EEEE 2929

ABC 123

I am talking about following source code.

// This fetches wrong related data.
$model =Model_User::gerUserRelatedData();
foreach($model as $m)
{
 $a = array()
 foreach($m->user_a as $user_a)
 {
   $a[] = $user_a;
 }
 $b = array();
 foreach($m->user_b as $user_b)
 {
   $b[] = $user_b;
 }
  print_r($a);
  print_r($b);
}
allgrowsm commented 3 years ago

By the way, I already updated to the latest fuel as well.

WanWizard commented 3 years ago

The ORM result hydration has been refactored, could you check if this has also solved your problem?

allgrowsm commented 3 years ago

@WanWizard Thank you very much for fixing this issue. I always appreviate it. I will check it out.

allgrowsm commented 3 years ago

@WanWizard Sorry for late response. I have checked today and now it is working correctly. Thank you very much for reafactoring. I really appreciate it.

allgrowsm commented 3 years ago

Now I close this issue.