Longman\TelegramBot\DB::insertUser PHP Method

insertUser() public static method

Insert users and save their connection to chats
public static insertUser ( Longman\TelegramBot\Entities\User $user, string $date, Chat $chat = null ) : boolean
$user Longman\TelegramBot\Entities\User
$date string
$chat Longman\TelegramBot\Entities\Chat
return boolean If the insert was successful
    public static function insertUser(User $user, $date, Chat $chat = null)
    {
        if (!self::isDbConnected()) {
            return false;
        }
        $user_id = $user->getId();
        $username = $user->getUsername();
        $first_name = $user->getFirstName();
        $last_name = $user->getLastName();
        try {
            $sth = self::$pdo->prepare('
                INSERT INTO `' . TB_USER . '`
                (`id`, `username`, `first_name`, `last_name`, `created_at`, `updated_at`)
                VALUES
                (:id, :username, :first_name, :last_name, :date, :date)
                ON DUPLICATE KEY UPDATE
                    `username`   = :username,
                    `first_name` = :first_name,
                    `last_name`  = :last_name,
                    `updated_at` = :date
            ');
            $sth->bindParam(':id', $user_id, PDO::PARAM_INT);
            $sth->bindParam(':username', $username, PDO::PARAM_STR, 255);
            $sth->bindParam(':first_name', $first_name, PDO::PARAM_STR, 255);
            $sth->bindParam(':last_name', $last_name, PDO::PARAM_STR, 255);
            $sth->bindParam(':date', $date, PDO::PARAM_STR);
            $status = $sth->execute();
        } catch (PDOException $e) {
            throw new TelegramException($e->getMessage());
        }
        //insert also the relationship to the chat into user_chat table
        if ($chat instanceof Chat) {
            $chat_id = $chat->getId();
            try {
                $sth = self::$pdo->prepare('
                    INSERT IGNORE INTO `' . TB_USER_CHAT . '`
                    (`user_id`, `chat_id`)
                    VALUES
                    (:user_id, :chat_id)
                ');
                $sth->bindParam(':user_id', $user_id, PDO::PARAM_INT);
                $sth->bindParam(':chat_id', $chat_id, PDO::PARAM_INT);
                $status = $sth->execute();
            } catch (PDOException $e) {
                throw new TelegramException($e->getMessage());
            }
        }
        return $status;
    }

Usage Example

 /**
  * Start a fake conversation for the passed command and return the randomly generated ids.
  *
  * @return array
  */
 public static function startFakeConversation()
 {
     if (!DB::isDbConnected()) {
         return false;
     }
     //Just get some random values.
     $message_id = mt_rand();
     $user_id = mt_rand();
     $chat_id = mt_rand();
     //Make sure we have a valid user and chat available.
     $message = self::getFakeMessageObject(['message_id' => $message_id], ['id' => $user_id], ['id' => $chat_id]);
     DB::insertMessageRequest($message);
     DB::insertUser($message->getFrom(), null, $message->getChat());
     return compact('message_id', 'user_id', 'chat_id');
 }