select('1') ->from(self::TABLE_NAME) ->andWhere(Op::equals(self::getWhere($userPreorder))) ->execute()->rowCount() === 1; } /** * @throws Exception * @throws \Doctrine\DBAL\Driver\Exception */ public static function get(UserPreorder $userPreorder): ?array { $qb = sqlQueryBuilder() ->select('*') ->from(self::TABLE_NAME) ->andWhere(Op::equals(self::getWhere($userPreorder))); $res = $qb->execute()->fetchAllAssociative(); if (count($res) !== 1) { return null; } $res = $res[0]; $res['custom_data'] = json_decode($res['custom_data'], true); $res['history'] = json_decode($res['history'], true); return $res; } /** * @throws Exception */ public static function upsert(UserPreorder $userPreorder, ?array $customData = null, ?array $messages = null): void { $exists = self::exists($userPreorder); $values = []; if ($customData !== null) { $values['custom_data'] = json_encode($customData); } if ($messages !== null) { $values['history'] = json_encode($messages); } if ($exists) { sqlQueryBuilder() ->update(self::TABLE_NAME) ->andWhere(Op::equals(self::getWhere($userPreorder))) ->directValues($values) ->execute(); return; } sqlQueryBuilder() ->insert(self::TABLE_NAME) ->directValues(array_merge(self::getWhere($userPreorder), $values)) ->execute(); } public static function getWhere(UserPreorder $userPreorder): array { return [ 'id_preorder_date' => $userPreorder->preorderDateId, 'id_user' => $userPreorder->userId, ]; } }