Extract SQL Statements To Gateways

Summary of what happened: Of the long list below, there are only StatsModel and XhrMethods left. The former will probably be solved by !2176 (merged). The latter might still take a while.


We followed through the book's recommendations about adding SQL gateway classes, and implemented a PDO-based database connection and a unit test in https://gitlab.com/foodsharing-dev/foodsharing/commits/extract-database-gateways.

It basically:

  • reimplements all the database access from the *Model classes in *Gateway classes
  • uses PDO instead of mysqli
  • use DI-style (pass in dependencies into constructor)
  • supports unit testing

A few thoughts on how that went:

  • it would be a lot of work to reimplement everything
  • would probably want to fix the inconsistent return values (e.g. result on success, or boolean on failure)

So, we'd want to consider a bit more of the wider impact before proceeding.

A few options on how we could proceed:

  • call the Gateway class from the existing Model class so the Control/Xhr classes don't need to change
  • do more minimal change (as Model classes are already more-or-less the same concept as the gateways)
    • remove all PREFIX values (not actually used) (all removed by Peter)
    • use mysqli prepared statements (can only do by-index I think, not named)
  • just switch model classes to use PDO and prepared statements with named parameters (keep everything else as similar as possible)
  • go forward with deeper changes to the things that call the Model classes currently (would need more investigation and thought)

Use in model classes

  • ActivityModel.php !1434 (merged)
  • APIModel.php
  • ApplicationModel.php
  • BasketModel.php (partially)
  • BellModel.php (partially)
  • BlogModel.php
  • BuddyModel.php
  • BusinessCardModel.php
  • ConsoleModel.php
  • ContentModel.php (partially)
  • DashboardModel.php
  • Model.php (partially)
  • ManualDb.php
  • src/Lib/Db/Db.php (contains only legacy mysqli code, can be removed once the migration to PDO is complete)
  • EmailTemplateAdminModel.php
  • EventModel.php (partially)
  • FairTeilerModel.php
  • FoodsaverModel.php WIP in !1178 (merged)
  • GeoCleanModel.php
  • IndexModel.php
  • InfoModel.php
  • LoginModel.php
  • LookupModel.php
  • MailboxModel.php
  • MailsModel.php
  • MaintenanceModel.php !1371 (closed) !1394 (merged)
  • MessageModel.php gets deleted by: !887 (merged)
  • NewAreaModel.php
  • PassportGeneratorModel.php
  • ProfileModel.php
  • QuizModel.php
  • RegionModel.php (-> RegionGateway & ForumGateway)
  • ReportModel.php
  • SearchModel.php
  • SettingsModel.php (done: !1163 (merged) & !1173 (closed))
  • StatisticsModel.php
  • StatsModel.php (maybe just leave the fetchweight and -count functions untouched as this topic is very controversial) #622 (closed)
    • Subtask: !1274 (merged) Remove stats and maintenance methods by seperate MR to be able to work on it if it is ever needed again later. See wiki.
  • StatsmanModel.php
  • StoreModel.php => See #1029 (closed) for current progress
  • TeamModel.php
  • WallPostModel.php
  • WorkGroupModel.php

Use in other classes

Search for use Foodsharing\Lib\Db\Db; or Db $model, to see these db imports (currently 26x).

Edited by Fridtjof