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.
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:
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.
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.
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.
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.
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.
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
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.
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.
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.
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.
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:
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...