如何检查插入是否已执行?

How can I check if an insert executed?

如何检查插入是否已执行?

$s = Visitor::insert($items);

变量$s包含插入的对象,不是布尔类型。

$s->exists 会告诉您条目是否在数据库中。

另一个好的检查是使用

$s = Visitor::insertGetId($items);

if($s)
{
  // inserted
}

另一种方法是使用wasRecentlyCreated:

$s = Visitor::insert($items);

if ($s->wasRecentlyCreated === true) { //check if a new row was created 
    return 'true';
}