I am administering a system which is fairly old. It is a code soup at the moment and bit by bit I have been refactoring the code.
Something I noticed while doing this was that 90% of the queries were CRUD. No joins, nothing else. All really simple stuff.
As a result I created a model class which cover’s these types of query’s. I then extend the model class for each table.
class Model extends MySQLi {
public function __construct() {
global $site;
$mysql = $site['mysql'];
parent::__construct($mysql['host'], $mysql['user'], $mysql['pass'], $mysql['db']);
}
I don’t like the use of the global but it does allow me to do this without having to worry about connection details.
<?php
class Contact extends Model {
public $table = 'contact';
}
$Contact = new Contact;
$Contact->get(array('where'=>array('id >' => 4), 'limit' => array(0, 20));
My concern is that if a script requires 3 tables. I will be using 3 models and thus surely that means I will have 3 independent database connections? One for each class?
Is this going to be a problem like I think it will be?
Is there a work around which will allow me to continue to extend MySQLi and maybe pass it an active connection instead of connection details for it to make its own connection?
Do not extend Mysqli, just contain it in your model.