Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
search in model relation
  • hi
    i have models like :
    users belongs_to group (users.group = group.id)
    group has_many fields (fields.group = group.id)
    fields has_one val (val.fields = fields.id)
    ---
    i want search in val.value !
    please help
  • find something in query.php :

    public function relation .......

    and use it  :
    $data['records'] = Model_User::find('all',
                        array(
                            'related' => array('groups', 'groups.fields', 'groups.fields.val'),
                            'where' => array(array('groups.id', 1)),
                            'or_where' => array(array('groups.fields.val.value', 'LIKE', '%'.\Input::get('search').'%'))
                        )
                );

    but nothing back!!!!!
  • HarroHarro
    Accepted Answer
    I personally hate this syntax, so I can't comment on it, perhaps something is wrong.

    Enable the profiler in your app config.php, enable database profiling in your db.php, and check what query that code generates. Perhaps you can spot the problem?

    I would use:

    $data['records'] = Model_User::query()
        ->related('groups')->related( 'groups.fields')->related('groups.fields.val')
        ->where('group', '=', 1)
        ->where('groups.fields.val.value', 'like', '%'.\Input::get('search').'%')
        ->get();

    It should do exactly the same, but I find it easier to read...

    p.s. you used or_where, it that intended? Don't you want users with group 1 AND that search value?
  • hi,

    your code is very nice, i 'm new in fuelphp and write code by document in wrong way ;(

    yes i select users by group id and search in group dynamic field value or dynamic field default value (for select list or check boxes).


    thank you!




  • HarroHarro
    Accepted Answer
    It's not wrong, there are two ways of coding ORM queries, one with find() and array notation, one wiith method chaining.

    I personally find the second one easier to read, which helps if you are working in a team and not all code is written by you.
  • hi harro,(sorry for my bad english)
    i have problem with model relation field select.
    i search in model relation by your help and set selected field from main table (model),
    but orm select all field in relation.
    how can i tell orm not select field from model relation?
  • HarroHarro
    Accepted Answer
    Remember, an ORM is not a query builder, the ORM is an object representation of your database, and it should not contain partial information.

    Although the ORM supports the select() method, using it has a lot of side effects since it will generate incomplete objects. Since the ORM only creates one object for each record, no matter how many times you run a query, you will not be able to access columns that were not part of your initial select.

    So it is strongly advised not to do it.

Howdy, Stranger!

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

In this Discussion