本文整理汇总了PHP中yii\db\Connection::getLastInsertID方法的典型用法代码示例。如果您正苦于以下问题:PHP Connection::getLastInsertID方法的具体用法?PHP Connection::getLastInsertID怎么用?PHP Connection::getLastInsertID使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yii\db\Connection
的用法示例。
在下文中一共展示了Connection::getLastInsertID方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: lastInsertId
/**
* Returns id of inserted record
*
* @return int
*/
protected function lastInsertId()
{
return (int) $this->db->getLastInsertID();
}
示例2: saveMessagesToDb
/**
* Saves messages to database
*
* @param array $messages
* @param \yii\db\Connection $db
* @param string $sourceMessageTable
* @param string $messageTable
* @param boolean $removeUnused
* @param array $languages
*/
protected function saveMessagesToDb($messages, $db, $sourceMessageTable, $messageTable, $removeUnused, $languages)
{
$q = new \yii\db\Query();
$current = [];
foreach ($q->select(['id', 'category', 'message'])->from($sourceMessageTable)->all() as $row) {
$current[$row['category']][$row['id']] = $row['message'];
}
$new = [];
$obsolete = [];
foreach ($messages as $category => $msgs) {
$msgs = array_unique($msgs);
if (isset($current[$category])) {
$new[$category] = array_diff($msgs, $current[$category]);
$obsolete += array_diff($current[$category], $msgs);
} else {
$new[$category] = $msgs;
}
}
foreach (array_diff(array_keys($current), array_keys($messages)) as $category) {
$obsolete += $current[$category];
}
if (!$removeUnused) {
foreach ($obsolete as $pk => $m) {
if (mb_substr($m, 0, 2) === '@@' && mb_substr($m, -2) === '@@') {
unset($obsolete[$pk]);
}
}
}
$obsolete = array_keys($obsolete);
$this->stdout("Inserting new messages...");
$savedFlag = false;
foreach ($new as $category => $msgs) {
foreach ($msgs as $m) {
$savedFlag = true;
$db->createCommand()->insert($sourceMessageTable, ['category' => $category, 'message' => $m])->execute();
$lastID = $db->getLastInsertID();
foreach ($languages as $language) {
$db->createCommand()->insert($messageTable, ['id' => $lastID, 'language' => $language])->execute();
}
}
}
$this->stdout($savedFlag ? "saved.\n" : "Nothing new...skipped.\n");
$this->stdout($removeUnused ? "Deleting obsoleted messages..." : "Updating obsoleted messages...");
if (empty($obsolete)) {
$this->stdout("Nothing obsoleted...skipped.\n");
} else {
if ($removeUnused) {
$db->createCommand()->delete($sourceMessageTable, ['in', 'id', $obsolete])->execute();
$this->stdout("deleted.\n");
} else {
$db->createCommand()->update($sourceMessageTable, ['message' => new \yii\db\Expression("CONCAT('@@',message,'@@')")], ['in', 'id', $obsolete])->execute();
$this->stdout("updated.\n");
}
}
}
示例3: saveMessagesToDb
/**
* Saves messages to database
*
* @param array $messages
* @param \yii\db\Connection $db
* @param string $sourceMessageTable
* @param string $messageTable
* @param boolean $removeUnused
* @param array $languages
*/
public function saveMessagesToDb($messages, $db, $sourceMessageTable, $messageTable, $removeUnused, $languages)
{
$q = new \yii\db\Query();
$current = [];
foreach ($q->select(['id', 'category', 'message'])->from($sourceMessageTable)->all() as $row) {
$current[$row['category']][$row['id']] = $row['message'];
}
$new = [];
$obsolete = [];
foreach ($messages as $category => $msgs) {
$msgs = array_unique($msgs);
if (isset($current[$category])) {
$new[$category] = array_diff($msgs, $current[$category]);
$obsolete += array_diff($current[$category], $msgs);
} else {
$new[$category] = $msgs;
}
}
foreach (array_diff(array_keys($current), array_keys($messages)) as $category) {
$obsolete += $current[$category];
}
if (!$removeUnused) {
foreach ($obsolete as $pk => $m) {
if (mb_substr($m, 0, 2) === '@@' && mb_substr($m, -2) === '@@') {
unset($obsolete[$pk]);
}
}
}
$obsolete = array_keys($obsolete);
$this->stdout("Inserting new messages...");
$savedFlag = false;
$columnNames = $db->getTableSchema($sourceMessageTable)->columnNames;
$hasLocationColumn = in_array('location', $columnNames) ?: false;
d($hasLocationColumn);
foreach ($new as $category => $msgs) {
d([$category, $msgs]);
foreach ($msgs as $m) {
$savedFlag = true;
$msgHash = md5($m);
$sourceMessageData = ['category' => $category, 'message' => $m];
if (true === $hasLocationColumn) {
$sourceMessageData['location'] = $this->extractLocations($category, $m);
$sourceMessageData['hash'] = $msgHash;
}
$db->createCommand()->insert($sourceMessageTable, $sourceMessageData)->execute();
$lastID = $db->driverName == 'pgsql' ? $db->getLastInsertID($sourceMessageTable . '_id_seq') : $db->getLastInsertID();
dd($lastID);
foreach ($languages as $language) {
$messageData = ['id' => $lastID, 'language' => $language];
if (true === $hasLocationColumn) {
$messageData['hash'] = $msgHash;
}
$db->createCommand()->insert($messageTable, $messageData)->execute();
}
}
}
$this->stdout($savedFlag ? "saved." . PHP_EOL : "Nothing new...skipped." . PHP_EOL);
$this->stdout($removeUnused ? "Deleting obsoleted messages..." . PHP_EOL : "Updating obsoleted messages..." . PHP_EOL);
if (empty($obsolete)) {
$this->stdout("Nothing obsoleted!...skipped." . PHP_EOL);
} else {
if ($removeUnused) {
$db->createCommand()->delete($sourceMessageTable, ['in', 'id', $obsolete])->execute();
$this->stdout("deleted." . PHP_EOL);
} else {
$db->createCommand()->update($sourceMessageTable, ['message' => new \yii\db\Expression("CONCAT('@@',message,'@@')")], ['in', 'id', $obsolete])->execute();
$this->stdout("updated." . PHP_EOL);
}
}
// ------------------------------ COUNTER ------------------------------
$counter = ['new' => 0, 'obsolete' => 0];
foreach ($new as $msgs) {
$counter['new'] += count($msgs);
}
foreach ($obsolete as $msgs) {
$counter['obsolete'] += count($msgs);
}
return $counter;
}