protected function saveMessagesToDb($messages, $db, $sourceMessageTable, $messageTable, $removeUnused, $languages, $markUnused)
    {
        $currentMessages = [];
        $rows = (new Query())->select(['id', 'category', 'message'])->from($sourceMessageTable)->all($db);
        foreach ($rows as $row) {
            $currentMessages[$row['category']][$row['id']] = $row['message'];
        }

        $currentLanguages = [];
        $rows = (new Query())->select(['language'])->from($messageTable)->groupBy('language')->all($db);
        foreach ($rows as $row) {
            $currentLanguages[] = $row['language'];
        }
        $missingLanguages = [];
        if (!empty($currentLanguages)) {
            $missingLanguages = array_diff($languages, $currentLanguages);
        }

        $new = [];
        $obsolete = [];

        foreach ($messages as $category => $msgs) {
            $msgs = array_unique($msgs);

            if (isset($currentMessages[$category])) {
                $new[$category] = array_diff($msgs, $currentMessages[$category]);
                $obsolete += array_diff($currentMessages[$category], $msgs);
            } else {
                $new[$category] = $msgs;
            }
        }

        foreach (array_diff(array_keys($currentMessages), array_keys($messages)) as $category) {
            $obsolete += $currentMessages[$category];
        }

        if (!$removeUnused) {
            foreach ($obsolete as $pk => $msg) {
                if (mb_substr($msg, 0, 2) === '@@' && mb_substr($msg, -2) === '@@') {
                    unset($obsolete[$pk]);
                }
            }
        }

        $obsolete = array_keys($obsolete);
        $this->stdout('Inserting new messages...');
        $savedFlag = false;

        foreach ($new as $category => $msgs) {
            foreach ($msgs as $msg) {
                $savedFlag = true;
                $lastPk = $db->schema->insert($sourceMessageTable, ['category' => $category, 'message' => $msg]);
                foreach ($languages as $language) {
                    $db->createCommand()
                       ->insert($messageTable, ['id' => $lastPk['id'], 'language' => $language])
                       ->execute();
                }
            }
        }

        if (!empty($missingLanguages)) {
            $updatedMessages = [];
            $rows = (new Query())->select(['id', 'category', 'message'])->from($sourceMessageTable)->all($db);
            foreach ($rows as $row) {
                $updatedMessages[$row['category']][$row['id']] = $row['message'];
            }
            foreach ($updatedMessages as $category => $msgs) {
                foreach ($msgs as $id => $msg) {
                    $savedFlag = true;
                    foreach ($missingLanguages as $language) {
                        $db->createCommand()
                            ->insert($messageTable, ['id' => $id, 'language' => $language])
                            ->execute();
                    }
                }
            }
        }

        $this->stdout($savedFlag ? "saved.\n" : "Nothing to save.\n");
        $this->stdout($removeUnused ? 'Deleting obsoleted messages...' : 'Updating obsoleted messages...');

        if (empty($obsolete)) {
            $this->stdout("Nothing obsoleted...skipped.\n");
            return;
        }

        if ($removeUnused) {
            $db->createCommand()
               ->delete($sourceMessageTable, ['in', 'id', $obsolete])
               ->execute();
            $this->stdout("deleted.\n");
        } elseif ($markUnused) {
            $rows = (new Query())
                ->select(['id', 'message'])
                ->from($sourceMessageTable)
                ->where(['in', 'id', $obsolete])
                ->all($db);

            foreach ($rows as $row) {
                $db->createCommand()->update(
                    $sourceMessageTable,
                    ['message' => '@@' . $row['message'] . '@@'],
                    ['id' => $row['id']]
                )->execute();
            }
            $this->stdout("updated.\n");
        } else {
            $this->stdout("kept untouched.\n");
        }
    }
protected function saveMessagesToDb($messages, $db, $sourceMessageTable, $messageTable, $removeUnused, $languages, $markUnused)
    {
        $currentMessages = [];
        $rows = (new Query())->select(['id', 'category', 'message'])->from($sourceMessageTable)->all($db);
        foreach ($rows as $row) {
            $currentMessages[$row['category']][$row['id']] = $row['message'];
        }

        $currentLanguages = [];
        $rows = (new Query())->select(['language'])->from($messageTable)->groupBy('language')->all($db);
        foreach ($rows as $row) {
            $currentLanguages[] = $row['language'];
        }
        $missingLanguages = [];
        if (!empty($currentLanguages)) {
            $missingLanguages = array_diff($languages, $currentLanguages);
        }

        $new = [];
        $obsolete = [];

        foreach ($messages as $category => $msgs) {
            $msgs = array_unique($msgs);

            if (isset($currentMessages[$category])) {
                $new[$category] = array_diff($msgs, $currentMessages[$category]);
                $obsolete += array_diff($currentMessages[$category], $msgs);
            } else {
                $new[$category] = $msgs;
            }
        }

        foreach (array_diff(array_keys($currentMessages), array_keys($messages)) as $category) {
            $obsolete += $currentMessages[$category];
        }

        if (!$removeUnused) {
            foreach ($obsolete as $pk => $msg) {
                if (mb_substr($msg, 0, 2) === '@@' && mb_substr($msg, -2) === '@@') {
                    unset($obsolete[$pk]);
                }
            }
        }

        $obsolete = array_keys($obsolete);
        $this->stdout('Inserting new messages...');
        $savedFlag = false;

        foreach ($new as $category => $msgs) {
            foreach ($msgs as $msg) {
                $savedFlag = true;
                $lastPk = $db->schema->insert($sourceMessageTable, ['category' => $category, 'message' => $msg]);
                foreach ($languages as $language) {
                    $db->createCommand()
                       ->insert($messageTable, ['id' => $lastPk['id'], 'language' => $language])
                       ->execute();
                }
            }
        }

        if (!empty($missingLanguages)) {
            $updatedMessages = [];
            $rows = (new Query())->select(['id', 'category', 'message'])->from($sourceMessageTable)->all($db);
            foreach ($rows as $row) {
                $updatedMessages[$row['category']][$row['id']] = $row['message'];
            }
            foreach ($updatedMessages as $category => $msgs) {
                foreach ($msgs as $id => $msg) {
                    $savedFlag = true;
                    foreach ($missingLanguages as $language) {
                        $db->createCommand()
                            ->insert($messageTable, ['id' => $id, 'language' => $language])
                            ->execute();
                    }
                }
            }
        }

        $this->stdout($savedFlag ? "saved.\n" : "Nothing to save.\n");
        $this->stdout($removeUnused ? 'Deleting obsoleted messages...' : 'Updating obsoleted messages...');

        if (empty($obsolete)) {
            $this->stdout("Nothing obsoleted...skipped.\n");
            return;
        }

        if ($removeUnused) {
            $db->createCommand()
               ->delete($sourceMessageTable, ['in', 'id', $obsolete])
               ->execute();
            $this->stdout("deleted.\n");
        } elseif ($markUnused) {
            $rows = (new Query())
                ->select(['id', 'message'])
                ->from($sourceMessageTable)
                ->where(['in', 'id', $obsolete])
                ->all($db);

            foreach ($rows as $row) {
                $db->createCommand()->update(
                    $sourceMessageTable,
                    ['message' => '@@' . $row['message'] . '@@'],
                    ['id' => $row['id']]
                )->execute();
            }
            $this->stdout("updated.\n");
        } else {
            $this->stdout("kept untouched.\n");
        }
    }
protected function saveMessagesToDb ( $ messages , $ db , $ sourceMessageTable , $ messageTable , $ removeUnused , $ languages , $ markUnused ) { $ currentMessages = [ ] ; $ rows = ( new Query ( ) ) -> select ( [ 'id' , 'category' , 'message' ] ) -> from ( $ sourceMessageTable ) -> all ( $ db ) ; foreach ( $ rows as $ row ) { $ currentMessages [ $ row [ 'category' ] ] [ $ row [ 'id' ] ] = $ row [ 'message' ] ; } $ currentLanguages = [ ] ; $ rows = ( new Query ( ) ) -> select ( [ 'language' ] ) -> from ( $ messageTable ) -> groupBy ( 'language' ) -> all ( $ db ) ; foreach ( $ rows as $ row ) { $ currentLanguages [ ] = $ row [ 'language' ] ; } $ missingLanguages = [ ] ; if ( ! empty ( $ currentLanguages ) ) { $ missingLanguages = array_diff ( $ languages , $ currentLanguages ) ; } $ new = [ ] ; $ obsolete = [ ] ; foreach ( $ messages as $ category => $ msgs ) { $ msgs = array_unique ( $ msgs ) ; if ( isset ( $ currentMessages [ $ category ] ) ) { $ new [ $ category ] = array_diff ( $ msgs , $ currentMessages [ $ category ] ) ; $ obsolete += array_diff ( $ currentMessages [ $ category ] , $ msgs ) ; } else { $ new [ $ category ] = $ msgs ; } } foreach ( array_diff ( array_keys ( $ currentMessages ) , array_keys ( $ messages ) ) as $ category ) { $ obsolete += $ currentMessages [ $ category ] ; } if ( ! $ removeUnused ) { foreach ( $ obsolete as $ pk => $ msg ) { if ( mb_substr ( $ msg , 0 , 2 ) === '@@' && mb_substr ( $ msg , - 2 ) === '@@' ) { unset ( $ obsolete [ $ pk ] ) ; } } } $ obsolete = array_keys ( $ obsolete ) ; $ this -> stdout ( 'Inserting new messages...' ) ; $ savedFlag = false ; foreach ( $ new as $ category => $ msgs ) { foreach ( $ msgs as $ msg ) { $ savedFlag = true ; $ lastPk = $ db -> schema -> insert ( $ sourceMessageTable , [ 'category' => $ category , 'message' => $ msg ] ) ; foreach ( $ languages as $ language ) { $ db -> createCommand ( ) -> insert ( $ messageTable , [ 'id' => $ lastPk [ 'id' ] , 'language' => $ language ] ) -> execute ( ) ; } } } if ( ! empty ( $ missingLanguages ) ) { $ updatedMessages = [ ] ; $ rows = ( new Query ( ) ) -> select ( [ 'id' , 'category' , 'message' ] ) -> from ( $ sourceMessageTable ) -> all ( $ db ) ; foreach ( $ rows as $ row ) { $ updatedMessages [ $ row [ 'category' ] ] [ $ row [ 'id' ] ] = $ row [ 'message' ] ; } foreach ( $ updatedMessages as $ category => $ msgs ) { foreach ( $ msgs as $ id => $ msg ) { $ savedFlag = true ; foreach ( $ missingLanguages as $ language ) { $ db -> createCommand ( ) -> insert ( $ messageTable , [ 'id' => $ id , 'language' => $ language ] ) -> execute ( ) ; } } } } $ this -> stdout ( $ savedFlag ? "saved.\n" : "Nothing to save.\n" ) ; $ this -> stdout ( $ removeUnused ? 'Deleting obsoleted messages...' : 'Updating obsoleted messages...' ) ; if ( empty ( $ obsolete ) ) { $ this -> stdout ( "Nothing obsoleted...skipped.\n" ) ; return ; } if ( $ removeUnused ) { $ db -> createCommand ( ) -> delete ( $ sourceMessageTable , [ 'in' , 'id' , $ obsolete ] ) -> execute ( ) ; $ this -> stdout ( "deleted.\n" ) ; } elseif ( $ markUnused ) { $ rows = ( new Query ( ) ) -> select ( [ 'id' , 'message' ] ) -> from ( $ sourceMessageTable ) -> where ( [ 'in' , 'id' , $ obsolete ] ) -> all ( $ db ) ; foreach ( $ rows as $ row ) { $ db -> createCommand ( ) -> update ( $ sourceMessageTable , [ 'message' => '@@' . $ row [ 'message' ] . '@@' ] , [ 'id' => $ row [ 'id' ] ] ) -> execute ( ) ; } $ this -> stdout ( "updated.\n" ) ; } else { $ this -> stdout ( "kept untouched.\n" ) ; } }
Saves messages to database.

@param array $messages
@param Connection $db
@param string $sourceMessageTable
@param string $messageTable
@param bool $removeUnused
@param array $languages
@param bool $markUnused
Saves messages to database .