# Bước 4+: Cách đổi mảng Model thành mảng User (ok)

![](https://3988971916-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LXUV09EJOZ8BKB8YL5I%2F-M45tSczpgjMMFpLTKhJ%2F-M45znE5cv-SUie1bXmQ%2FScreenshot_25.png?alt=media\&token=e388cc8e-fc0d-46b9-93a8-d6620976c749)

![](https://3988971916-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LXUV09EJOZ8BKB8YL5I%2F-M45tSczpgjMMFpLTKhJ%2F-M45zrkCzRBhDQFWESJz%2FScreenshot_27.png?alt=media\&token=7cf3f59e-ed89-4a0b-89d7-c8277c573df3)

![](https://3988971916-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LXUV09EJOZ8BKB8YL5I%2F-M45tSczpgjMMFpLTKhJ%2F-M45zv8bYkiUnMMg4R0Y%2FScreenshot_26.png?alt=media\&token=2ea29fb2-2187-4a5e-8b95-b707c330cbcc)

![](https://3988971916-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LXUV09EJOZ8BKB8YL5I%2F-M45tSczpgjMMFpLTKhJ%2F-M45zyUnf1cmakOCk3a7%2FScreenshot_28.png?alt=media\&token=11fe903c-149e-4265-a1ba-2aa23e110cc4)

![](https://3988971916-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LXUV09EJOZ8BKB8YL5I%2F-M45tSczpgjMMFpLTKhJ%2F-M46-0L1Y9OBMwhSubl_%2FScreenshot_29.png?alt=media\&token=684ff4fe-b087-4eb4-b6b0-6ea7fee96bfa)

```
<?php  
require_once "AppModel.php";
require_once "Helper.php";
require_once "Session.php";
class  User extends AppModel {
	protected $table = 'user';
	protected $alias = 'User';
	protected $rules = array(
		"email" => array(
			"form" => array(
				"type" => "text"
			), 
			"notEmpty" => array(
				"rule" => "notEmpty", 
				"message" => MSG_ERR_NOTEMPTY
			), 
			"isEmail" => array(
				"rule" => "email", 
				"message" => MSG_ERR_EMAIL
			)
		), 
		"password" => array(
			"form" => array(
				"type" => "password"
			), 
			"notEmpty" => array(
				"rule" => "notEmpty", 
				"message" => MSG_ERR_NOTEMPTY
			)
		), 
		"fullname" => array(
			"form" => array(
				"type" => "text"
			), 
			"notEmpty" => array(
				"rule" => "notEmpty", 
				"message" => MSG_ERR_NOTEMPTY
			)
		), 
		"address" => array(
			"form" => array(
				"type" => "textarea"
			), 
			"notEmpty" => array(
				"rule" => "notEmpty", 
				"message" => MSG_ERR_NOTEMPTY
			)
		)
	);
	public function __construct() {
		parent::__construct();
	}
	public function login($data) {
		// array (
	 //  	'Model' => 
		//   array (
		//     'email' => 'admin@gmail.com',
		//     'password' => '123456',
		//   ),
		// )
    $exists = $this->find(array(
      'conditions' => array(
        'email' => $data[$this->alias]['email'],
        'password' => Helper::hash($data[$this->alias]['password'])
      )
    ), 'first');
    if (!empty($exists)) {
      $this->session->write(USER_INFO, $exists);
      $this->session->write(LOGGED_IN, true);
      return true;
    }
    return false;
  }
  public function find($conditions, $first = 'all') {
    $results = $this->db->select($this->table, $conditions);
    if (!empty($results) && $first == 'first') {
      return $results[0];
    }
    return $results;
  }
  function select($myTable, $options = array(), $isCount = false) {
    // Get options by parameters
    $myFields   = isset($options['fields']) ? $options['fields'] : '*';
    $conditions = isset($options['conditions']) ? $options['conditions'] : null;
    $orders     = isset($options['orders']) ? $options['orders'] : null;
    $groups     = isset($options['groups']) ? $options['groups'] : null;
    $mJoins     = isset($options['joins']) ? $options['joins'] : null;
    $mlimit     = isset($options['limit']) ? $options['limit'] : false;
    $moffset    = isset($options['offset']) ? $options['offset'] : false;
    try {
      $returnArr = array();
      $table     = $myTable;
      $alias     = $joins = $order = $group = $limit = "";
      $fields    = "";
      if (is_array($myFields)) {
        $fields = implode(', ', $myFields);
      } else {
        $fields = $myFields;
      }
      if (isset($mJoins) && is_array($mJoins)) {
        foreach ($mJoins as $jTable => $join) {
          if (empty($jTable) || empty($join['join_key']) || empty($join['main_key']) || !isset($join['join_fields']) || !isset($join['join_fields'][1]))
            continue;
          $fields .= ", " . $jTable . "." . $join['join_fields'][0] . ", " . $jTable . "." . $join['join_fields'][1];
        }
      }
      $conditions = $this->buildConditions($conditions);
      $order      = $this->buildOrders($orders);
      $group      = $this->buildGroups($groups);
      $joins      = $this->buildJoins($myTable, $mJoins);
      $tmpTable   = explode('_', $table);
      $alias      = array();
      foreach ($tmpTable as $tmp) {
        $alias[] = ucfirst($tmp);
      }
      $alias = implode($alias);
      $query = compact('table', 'alias', 'joins', 'fields', 'conditions', 'joins', 'group', 'order', 'limit');
      $sql   = $this->renderStatement('select', $query);
      $sql   = $this->setLimit($sql, $mlimit, $moffset);
      if ($isCount)
        $returnArr = $this->fetchRow($sql);
      else
        $returnArr = $this->fetchAll($sql);
    }
    catch (Exception $ex) {
      var_dump($ex);
    }
    return $returnArr;
  }
}
```

```
<?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 = '';
		protected $alias = null;
		public function __construct() {
			$this->db = Mysql::getInstance();
			$this->form = new Form();
			$this->form->setModel($this->alias);
			$this->form->setRules($this->rules);
		}
	};
?>
```

```
<?php  
	class Form {
		private $model = 'Model';
		protected $rules = null;
		public $inputs = array();
		private $errors = null;
		public function input($name) {
    	$type = 'text';
	    foreach ($this->inputs as $field => $input) {
	      if ($field == $name) {
	        $type  = $input['type'];
	        $style = isset($input['style']) ? ' style="' . $input['style'] . '"' : '';
	        switch ($type) {
	          case 'text':
	            $inputField = '<input type="text"' . $style . ' name="data[' . $this->model . '][' . $name . ']"' . ' value="' . (isset($this->data[$this->model][$name]) ? $this->data[$this->model][$name] : '') . '" />';
	            break;
	          case 'password':
	            $inputField = '<input type="password"' . $style . ' name="data[' . $this->model . '][' . $name . ']"' . ' value="' . (isset($this->data[$this->model][$name]) ? $this->data[$this->model][$name] : '') . '" />';
	            break;
	        }
	      }
	    }
	    echo $inputField;
	  }
	  public function setRules($rules) {
	    if (!empty($rules)) {
	      $this->rules = $rules;
	      foreach ($rules as $field => $rule) {
	        if (isset($rule['form'])) {
	          $this->inputs[$field] = $rule['form'];
	        }
	      }
	    }
	  }
	  public function error($field) {
	    if (!empty($this->errors[$field])) {
	      echo '<p class="err">' . $this->errors[$field] . '</p>';
	    }
	  }
	  public function setModel($model) {
	    $this->model = $model;
	  }
	}
?>
```
