0

I am experienced with OOP but I am just starting to use the MVC model.

I would like to know what are the recommended practices to use PDO within the model and still keep a high level of abstraction. I have seen on the internet various approaches, the ones I liked the most were:

Making the model extends a specialized class for SQL queries, something like this:

class Car extends Model{...}
class Model extends SQLQuery{...}

Passing around a global database connection

class Car extends Model{
    private $connection;
    __constructor($pdo ...){
        $this->connection = $pdo;
        ...
    }
}

I also read some folks saying that all the queries should be made in the controller and use the model only to structure the data. I did not like much this one because it would not allow me to do such things like making a object change his self and update the database.

So, what are your recommendations? Thanks.

Daniel Oliveira
  • 1,280
  • 14
  • 36
  • 2
    Since when "Car" is a specialized subtype of "Database Connection" or "SQL Query"? – tereško Aug 05 '17 at 19:28
  • @tereško what I meant there was "SQLQuery" not "DatabaseConnection" – Daniel Oliveira Aug 05 '17 at 19:31
  • 1
    That made it actually worse, since queries are what PDO executes. – tereško Aug 05 '17 at 19:31
  • if you are going to pass around a DB connection, Id suggest using a singleton, or what I call a mutiton ( multiple singleton ) then you have just one connection, mutiton is useful if you connect to more then on DB, I just added some interfaces and traits for these 2 to my project here > https://github.com/ArtisticPhoenix/Evo/tree/master/Evo/Core/Singleton – ArtisticPhoenix Aug 05 '17 at 19:44
  • 4
    @ArtisticPhoenix that's completely **WRONG**! If you need to pass around the same connection to multiple objects, you use dependency injection, which **he is already doing**. – tereško Aug 05 '17 at 19:46
  • Another Idea is to feed PDO a class directly this is possible, then have a save, and find( id ) method, ORM style. with PDO::FETCH_CLASS – ArtisticPhoenix Aug 05 '17 at 19:47
  • Databases are a special case, IMO, one DB connection is the same as another is the same as another. It vastly simplifies things, there is little difference between wrapping PDO in a singleton, or passing a PDO object as an argument, in either case you are tightly coupled with the PDO library, it makes no difference how you got it. – ArtisticPhoenix Aug 05 '17 at 19:51
  • For example if PHP removed the `prepare` method from PDO and re-named it ( something that would never happen ) it wouldn't matter how you got that PDO instance, it's the same thing. I'm not talking about an extensive class just a wrapper for the connection. – ArtisticPhoenix Aug 05 '17 at 19:53
  • Besides there is no rule that says you cant pass a singleton derived object into a constructor, so you can do both. it's a matter of maintaining the state of the connection. – ArtisticPhoenix Aug 05 '17 at 19:59
  • 1
    @ArtisticPhoenix you probably should read this thing: https://stackoverflow.com/questions/11369360/how-to-properly-set-up-a-pdo-connection/11369679#11369679 .. and learn about the concept of data mappers: https://martinfowler.com/eaaCatalog/dataMapper.html, because you seem to be following the .. emm .. "false path of extend-everything" – tereško Aug 05 '17 at 22:22
  • @tereško - maybe you should read this, before you assume what I am following, the Database is sort of a special case IMO because it's a resource wrapper. https://github.com/ArtisticPhoenix/Evo – ArtisticPhoenix Aug 06 '17 at 00:51
  • There is nothing to read there. It is a globally scoped mess ;D Please stop spreading that bs. – tereško Aug 22 '17 at 18:16

0 Answers0