j4mie / idiorm

A lightweight nearly-zero-configuration object-relational mapper and fluent query builder for PHP5.
http://j4mie.github.com/idiormandparis/
2.01k stars 369 forks source link

Delete() does not affect #158

Closed tashemi closed 11 years ago

tashemi commented 11 years ago

I try to delete a row using next code:

$event = ORM::for_table('events')->where('accountId', $accountId)->find_one($eventId);
$event->delete();

Idiorm generates next SQL

Select * From `events` Where `accountId`='2' And `id`=1
Delete From `events` Where `id`=NULL

Why can it happen?

treffynnon commented 11 years ago

Could you try the following code and report back the results?

$event = ORM::for_table('events')->find_one($eventId);
var_dump($event);

Incidentally if you are using find_one() and supplying it an ID then you don't need the where() in your query as the ID is already a unique reference/primary key to a certain record.

tashemi commented 11 years ago

Table events contains events created by different users. I use where() be sure that logged user tries delete his own event and not somebody's else. var_dump results:

object(Idiorm)#29 (22) {
  ["_connection_name":protected]=>
  string(7) "default"
  ["_table_name":protected]=>
  string(6) "events"
  ["_table_alias":protected]=>
  NULL
  ["_values":protected]=>
  array(0) {
  }
  ["_result_columns":protected]=>
  array(1) {
    [0]=>
    string(1) "*"
  }
  ["_using_default_result_columns":protected]=>
  bool(true)
  ["_join_sources":protected]=>
  array(0) {
  }
  ["_distinct":protected]=>
  bool(false)
  ["_is_raw_query":protected]=>
  bool(false)
  ["_raw_query":protected]=>
  string(0) ""
  ["_raw_parameters":protected]=>
  array(0) {
  }
  ["_where_conditions":protected]=>
  array(0) {
  }
  ["_limit":protected]=>
  NULL
  ["_offset":protected]=>
  NULL
  ["_order_by":protected]=>
  array(0) {
  }
  ["_group_by":protected]=>
  array(0) {
  }
  ["_having_conditions":protected]=>
  array(0) {
  }
  ["_data":protected]=>
  array(8) {
    ["Id"]=>
    string(1) "1"
    ["AccountId"]=>
    string(1) "2"
    ["Caption"]=>
    string(10) "Coffee cup"
    ["Date"]=>
    string(10) "2013-10-02"
    ["Time"]=>
    string(5) "08:30"
    ["Place"]=>
    string(4) "Kyiv"
    ["Details"]=>
    string(47) "Morning coffee with nice talking about Universe"
    ["Coords"]=>
    NULL
  }
  ["_dirty_fields":protected]=>
  array(0) {
  }
  ["_expr_fields":protected]=>
  array(0) {
  }
  ["_is_new":protected]=>
  bool(false)
  ["_instance_id_column":protected]=>
  NULL
}
treffynnon commented 11 years ago

I can see your problem. Your ID column is not the same as that expected by Idiorm. You are using Id whereas Idiorm expects id (note all lower case). So you have a few choices:

  1. Rename the column to id instead of Id
  2. Use the Idiorm configuration to set the correct column name for that table. See: http://idiorm.readthedocs.org/en/latest/configuration.html#id-column (note there is a global and separate per-table setting)
  3. use_id_column() - see PHPDoc at: https://github.com/j4mie/idiorm/blob/a1f5dc4029526f0d2605bce4a6f1f4c562ce9aa9/idiorm.php#L552
tashemi commented 11 years ago

@treffynnon thanks, now I see difference