Ryan Gantt Fri Jan 28 19:52:22 -0500 2011

Subject: Troubles with finders

The following code:

$user = User::find( 'first', array( 'conditions' => array( 'email=?', $this->params->login_email ) ) );

Does not throw an exception and also does not return any results. There is no record in the database that matches the conditions. How am I supposed to know if I'm getting anything back from ::find?!


forrest lyman Fri Jan 28 22:20:51 -0500 2011

why would it throw an exception if your result set is empty?

Ryan Gantt Fri Jan 28 23:31:17 -0500 2011

User::find( $id ) throws an exception if the row with primary key == $id doesn't exist, so why should the functionality be different for equally-strong searches on another field? If I switched "email" to the primary key, I would get an exception.

In fact, if I change that call in any way to break it:

$user = User::find( 'first', array( 'conditions' => array( 'emails=?', $this->params->login_email ) ) );
$user = User::find( 'first', array( 'condition' => array( 'email=?', $this->params->login_email ) ) );
$user = User::find( 'firsts', array( 'conditions' => array( 'email=?', $this->params->login_email ) ) );

I get a meaningful exception, based on which I can control execution flow. Running a query that results in an empty set is--by definition--an exceptional event, so I don't understand why it wouldn't also throw an exception.

My experience with ActiveRecord in rails is limited; is this how the functionality works there? Maybe my complaint is just too uninformed :)

Kien La Fri Feb 04 17:46:24 -0500 2011

We've followed rails' convention where find('pk') will throw an exception if not found. All other find methods return an empty array/null if nothing is found.

(1-3/3)