Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Pagination and "has_many" releations.
  • I have 2 tables: table talk_topic "has many" talk_posts. Please help me to get from db limit and offset talk_posts, but from Model_Talk_Topic. I need something like this:
    $data['talk_topic'] =  Model_Talk_Topic::find($id, array(
    'related' =>array('talk_posts'=>array('limit' =>1),
    )
    );
    i know that i can  Model_Post_Topic, but i need get result, working with Model_Talk_Topic. All releations in models  are registered correctly,100%.
  • If you want to generate a table view from a query that has related tables, you should use 'rows_limit' and 'rows_offset'.

    By default ORM will limit on the parent only, not on the total result.
  • With this:
    $data['talk_topic'] =  Model_Talk_Topic::find($id, array(
    'related' =>array('talk_posts'=>array('rows_limit' =>1),
    )
    )
    );

    I get topic with all posts, but i need to take only 2(not 10). Can you help with litle code? 
  • rows_limit doesn't go on the relation:

    $data['talk_topic'] =  Model_Talk_Topic::find($id, array(
         'related' =>array('talk_posts'), 'rows_limit' =>1)
    );

    I think. I never use Array notation, I use chaining:

    $data['talk_topic'] =  Model_Talk_Topic::query()
        ->where('id', '=', $id)
        ->related('
    talk_posts')
        ->rows_limit(2)
        ->get();

    will get you 2 objects, which might be the same topic, or two different topics in case the first had less then two posts...
  • Harro Verton, I am grateful to you for your help. Both of them works great.
  • You're welcome. Happy coding!

Howdy, Stranger!

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

In this Discussion