Salut
Nu pot sa spun daca e bine sau nu cum vrei sa construiesti sistemul MVC, depinde de obijnuinta si experienta care se imbunatateste treptat.
Eu retin intr-un array numele tabelelor, iar coloanele sunt specificate direct cu numele lor in $sql (nu le tin si in vreo variabila)
Ca sa folosesti clasa pdo_mysqli in alta clasa, poti sa faci acea clasa ca extindere din clasa pdo_mysqli:
Cod: Selectaţi tot
class model extends pdo_mysqli {
//properties
function __construct($conn_data) {
parent::__construct($conn_data); //include the parent __construct() instructions
}
//add categories, $frm = form data
public function addCtg($frm){
// take needed values for #sql, to pass as array
$val = ['name'=>$frm['name'], 'parent'=>$frm['parent']];
$sql = "INSERT INTO category (name, parent) VALUES (:name, :parent)";
//return response of sqlExec() for insert data
return $conn->sqlExec($sql, $val);
}
}
Sau definesti in acea clasa o proprietate care sa contina obiectul clasei mysqli_pdo.
Cam asa am la CMS:
- Fisier common.php, cu date conectare mysql, start sesiune, constante si array-ul cu tabele. Il includ in toate fisierele php
Cod: Selectaţi tot
// For connecting to MySQL database (MySQL server, user, password, database name)
$mysql['host'] = 'localhost';
$mysql['user'] = 'root';
$mysql['pass'] = '';
$mysql['bdname'] = 'cms';
// start session (if isn't started)
if(!isset($_SESSION)) {
$sn = session_name();
if(isset($_COOKIE[$sn])) $sessid = $_COOKIE[$sn];
else if(isset($_GET[$sn])) $sessid = $_GET[$sn];
if(!isset($sessid) || preg_match('/^[a-z0-9,\-]{22,40}$/i', $sessid)) session_start();
else {
session_id(uniqid());
session_start();
session_regenerate_id();
}
}
// Constants
$set_base_dir = dirname($_SERVER['SCRIPT_NAME']);
if(strlen($set_base_dir) ==1) $set_base_dir = '';
define('TIME', time());
define('BASE_DIR', $set_base_dir);
define('BASE_SITE', $protocol . $_SERVER['SERVER_NAME'] . BASE_DIR);
define('DIR_PHP', 'phpfiles/');
//etc..
define('TB_PREFIX', 'mp_'); // prefix of the tables in database
// tables for site data in mysql
$tables = [
'category'=> TB_PREFIX .'ctg',
'pages'=> TB_PREFIX .'pages'
];
Clasa model primeste 2 parametri, obiectul clasei pdo_mysqli si $tables.
Cod: Selectaţi tot
class model {
protected $obsql = false; // stores the object with connection to mysql
public $tables = []; // tables for site pages data in mysql
// receives the object with connection to MySQL (from mysqli_pdo class), and tables
function __construct($obsql, $tables) {
if(is_object($obsql)) $this->obsql = $obsql;
$this->tables = $tables;
}
//add categories, $frm = form data
public function addCtg($frm){
// take needed values for #sql, to pass as array
$val = ['name'=>$frm['name'], 'parent'=>$frm['parent']];
$sql = "INSERT INTO ". $this->tables['category'] ." (name, parent) VALUES (:name, :parent)";
//return response of sqlExec() for insert data
return $this->obsql->sqlExec($sql, $val);
}
]
Si o folosesc asa:
Cod: Selectaţi tot
include 'common.php';
include 'pdo_mysqli.php';
include 'model.php';
$obsql = new mysqli_pdo($mysql);
$modl = new model($obsql, $tables);
$modl->addCtg($_POST);