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;
}