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

时间:2009-08-10 03:49:16

标签: php orm kohana

问题:

PHP代码(无框架)

if ($this->uid)
        {
            $query = sprintf('UPDATE %sUSER SET USERNAME = "%s", ' .
                'PASSWORD = "%s", EMAIL_ADDR = "%s", IS_ACTIVE = %d ' .
                'WHERE USER_ID = %d',
                DB_TBL_PREFIX,
                mysql_real_escape_string($this->username, $GLOBALS['DB']),
                mysql_real_escape_string($this->password, $GLOBALS['DB']),
                mysql_real_escape_string($this->emailAddr, $GLOBALS['DB']),
                $this->isActive,
                $this->userId);
            mysql_query($query, $GLOBALS['DB']);
        }
        else
        {
            $query = sprintf('INSERT INTO %sUSER (USERNAME, PASSWORD, ' .
                'EMAIL_ADDR, IS_ACTIVE) VALUES ("%s", "%s", "%s", %d)',
                DB_TBL_PREFIX,
                mysql_real_escape_string($this->username, $GLOBALS['DB']),
                mysql_real_escape_string($this->password, $GLOBALS['DB']),
                mysql_real_escape_string($this->emailAddr, $GLOBALS['DB']),
                $this->isActive);
            mysql_query($query, $GLOBALS['DB']);

            $this->uid = mysql_insert_id($GLOBALS['DB']);
        }

Kohana Code(使用ORM)

public function save()
    {
        $query = ORM::factory('user', $this->user->uid);

        if ($this->user->uid) :
            $query->username = $this->user->username;
            $query->password = $this->user->password;
            $query->email_addr = $this->user->emailAddr;
            $query->is_active = $this->user->isActive;
        else:
               //????????

    }

当我甚至不知道它是插入还是ORM执行的更新时,我会创建相同的mysql_insert_id吗?或者有另一种方法来知道最后一次插入操作的id吗?

2 个答案:

答案 0 :(得分:3)

我从未与Kohana合作过,但是如果它像其他一些ORM (我已经使用了Doctrine)一样,它应该在你完成后自动为你设置“id”插入:

  • 如果在您尝试保存的内容中设置了“id”字段,则应更新记录
  • 否则,它应该插入一条新记录,并将“id”字段设置为数据库返回的最后插入的id。

所以你不必关心ORM是否进行了插入或更新:你将获得id (如果是插入,ORM将从数据库中设置它;如果更新,你将会之前就知道了。


有关更多信息,您可以查看Creating New Records and Adding Related Records in One-to-Many Relationships,其中说明(引用):

  

save()方法设置主键   对象(通常是id)的   last_insert_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”属性已自动从DB设置:您不必关心它。

答案 1 :(得分:2)

Kohana ORM lib公开了两个可用于确定记录状态的属性:$ 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