0

I'm new in MVC, I try to put PDO into my model

I extends PDO in my Model & page model extends from Model

PDO->Model->Page Model

my problem is because page model extends Model, so it will construct connect db again. (Model has to construct connect db, because it extends from PDO)

anyone know how to fix this?

class Model extends PDO{

    protected   $dsn        = "mysql:host=127.0.0.1; dbname=abc; charset=utf8;",
                $username   = "member", 
                $password   = "123",
                $options    = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');

    public function __construct(){
        //connect db
        parent::__construct($this->dsn, $this->username, $this->password, $this->options);
        echo "db connect";
    }
}

class index_model extends Model{

    public function __construct(){
        parent::__construct();
        //if i don't add this, will get error,
        //but if i add, I will connect db twice   
    }

    public function mainData(){
        $sql = parent::prepare("SELECT pday FROM pday");    
        $sql->execute();
        $data = $sql->fetch(PDO::FETCH_ASSOC);
        echo $data['pday'];
    }
}
Benjamin W
  • 2,658
  • 7
  • 26
  • 48
  • If you want to connect only once with the mysql server through pdo, I guess you have to implement the Singleton pattern. It's not the best way, but is a point to start... – Dan Costinel Mar 21 '16 at 12:20
  • 2
    You may find this reading extremely useful, [Your first database wrapper's childhood diseases](https://phpdelusions.net/pdo/common_mistakes) – Your Common Sense Mar 21 '16 at 12:21
  • Also, I would recommend for you to read more about what "model" actually is: http://stackoverflow.com/a/5864000/727208 – tereško Mar 22 '16 at 02:23

1 Answers1

0

I have fix my problem, I create another class for connection,

and put this inside of Model method, page model extends model, so it will not construct connection twice.

class Model{
   protected $db;

   public function connect(){
            $this->db = new DB;
   }
}

class DB extends PDO{
    protected   $dsn        = "mysql:host=127.0.0.1; dbname=abc; charset=utf8;",
                $username   = "member", 
                $password   = "123",
                $options    = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');

    public function __construct(){
          parent::__construct($this->dsn, $this->username, $this->password, $this->options);
    }
}

class index_model extends Model{
    private $sql;

    public function mainData(){
        $this->sql = $this->db->prepare("...");
        ...
    }
}

$index = new index_model;
$index->connect();
$index->mainData();
Benjamin W
  • 2,658
  • 7
  • 26
  • 48
  • It doesn't fix anything – Your Common Sense Mar 22 '16 at 06:35
  • it fixed, because Model don't extends PDO anymore, so no more PDO __construct inside of Model, when pageModel extends Model, all pageModel needs is call the connection function from Model, therefor it wont connect twice, I have tested, it works fine – Benjamin W Mar 22 '16 at 07:37
  • Yes, it fixed. For a single Model. but in a usual application you have several hundreds models. If it's really a Model you are talking about. – Your Common Sense Mar 22 '16 at 07:41