Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Change Query After creating Query Object
  • I have defined a main query.

    $query = \Model_Institution::query()->where('id',$inst)->related(array('stds'=>array('related'=>array('products')),'students'=>array('related'=>array('products')),'products'=>array('related'=>array('students','stds'))));

    after that,
    if(\Input::get('std')){
       $query->where('students.stds.name',\Input::get('std'));
    }

    it gives null $query

    how i change query object after 1 st query? especially in case of filtering of relations resursively.
  • That code looks fine, I use that all the time. So the problem must be elsewhere.
  • $res = $query->get();
  • sorry, there is no complaint. i need a help.

    summary

    i have query with c is related to  b is related to a.

    i want to show b in the view.

    $result = $a->get_one();
    $result gone to view

    so with object result, i have a & b & c.
    a as per selection, b & c are in relation. understand, c is related to b, not to a.

    i want to show all b in a table of a in view. it is fine. $result->b  . ok

    i want to filter b with respect to c.

    example. a is school,  b is students,  c is their classes..

    how it is possible, that is exact question.
  • if ($res->b->c){
    ....
    }

    Or define your relation with inner join
  • Which is what I wrote in the other topic.
  • i given my query object, contain complex relations.

    $query = \Model_Institution::query()->where('id',$inst)->related(array('stds'=>array('related'=>array('products')),'students'=>array('related'=>array('products')),'products'=>array('related'=>array('students','stds'))));

    here.
    each Institution has many products
    each products have different stds.
    i want to filter it beased on selection

    if(\Input::get('stds')){
    $query->where('products.stds.name',\Input::get('std')); //Here is my problem. details below.
    }

    if user selected std-A. then how i filter the result.

    in view i used this.

    $institution->products // this used over a loop, working properly, but i cant filter it, filtering causes error, my $query->get_one() become null.


    an example is preferred, confused since a week.

    simple queries like
    $query->where('products.gender',\Input::get('gender')); is working, because the gender is a column in products table. bit i want to filter the products with std. which is another table, also $query already have the relation of the same.
  • HarroHarro
    Accepted Answer
    All data passed to a view is encoded, and that includes a query object. You should not have application logic in your views.

    Filtering is application logic, and should happen in your controller.

    ORM model objects are encoding aware, so you can pass those to a view without them being encoded. Their properties will encode on the fly when you access them.

    You keep on saying filtering is a problem, have you checked why it is a problem? Have to checked which SQL is generated when you add the additional where()? Use the profiler to do so.

    My guess: you can't use model aliases in dot-notation:

    $query->where('products.students.name',\Input::get('std')); // This should work fine
  • i have to ask you a duplicate question, i didnt understand the answer you said 2 days before.

    i have a model_institution

    $query = \Model_institution::query()->where('id',5)->related(array('students'=>array('where'=>array('name','myname'))));

    if the "myname" is not exist, according to my thinking, i should get a null students data and valid institution object, because institution with id=5 is exist. but now i getting empty institution object. why, i only filtered on relation.
  • No, as I wrote before, by default the ORM generates standard joins (which in the case of MySQL is an INNER join), which means related table entries MUST exist.

    If you want other join types, you have to define them either on the models' relation definition:

        'join_type' => 'left inner',

    or if you want to define it at runtime:

    ... ->related('modelname', array('join_type' => 'left inner'))-> ...

    See http://stackoverflow.com/questions/6294778/mysql-quick-breakdown-of-the-types-of-joins for some join info. You will see that a "LEFT INNER" join returns your desired result.
  • i have tried with all,, i getting my $results null.

    http://bin.fuelphp.com/snippet/view/L5

    this is my debiginfo in profiler of same query, where i see there are 1 rows in t0 returned, but i cant get it, i getting only null. kindly help this matter.
  • Ok, so the query is ok? If you paste this query in something like PHPMySQLAdmin, do you get the result you are after?

    If the query produces a result, but get() or get_one() returns null, you might have hit a bug in the ORM.

    Can you double-check that is the case, and if so, create an issue for this at http://github.com/fuel/orm/issues, documenting the exact problem?

    Then it can be addressed when we're back after the holidays.

Howdy, Stranger!

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

In this Discussion