j4mie / paris

A lightweight Active Record implementation for PHP5, built on top of Idiorm.
http://j4mie.github.com/idiormandparis/
996 stars 131 forks source link

Question/Request: found() function #50

Closed BoxOfSnoo closed 11 years ago

BoxOfSnoo commented 11 years ago

I'm trying to return an error if a where() call returns zero records, but Paris creates an object instead, with field defaults. Should it not return an empty object instead?

Is there some kind of functionality I'm missing? Or is this more an idea for Idiorm? Or something more basic?

treffynnon commented 11 years ago

Can you post the exact code you're trying to run? So I can get a better idea of what you're attempting to do.

You should be able to just use PHP's empty() function on the results to determine if anything was returned.

BoxOfSnoo commented 11 years ago
function getRec($id) {
    $ro = Model::factory('RepairOrder')->where('RecNo', $id)->find_array();

    // I'd like to return an error value here instead of a valid JSON record if the field is not found
    echo json_encode($ro);
}

empty doesn't work, because the array isn't empty.

treffynnon commented 11 years ago

OK so I have spent some time trying to replicate you issue and I cannot. Please see the following code and results.

Code:

ORM::configure('sqlite:./example.sqlite');

class RepairOrder extends Model {

}

$record = Model::factory('RepairOrder')->create();
$record->RecNo = 1;
$record->title = 'Record 1';
$record->save();

$record = Model::factory('RepairOrder')->create();
$record->RecNo = 2;
$record->title = 'Record 2';
$record->save();

$records = Model::factory('RepairOrder')->find_array();
var_dump($records); // Result 1
var_dump(empty($records)); // Result 2

$records = Model::factory('RepairOrder')->where('RecNo', 3)->find_array();
var_dump($records); // Result 3
var_dump(empty($records)); // Result 4

Results:

// Result 1
array(2) {
  [0]=>
  array(2) {
    ["RecNo"]=>
    string(1) "1"
    ["title"]=>
    string(8) "Record 1"
  }
  [1]=>
  array(2) {
    ["RecNo"]=>
    string(1) "2"
    ["title"]=>
    string(8) "Record 2"
  }
}

// Result 2
bool(false)

// Result 3
array(0) {
}

// Result 4
bool(true)

I hope that helps you to debug the code.

If you are still experiencing this issue then please reopen the ticket with your full code sample and results like those laid out above.

BoxOfSnoo commented 11 years ago

The code fails as above when $id is passed a string that it can't coerce into a valid integer. I have this code behind a Slim framework API, so I needed to test corrupted data being passed, I was hoping that the database level would fail, rather than returning what looks like a row at first glance.

e.g.

    $records = Model::factory('RepairOrder')->where('RONo', "x")->find_array();
    var_dump(empty($records)); // Result A
    var_dump($records); // Result B
boolean false
array (size=1)
  0 => 
    array (size=143)
...

You don't want to see my entire field set that I am working with for the moment :)

However, it looks like it may be a MySQL client issue, as passing the same SQL WHERE clause on the command line still passes me back a row.

I will leave this here for documentation reasons.