Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
ORM JOINS
  • I have been looking but i cant find any documentation on using the ORM for joins. I have a Model_Lists and a Model_Music and im trying to get them to join using the ORM.
    class Model_Music extends Orm\Model {
    
        protected static $_table_name = 'mini_lists_music';
        protected static $_primary_key = array('list_id');
        protected static $_has_one = array(
            'lists' => array(
                'model_to'  => 'Model_Lists',
                'key_from'  => 'list_id',
                'key_to'    => 'list_id',
            ),
        );
    
    }
    
    Model_Music::find('all');
    

    Doesn't do the join however, im just missing the right syntax for it.
  • It won't auto-include any relations, there's a couple of options to get them:
    // eager loading, using joins:
    $music = Model_Music::find('all', array('related' => array('lists')));
    // or
    $music = Model_Music::find()->related('lists')->get();
    
    // or use lazy loading, it won't use joins but query a relation once requested
    // first get a "music", 1 query without join
    $music = Model_Music::find('first');
    // now request the lists, which will do another query without join automaticly
    $lists = $music->lists;
    
  • Jelmer Schreuder wrote on Wednesday 6th of April 2011:
    It won't auto-include any relations, there's a couple of options to get them:
    // eager loading, using joins:
    $music = Model_Music::find('all', array('related' => array('lists')));
    // or
    $music = Model_Music::find()->related('lists')->get();
    
    // or use lazy loading, it won't use joins but query a relation once requested
    // first get a "music", 1 query without join
    $music = Model_Music::find('first');
    // now request the lists, which will do another query without join automaticly
    $lists = $music->lists;
    
    Thanks, this was exactly what i was looking for.

Howdy, Stranger!

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

In this Discussion