Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Help pls with Many to Many relations
  • SOLVED!!!
    I have three tables.
    Table Products
    'Size' with all sizes (M, S, XL, XXL, and other sizes)
    And Table sizesinproduction with relation Products <= sizesinproduction <= size
    I want to make ORM query to extract some products with size = 'M' code sql => http://pastecode.com/2V4
    code in model => http://pastecode.com/2V5
    Query in controller =>
    $data = Model_Products::find('all',array(
                                                  'related'=>array(
                                                      'sizeinproduction'=>array(
                                                          'related'=>array(
                                                              'size'=>array(
                                                                  'where'=>array(
                                                                      'sizename'=>'M'
                                                                  )
                                                              )
                                                          )
                                                      )
                                                  )
                                               ));
    

    I got: Fuel\Core\Database_Exception [ 1054 ]: Unknown column 't2_through.sizeid' in 'field list' [ SELECT `t0`.`prodid` AS `t0_c0`, `t0`.`catid` AS `t0_c1`, `t0`.`productname` AS `t0_c2`, `t0`.`description` AS `t0_c3`, `t0`.`quantity` AS `t0_c4`, `t0`.`photo` AS `t0_c5`, `t0`.`price` AS `t0_c6`, `t1`.`prodidsizes` AS `t1_c0`, `t1`.`whatsizes` AS `t1_c1`, `t2_through`.`whatsizes`, `t2_through`.`sizeid`, `t2`.`sizeid` AS `t2_c0`, `t2`.`sizename` AS `t2_c1` FROM `products` AS `t0` LEFT JOIN `sizeinproduction` AS `t1` ON (`t0`.`prodid` = `t1`.`prodidsizes`) LEFT JOIN `sizeinproduction` AS `t2_through` ON (`t1`.`prodidsizes` = `t2_through`.`sizeid`) LEFT JOIN `size` AS `t2` ON (`t2_through`.`whatsizes` = `t2`.`prodid`) WHERE `t2`.`sizename` = 'M' ]
  • With SQL syntax:
    Select * from products left join sizeinproduction
    ON (sizeinproduction.prodidsizes = products.prodid )
    left join size ON ( size.sizeid = sizeinproduction.whatsizes )
    where sizename = 'M' Very simply, but how I can make it with ORM?
    :)
  • I found my problem.
    It was here:
    protected static $_many_many = array(
            'size' => array(
                'key_from' => 'whatsizes',
                'key_through_from' => 'sizeid', // column 1 from the table in between, should match a posts.id
                'table_through' => 'size', // both models plural without prefix in alphabetical order
                'key_through_to' => 'sizeid', // column 2 from the table in between, should match a users.id
                'model_to' => 'Model_Size',
                'key_to' => 'sizeid',
                'cascade_save' => true,
                'cascade_delete' => false,
            )
        );
    
    BUT! I don't understand why it works. :) Will be very very helpful if someone can write article in docs how use ORM with examples.
  • Jelmer Schreuder wrote on Friday 2nd of September 2011:

    I know. But in docs no much examples.

Howdy, Stranger!

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

In this Discussion