Php 如何知道ORM save()方法是否生成了更新或插入查询?

Php 如何知道ORM save()方法是否生成了更新或插入查询?,php,orm,kohana,Php,Orm,Kohana,问题是: PHP代码(无框架) 科哈纳代码(使用ORM) 当我甚至不知道这是ORM执行的插入还是更新时,我会继续创建相同的mysql\u insert\u id吗?或者有其他方法可以知道上次插入操作的id吗?我从未使用过Kohana,但是如果它像其他ORM一样工作(我使用了一点Doctrine),它应该在完成插入时自动为您设置“id”: 如果在您试图保存的内容中设置了“id”字段,则应更新记录 否则,它应该插入一条新记录,并使用数据库返回的最后一个插入的id设置“id”字段 因此,您不必关心

问题是:

PHP代码(无框架)

科哈纳代码(使用ORM)


当我甚至不知道这是ORM执行的插入还是更新时,我会继续创建相同的mysql\u insert\u id吗?或者有其他方法可以知道上次插入操作的id吗?

我从未使用过Kohana,但是如果它像其他ORM一样工作(我使用了一点Doctrine),它应该在完成插入时自动为您设置“id”:

  • 如果在您试图保存的内容中设置了“id”字段,则应更新记录
  • 否则,它应该插入一条新记录,并使用数据库返回的最后一个插入的id设置“id”字段
因此,您不必关心是否知道ORM进行了插入或更新:您将以其他方式获得id(如果是插入,ORM将从DB设置它;如果是更新,您将在之前知道它)


欲了解更多信息,请查看以下内容(引用):

save()
方法设置主键 将对象(通常为id)的
最后一次插入id

给定的示例如下所示:

// create a new page record
$page = ORM::factory('page');
$page->title = "Test Page";
$page->content = "This is a test page";
$page->save();

// create a new keyword record for the page that was just created
$keyword = ORM::factory('keyword');
$keyword->name = "testing";
$keyword->page_id = $page->id;  // $page->id has the last insert id from the above save
$keyword->save();

$page的“id”属性已经从数据库中自动设置:您不必在意这一点。

我从未使用过Kohana,但如果它像其他ORM一样工作(我使用过一点),它应该在插入时自动为您设置“id”:

  • 如果在您试图保存的内容中设置了“id”字段,则应更新记录
  • 否则,它应该插入一条新记录,并使用数据库返回的最后一个插入的id设置“id”字段
因此,您不必关心是否知道ORM进行了插入或更新:您将以其他方式获得id(如果是插入,ORM将从DB设置它;如果是更新,您将在之前知道它)


欲了解更多信息,请查看以下内容(引用):

save()
方法设置主键 将对象(通常为id)的
最后一次插入id

给定的示例如下所示:

// create a new page record
$page = ORM::factory('page');
$page->title = "Test Page";
$page->content = "This is a test page";
$page->save();

// create a new keyword record for the page that was just created
$keyword = ORM::factory('keyword');
$keyword->name = "testing";
$keyword->page_id = $page->id;  // $page->id has the last insert id from the above save
$keyword->save();

$page的“id”属性已从数据库中自动设置:您不必在意这一点。

Kohana ORM库公开了两个属性,您可以使用它们来确定记录的状态:$loaded和$saved

如果记录存在于数据库中,
$loaded
将设置为TRUE;如果记录存在于数据库中且没有未保存的更改,
$saved
将设置为TRUE

更新记录的示例

// Assume I have 1 user in my database with id 3
$user = ORM::factory('user', 3);

// These will return TRUE
$user->loaded; // TRUE
$user->saved;  // TRUE

// However if we change one of the records attributes
$user->name = 'John';

// Then the changes have not been saved, so this returns FALSE
$user->saved;  // FALSE

// But the user was loaded from the database, so this returns TRUE
$user->loaded; // TRUE

// If I now save the changes
$user->save();

// Both will now return TRUE
$user->loaded; // TRUE
$user->saved;  // TRUE
// If I create a new user
$user = ORM::factory('user');
$user->name = 'Jack';

// The user has neither been loaded from the database, nor have the changes to it been saved
// So both variables will return FALSE
$user->loaded; // FALSE
$user->saved;  // FALSE

// If I now save the user, the changes have been saved and the record is present in the db
// So both variables return TRUE
$user->loaded; // TRUE
$user->saved;  // TRUE
创建记录的示例

// Assume I have 1 user in my database with id 3
$user = ORM::factory('user', 3);

// These will return TRUE
$user->loaded; // TRUE
$user->saved;  // TRUE

// However if we change one of the records attributes
$user->name = 'John';

// Then the changes have not been saved, so this returns FALSE
$user->saved;  // FALSE

// But the user was loaded from the database, so this returns TRUE
$user->loaded; // TRUE

// If I now save the changes
$user->save();

// Both will now return TRUE
$user->loaded; // TRUE
$user->saved;  // TRUE
// If I create a new user
$user = ORM::factory('user');
$user->name = 'Jack';

// The user has neither been loaded from the database, nor have the changes to it been saved
// So both variables will return FALSE
$user->loaded; // FALSE
$user->saved;  // FALSE

// If I now save the user, the changes have been saved and the record is present in the db
// So both variables return TRUE
$user->loaded; // TRUE
$user->saved;  // TRUE

Kohana ORM库公开了两个属性,您可以使用它们来确定记录的状态:$loaded和$saved

如果记录存在于数据库中,
$loaded
将设置为TRUE;如果记录存在于数据库中且没有未保存的更改,
$saved
将设置为TRUE

更新记录的示例

// Assume I have 1 user in my database with id 3
$user = ORM::factory('user', 3);

// These will return TRUE
$user->loaded; // TRUE
$user->saved;  // TRUE

// However if we change one of the records attributes
$user->name = 'John';

// Then the changes have not been saved, so this returns FALSE
$user->saved;  // FALSE

// But the user was loaded from the database, so this returns TRUE
$user->loaded; // TRUE

// If I now save the changes
$user->save();

// Both will now return TRUE
$user->loaded; // TRUE
$user->saved;  // TRUE
// If I create a new user
$user = ORM::factory('user');
$user->name = 'Jack';

// The user has neither been loaded from the database, nor have the changes to it been saved
// So both variables will return FALSE
$user->loaded; // FALSE
$user->saved;  // FALSE

// If I now save the user, the changes have been saved and the record is present in the db
// So both variables return TRUE
$user->loaded; // TRUE
$user->saved;  // TRUE
创建记录的示例

// Assume I have 1 user in my database with id 3
$user = ORM::factory('user', 3);

// These will return TRUE
$user->loaded; // TRUE
$user->saved;  // TRUE

// However if we change one of the records attributes
$user->name = 'John';

// Then the changes have not been saved, so this returns FALSE
$user->saved;  // FALSE

// But the user was loaded from the database, so this returns TRUE
$user->loaded; // TRUE

// If I now save the changes
$user->save();

// Both will now return TRUE
$user->loaded; // TRUE
$user->saved;  // TRUE
// If I create a new user
$user = ORM::factory('user');
$user->name = 'Jack';

// The user has neither been loaded from the database, nor have the changes to it been saved
// So both variables will return FALSE
$user->loaded; // FALSE
$user->saved;  // FALSE

// If I now save the user, the changes have been saved and the record is present in the db
// So both variables return TRUE
$user->loaded; // TRUE
$user->saved;  // TRUE

如果我使用ORM,是否有一条语句允许我获取最后一次插入的id?噢!我刚找到它!真不敢相信我竟然错过了我需要的陈述!如果我使用ORM,是否有一条语句允许我获取最后一次插入的id?噢!我刚找到它!真不敢相信我竟然错过了我需要的陈述!