Bước 8: Tạo phương thức setEncoding cho class Mysql (ok)

<?php  
	require_once 'database.php';
	class Mysql {
		var $config = array();
		var $tableParameters = array(
			'charset' => array(
				'value' => 'DEFAULT CHARSET', 
				'quote' => false, 
				'join' => '=', 
				'column' => 'charset'
			), 
			'collate' => array(
				'value' => 'COLLATE', 
				'quote' => false, 
				'join' => '=', 
				'column' => 'Collation'
			), 
			'engine' => array(
				'value' => 'ENGINE', 
				'quote' => false, 
				'join' => '=', 
				'column' => 'Engine'
			)
		);
	  var $keywords = array(
	  	'>=', 
	  	'<=', 
	  	'>', 
	  	'<', 
	  	'IN', 
	  	'NOT', 
	  	'IS', 
	  	'LIKE', 
	  	'!=', 
	  	'<>'
	  );
	  var $columns = array(
	  	'primary_key' => array(
	  		'name' => 'int(11) DEFAULT NULL auto_increment'
	  	), 
	  	'string' => array(
	  		'name' => 'varchar', 
	  		'limit' => '255'
	  	), 
	  	'text' => array(
	  		'name' => 'text'
	  	), 
	  	'integer' => array(
				'name' => 'int', 
				'limit' => '11', 
				'formatter' => 'intval'
			), 
			'float' => array(
				'name' => 'float', 
				'formatter' => 'floatval'
			), 
			'datetime' => array(
				'name' => 'datetime', 
				'format' => 'Y-m-d H:i:s', 
				'formatter' => 'date'
			), 
			'timestamp' => array(
				'name' => 'timestamp', 
				'format' => 'Y-m-d H:i:s', 
				'formatter' => 'date'
			), 
			'time' => array(
				'name' => 'time', 
				'format' => 'H:i:s', 
				'formatter' => 'date'
			), 
			'date' => array(
				'name' => 'date', 
				'format' => 'Y-m-d', 
				'formatter' => 'date'
			), 
			'binary' => array(
				'name' => 'blob'
			), 
			'boolean' => array(
				'name' => 'tinyint', 
				'limit' => '1'
			)
		);
		static $instance = null;
		public function __construct($config = array()) {
	    $this->config = $config;
	    return $this->connect();
	  }
	  public static function getInstance() {
	  	$config = array(
	      'host' => DATABASE_HOST,
	      'login' => DATABASE_USERNAME,
	      'password' => DATABASE_PASSWORD,
	      'port' => DATABASE_PORT,
	      'database' => DATABASE_NAME,
	      'encoding' => 'utf8'
	    );
	    if(null === Mysql::$instance) {
	    	Mysql::$instance = new Mysql($config);
	    }
	    return Mysql::$instance;
	  }
	  function connect() {
	    $config           = $this->config;
	    $this->connected  = false;
	    $this->connection = mysqli_connect($config['host'], $config['login'], $config['password'], $config['database']);
	    if ($this->connection !== false) {
	      $this->connected = true;
	    }
	    if (!empty($config['encoding'])) {
	    	echo '<pre>';
	    		print_r($this->setEncoding($config['encoding']));
	    	echo '</pre>';
	      $this->setEncoding($config['encoding']);
	    }
	    return $this->connected;
	  }
    function setEncoding($enc) {
	    return $this->_execute('SET NAMES ' . $enc) != false;
	  }
	  function _execute($sql) {
	    if (preg_match('/^\s*call/i', $sql)) {
	      return $this->_executeProcedure($sql);
	    } else {
	      return mysqli_query($this->connection, $sql);
	    }
	  }
	  function _executeProcedure($sql) {
	    $answer      = mysqli_multi_query($this->connection, $sql);
	    $firstResult = mysqli_store_result($this->connection);
	    if (mysqli_more_results($this->connection)) {
	      while ($lastResult = mysqli_next_result($this->connection));
	    }
	    return $firstResult;
	  }
	}
?>
<?php  
if (!class_exists("Mysql")) require_once "Mysql.php";
	if(!class_exists('Form')) require_once 'Form.php';
	class AppModel {
		public $form = null;
		protected $rules = null;
		protected $db = null;
		protected $table = '';
		public function __construct() {
			$this->db = Mysql::getInstance();
			$this->form = new Form();
			$this->form->setRules($this->rules);
		}
	};
	new AppModel();
?>

Last updated