1 // will use find_by_pk and throw a RecordNotFound exception.
2 Unit::find($unit_id);
3 // will return an array, always.
4 Unit::all(array('conditions' => array('id' => $unit_id), 'limit' => 1));
5
6 // You can catch the exception
7 try {
8 Unit::find($unit_id);
9 } catch (RecordNotFound $e) {
10 // do something
11 }
12 // Or use the find_by_id instead
13 Unit::find_by_id($unit_id);
Thanks Yoan B
You have answered my questions and left me with a few more... :-)
I have tried find_by_id() and it works, ie. it doesn't crash. Why is that? Your link to find_by_pk() indicates that it would throw the same exception as find(). Is find_by_id() different to find_by_pk()?
Further, I tried to catch a RecordNotFound exception with both methods (when querying a non-existent record) but it doesn't seem to work:
find() simply crashes. I have installed firebug and firephp, now, and all I get is a blank browser and a "NetworkError: 500 Internal Server Error" message.
find_by_id() doesn't do anything, ie. no error message and it renders the rest of the page with my $query output variable simply not existing. But at least I can test this by checking
if ($query) ...
Any more light on this?
Thanks!!
You really should activate the errors (at best, in your php.ini):
1 error_reporting(-1);
2 ini_set('display_errors', 'on');
And that exception doesn't live in the global scope but in ActiveRecord's. You can either add use ActiveRecord\RecordNotFound
or use the whole path including its namespace.
1 try {
2 Unit::find(1);
3 // is a shortcut for this:
4 Unit::find_by_pk(1);
5 } catch (\ActiveRecord\RecordNotFound $e) {
6 // do something
7 }
8
9 Unit::find_by_id(1);
10 // is a shortcut for this:
11 Unit::find('first', array('conditions' => array('id' => 1)));
Ah good. I'll check that a soon as I am back at my desk. Thanks for your help!
(1-4/4)
Subject: difference between find() and all() and debugging question
Hi everyone
I have a newbie question/problem and hope someone can help me here:
I am using php activerecord in a Codeigniter project (PHP version 5.3.6).
My MySQL table ('units') has an 'id' field and several others ('name', 'level', 'credits').
In my Controller I have a function test() that receives a unit_id parameter
(I am using a customised controller so I don't need to call a particular view.)
It looks like this:
public function test($unit_id) {
$query1 = Unit::all(array('conditions' => array('id = ?', $unit_id)));
$this->view_data['query1'] = $query1;
Both queries return data as intended:
$query1 returns an array of objects (actually just one because 'id' is primary key)
$query2 returns an object
However, if I ask for an 'id' that doesn't exist, $query1 returns an empty array, while $query2 breaks my application (no views are sent to the browser).
So my questions are:
What does find() return when no matching record is found?
How can I test the result? (eg. if(count($query1) == 0)
How can I avoid crashing my application when querying with find()?
I have been looking through the wiki/docs but struggle to find the answers.
I am fairly new to PHP and totally new to CI and activerecord, so I am a bit lost in the woods.
Thank you for your help and big thank you for developing this awesome tool.
Cheers, S