Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Strange many many problem
  • I am having a very unreal many to many problem here. I have two ORM models setup and many to many relation between two of thems: Category and Location model. The setup like this:
    // Location model
    protected static $_many_many = array(
         'categories' => array(
             'key_from' => 'id',
             'key_through_from' => 'location_id',
             'table_through' => 'categories_locations',
             'key_through_to' => 'category_id',
             'model_to' => 'Model_Category',
             'key_to' => 'id',
             'cascade_save' => true,
             'cascade_delete' => true,
         )     
     ); 
    
    
    // Category model  
    protected static $_many_many = array(
         'locations' => array(
             'key_from' => 'id',
             'key_through_from' => 'category_id', 
             'table_through' => 'categories_locations', 
             'key_through_to' => 'location_id', 
             'model_to' => 'Model_Location',
             'key_to' => 'id',
             'cascade_save' => true,
             'cascade_delete' => true,
         ),
     ); 
    

    I am now facing really strange behavior. For a particular location record count says only 1, even though I have many relations saved in many-many pivot table. Example:
    // actual values in categories_locations
    category_id  location_id
    27           44
    55           44
    56           44
    58           44
    25           5
    27           5
    26           5
    
    $location = Model_Location::find(44);
    echo count($location->categories); // will echo 1?!? WTF?
    
    $location2 = Model_Location::find(5);
    echo count($location2->categories); // will echo 3!!! 
    
    

    The really strange thing is I am seeing this only with some locations. I am really busting my head over this but makes no sense whatsoever and why it would work normally with some location and have problems with another?
  • Ok looks like a problem with loading. Lazy loading wont work for some reason here? $location = Model_Location::find(40);
    echo count($location->categories); will output 1 but if I use $location = Model_Location::find()->related('categories')->where('id', 40)->get_one();
    echo count($location->categories); will output 4 Why do I have such a behavior. It's not like I'm doing it for the firs time!?
  • Which version of FuelPHP? I think I saw an issue about this some time ago, and I know that one is fixed in 1.3.
  • I'm on 1.3 develop branch? I tried everything and I can get it work always if I use eager loading but does not work with lazy loading... It's strange because I use it in my other apps with same setup and it works?!
  • I switched to 1.3/master and 1.4/develop and I am seeing the same issue with lazy loading on these two versions as well. Am I really the first one with this problem?
  • I'm using ORM in development on a daily basis (I develop on 1.4/develop), and so far I haven't seen this behaviour. Lazy loading is build around the fact that if the property doesn't exist, __get() is called, which will fire the query and returns the results if needed. I've noticed that not all interactions with an object property will trigger the call to __get(), and also that this behaviour differs between PHP versions. If you enable the profiler, can you see the query to get the related records? If so it's not related to this, but it's a logic error.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion