Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
I have an issue with ORM, KIndly Help
  • This is my controller students/index bin http://bin.fuelphp.com/snippet/view/L4
    i am using simple table with filter by forms and select inputs, so GET variable will handle filter with SQL directly, Working fine,

    Issues,

    1) This Code shows 9 Queries to Database when i put $query->get_one(). if i changed to $query->get(), it shows 3 queries. it is ok. but i need to have get_one, as it is specific to 1 institution, but in each institution have many students and stds.
    2) when i provide a get value which has no result, then i get the main query empty?
    although i call all filter to my related "students" you see, instead of getting empty values in $institution->students , i getting $institution empty.

    kindly check and do a help
  • One get() or one get_one() == one query.

    I don't see anything wrong with you code, so I suggest you need to debug to find out why a query runs multiple times.

    A standard related query is an inner join (a left join), not an outer join, so all joined tables must have a match to be included in the result. You can pass an alternative "join_type" in the relation conditions:

    ... ->related('table', array('join_type' => 'outer'))-> ...
  • Dear,

    I have found the issue?
    i am calling a belongto relation from a view, it calls the query several times as loops continue. Then how can i chain relation() query.

    $a->related(array(b->related(d),c);
  • dear,

    it is worked,

    i have used this

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

    But what abut the issue (2)

    When one of my filter doesn't have any students, then i getting whole object empty.?

    How it is happening, Eg. If i dont have any student with gender=female. then i getting whole $query->get_one(); empty.  why this happening, i am filtering it's relation, atleast i need the result of object.
  • If you want to have nulls in the result, you need to change the join type of the relation.

    You can do that in the model, in the relation definition, if you always want this behavior, or you can do that in the related() call, like I described above.

    p.s. queries, or any other logic, in a view is a bad idea. The logic should be in your model or optionally in the controller, and only data should be passed to a view.
  • i am not using queries in view
  • HarroHarro
    Accepted Answer
    Then I have misunderstood you, and we have a language issue.
  • Exctly the join type 'left inner' shoud work, but it is not yer worked, What shoudl i do, still i get parent null if child is null
  • Enable the profiler in your config file, and enable database profiling in your db config.

    Run your queries, then check the profiler output, it shows you the exact SQL generated, and the result in number of rows. As a double check, you could copy the generated SQL into something like PhpMySQLadmin, and see if the raw query produces any result.

    If you get zero rows, your query is incorrect, if you get a result, it might be an issue with ORM hydration.
  • Dear,

    I have enabled profiler already. Found all Queries. I copies the exact related query, and copied into phpmyadmin, it returns null, so i think the issue.

    1) related to the way i enter relation.
    2)Orm generating Queries.

    My Portion of controller is given http://bin.fuelphp.com/snippet/view/Mc
    I getting this error, http://bin.fuelphp.com/snippet/view/Me

    Exactly What is Error, According to my end, i cant find any error, It is very important for me, if any relation return null, i getting the base model Null, It is not acceptable for me.

    A kind help appreciated


  • I have found some info.
    LEFT join + Where = INNER join.

    logically it is currect, The where will execute after left joining completed,

    So i getting null if child is null after where. How should i fix that? can you recommend something.

    in StackOverflow i found that using AND is recommended to get this working, how i implement AND instead of Where in ORM?
  • You have a where on a column of the child in the relation?

    Ah, in that case the result you get is logical, even of you change the join, the where will not match, so you wont get the result.

    I have to say I don't get your query at all. What exactly do you want the output to be? All your where() clauses seem to be on student(), yet you query the institution model?

    Isn't it logical that you get no institution object back if the requested student does not exist?
  • in students page, i would like to get all students of a certain institution, so i call the specific institution and all students related to it, so the page is of students, that the page shows only students, so institutions information too. if i join both, i will have to do only one query, which include all information that i need for that page,

    but i have filter facility in page, by select input, when i do so, i getting null on institution if no students exist, institution always exist, bit null cause worst. whole page crashes, how i fix that, i want to do filter, i sure it is caused by LEFT + Where = Inner problem, even in phpmyadmin, it is null. kindly help
  • HarroHarro
    Accepted Answer
    Isn't it more logical to build the query different then? If you want Students, why not query students?

    $students = Model_Students::query()
        ->related('institution')
        ->where('institution.id', '=', $institution);

    // add your where() filters

    $students = $students->get();
  • Exactly, i think you misunderstood my question, Your last answer will work, but not 100%.

    what i want is , if child return null, i want it's parent should not return null.

    institution always there at the ID. but sometime students not at filter. (those student who has blood group AB+ve). if so, i want the institution object, and count($institution->students) = 0.
  • Ok, I understand.

    I don't have an immediate answer for you, Steve is the ORM man. You might want to ping him on IRC, he doesn't visit the forum. His nic is uru.

    I will see if I can find the time this weekend to setup some tests.
  • Yes, Very Thanks, How i access IRC?
  • Via the web, using https://webchat.freenode.net/

    Or via an IRC client on your PC. For linux, Pidgin is the most popular. Freenode has a tutorial online: http://www.irchelp.org/irchelp/irctutorial.html

    The channel name is: #fuelphp
  • I can not reproduce your problem.

    I made a complicated query, and made sure no child matched the where clauses:

    $query = Model_Parent::query()
            ->where('column', 'value')
            ->and_where_open()
                    ->where('id', 1)
                    ->or_where('id', 2)
                    ->or_where('id', 3)
                            ->or_where_open()
                                    ->where('id', 99)
                            ->or_where_close()
                            ->and_where_open()
                                    ->or_where('child.id', 99)
                            ->and_where_close()
                    ->and_where_close()
            ->related('child')
            ->offset(0)
            ->limit(5);

    print((string) $query->get_query());
    var_dump($query->get());

    This will generate this query (the output of the print):

    SELECT `t0`.`id` AS `t0_c0`, `t0`.`name` AS `t0_c1`, `t0`.`column` AS `t0_c2`, `t1`.`id` AS `t1_c0`, `t1`.`parent_id` AS `t1_c1`, `t1`.`date` AS `t1_c2`, `t1`.`child` AS `t1_c3` FROM (SELECT `t0`.`id`, `t0`.`name`, `t0`.`column` FROM `parent` AS `t0` WHERE `t0`.`column` = 'value' LIMIT 5 OFFSET 0) AS `t0` LEFT JOIN `child` AS `t1` ON (`t0`.`id` = `t1`.`parent_id`) WHERE (`t0`.`id` = 1 OR `t0`.`id` = 2 OR `t0`.`id` = 3 OR (`t0`.`id` = 99) AND (`t1`.`id` = 99))

    and If you paste this into PHPMySQLAdmin, the result is:

    t0_c0     t0_c1     t0_c2     t1_c0     t1_c1     t1_c2     t1_c3
    3         Name      value     NULL      NULL      NULL      NULL

    Which is the parent data with zero child records. The next line dumps the result of the query, which is one parent object with the data from the parent found, and zero child records.
  • i will check and inform you soon, can you inform me why we need two or more primary key, what is the benefit of it. rather than single primary key.

    A kind help appreciated. with an example is perfect
  • I don't understand your question? Both "parent" and "child" tables in my test have a single primary key, "id", and a has_many relation on "parent_id".
  • Sorry, This is not related to this, this is a separate question

    can you inform me why we need two or more primary key, what is the benefit of it. rather than single primary key.

    A kind help appreciated. with an example is perfect
  • You don't need anything. What your primary key is follows from your database design, and the design principles you use.

    If you use non-contextual PK's, like Fuel's default 'id' which is an auto-increment column, it's value has no relation to the rest of the row (although sometimes it is used as such, for example when it doubles as user id). So there is never a need for compound keys, since your auto-increment key is always unique.

    But if you use contextual keys, i.e. columns that contain data, sometimes there is no single column that is guaranteed to be always unique.

    See https://en.wikipedia.org/wiki/Compound_key
  • I checked the query you made before 2 post.. Not worked as expected. Why you can't make an example with simple where clause. Without using or_where and Or_where_open

    I need a orm query design. Which is in chained type for filtering purpose. And using where on child. If child count is 0 or null. I want my parent object completely. That mean the where must be applied on the child. Not the whole result.
    $query = Where('child's.id',\Input::get('id')); yhis one gives me whole object null.if child is null.



    I sure. Now you got it.
  • You can just remove the entire block between and_open() and and_close(), and the query remains the same.

    If you look at what the raw query returns, the result is logical.

    If there is no child matching, the result row contains NULL for all child columns. If you then add a WHERE clause saying "child.id = 1", it clearly doesn't match the value NULL, so that record is filtered.

    Since there are no child records at all, all records are filtered, the result set is empty, so there is parent to hydrate from it as well.

    If you accept NULL as a value, your where clause must show so:

    if ($id = \Input::get('id'))
    {
        $query->where_open()
            ->where('child.id', '=', $id)
            ->or_where('child.id', '=', NULL)
        ->where_close();
    }

    It is good practice to, if you are in a s situation with complex queries, to write your query in PHPMyAdmin in SQL first to get the proper result, and then translate it into ORM method calls.

    That will also force you to learn some SQL, since I have a feeling that lack of understanding how that exactly works is the cause of the issue here...
  • You are great

Howdy, Stranger!

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

In this Discussion