Michael D Mon Jun 04 03:36:59 -0400 2012

Subject: Fatal error if record not found

If I am submitting a id that does not exist in the database, php-acticerecord gets a fatal error.

example

$user = User::find(99);

99 doesnt exist and now I will get a fatal error. How do I do so that if the id doesn't exist, don't do anything? Because users can type what ever id they want in the uri (user/profile/{id}) and I don't want to show errors like that on the site for users.


Nanne Huiges Mon Jun 04 11:16:07 -0400 2012

You should either use something like `find_by_id` (which will not produce an error, but return false..) or you can catch the error (you could've found that by looking at the php manual about exceptions btw).

If you use option 1, you should also do something explicit when it returns false (like show a custom error) ofcourse

Michael D Mon Jun 04 11:37:41 -0400 2012

I still get error if I use find_by_id.

User::find_by_id($id);

C. A. Beninati Fri Jun 22 16:47:30 -0400 2012

Nanne Huiges wrote:

You should either use something like `find_by_id` (which will not produce an error, but return false..) or you can catch the error (you could've found that by looking at the php manual about exceptions btw).

If you use option 1, you should also do something explicit when it returns false (like show a custom error) ofcourse

Helped me, thanks.

(1-3/3)