After some help from teresko, i have managed to implement the factory design method in order to connect to my database only once (thats what i thought), but now im recieving this error:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[08004] [1040] Too many connections' in
It then goes on to describe about 8 different connections in different places. How comes im making so many connections?
Here is my factory class:
namespace App\Core;
class structureFactory
{
protected $provider = null;
protected $connection = null;
public function __construct( callable $provider )
{
$this->provider = $provider;
}
public function create( $name)
{
if ( $this->connection === null )
{
$this->connection = call_user_func( $this->provider );
}
return new $name( $this->connection );
}
}
And because i only need the connections in my model classes i have created a baseModel class which gets the database connection from structureFactory and looks like this:
namespace App\Core\Models;
use App\Core\structureFactory as SF;
abstract class baseModel {
public $getConnection;
protected function getAdapter() {
$this->getConnection = function() {
$m_rdbms = 'mysql';
$m_host = 'localhost';
$m_db_name = 'cvcms';
$m_host_name = $m_rdbms . ':host=' . $m_host. ';dbname=' . $m_db_name;
$m_uname = 'root';
$m_pwd ='';
$instance = new \PDO($m_host_name, $m_uname, $m_pwd);
$instance->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$instance->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
return $instance;
};
$factory = new SF($this->getConnection);
return $factory->create('App\\Core\\Models\\DAL');
}
}
So this basically makes a new SF with the Connection made in getConnection function, and then returns a new DAL.
Here is an example of my controller which makes a new DAL and calls the function safeQuery :
namespace App\Frontend\Base\Index;
use App\Frontend\Base\baseController as base;
use App\Core\Models\DAL as DAL;
class indexController extends base{
public $data;
public function __construct(){
parent::__construct();
}
function indexMethod(){
$query = "SELECT site_data FROM site_data WHERE site_data = :name;";
$args = array(":name" => 'site_name');
$this->data = new DAL;
$this->data = $this->data->safeQuery($query,$args);
}
function render(){
//Render the index template/view parsing the data acquired from model.
}
}
And in my DAL construct i store the connection in a local variable like so by gettting it from the baseModel:
use App\Core\Models\baseModel as BM;
class DAL extends BM {
private $c_arr_database_connection_messages;
private $c_obj_pdo;
private $c_obj_stmt;
public function __construct() {
$this->c_arr_database_connection_messages = array();
$this->c_obj_pdo = $this->getAdapter();
echo $this->c_obj_pdo;
$this->c_obj_stmt = null;
}
...
}
Can you guys help me work out where im going wrong please?
Cheers Tom
Your structureFactory
could look something like this.
namespace App\Core;
class structureFactory
{
protected $connection;
public function getConnection()
{
if (!$this->connection) {
$this->connection = new PDO('dsn');
}
return $this->connection;
}
}
namespace App\Core\Models;
use App\Core\structureFactory as structureFactory;
abstract class baseModel {
protected $factory;
public function __construct(structureFactory $factory)
{
$this->factory = $factory;
}
protected function getAdapter() {
return $this->factory->getConnection();
}
}
use App\Core\Models\baseModel as BM;
class DAL extends BM {
private $c_arr_database_connection_messages;
private $c_obj_pdo;
private $c_obj_stmt;
public function __construct(App\Core\structureFactory $factory) {
parent::__construct($factory);
$this->c_arr_database_connection_messages = array();
$this->c_obj_pdo = $this->getAdapter();
echo $this->c_obj_pdo;
$this->c_obj_stmt = null;
}
...
}