Vậy trước hết, ta tìm hiểu xem model là gì ?. Và tại sao phải sử dụng model ?. Model là tầng xử lý những tác vụ liên quan đến tương tác cơ sở dữ liệu từ những yêu cầu của controller. Model xử lý và trả về kết quả dưới dạng một mảng dữ liệu, khi đó thông qua view ta sẽ đẩy nội dung của mảng dữ liệu ấy ra bên ngoài. Việc tách biệt tầng model có rất nhiều thuận lợi, trước là dễ quản lý sau là dễ nâng cấp và phát triển trong tương lai của mã nguồn.
Để tương tác được với Model thì trước tiên ta phải kết nối được với cơ sở dữ liệu. Vậy ta tạo 1 bảng user với các cú pháp như sau:
Mã:
CREATE TABLE user (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
username varchar(50) NOT NULL,
password char(32) NOT NULL,
level int(1) NOT NULL DEFAULT '1',
PRIMARY KEY (id)
);
INSERT INTO 'user' (username,password,level) VALUES('admin', '12345', 2);
INSERT INTO 'user' (username,password,level) VALUES('kenny', '12345', 2);
INSERT INTO 'user' (username,password,level) VALUES('jacky', '12345', 1);
INSERT INTO 'user' (username,password,level) VALUES('Lena', '12345', 1);
Mã:
resources.db.adapter = "Pdo_mysql" resources.db.params.host = "localhost" resources.db.params.username = "root" resources.db.params.password = "" resources.db.params.dbname = "qhonline"
Sau khi đã kết nối được với cơ sở dữ liệu, tiếp theo. Chúng ta sẽ tạo file User.php trong thư mục Model với nội dung như sau:
PHP Code:
<?phpclass Model_User extends Zend_Db_Table_Abstract{
protected $_name="user";
protected $_primary="id";
public function listall(){
return $this->fetchall()->toArray();
}
}Lưu ý là thư mục Models của chúng ta có s, nhưng khi định nghĩa thì chúng ta bỏ qua s và viết bình thường là Model.
Trong lớp Model_User ở trên, ta kế thừa lớp Zend_Db_Table_Abstract. Và khai báo tên bảng, tên khóa chính thông qua hai thuộc tính $_name và $_primary. Cuối cùng, ta định nghĩa phương thức listall() và dùng phương thức fetchall() để lấy toàn bộ dữ liệu vốn có trong bảng user.
Ở đây zend framework sử dụng cơ chế Active Record. Nên phương thức $this->fetchall() nó tương đương với cú pháp lặp toàn bộ dữ liệu từ câu truy vấn select * from user vậy.
Sau cùng, ta tạo lớp UserController trong file controllers/UserController.php với nội dung sau:
PHP Code:
<?phpclass UserController extends Zend_Controller_Action{
public function indexAction(){
$muser=new Model_User;
echo "<pre>";
print_r($muser->listall());
echo "</pre>";
}
}Để sử dụng được Model trong controller ta phải khởi tạo đối tượng từ lớp Model mà ta đã định nghĩa ở trên. Sau đó từ đối tượng ta lại gọi các phương thức muốn thực thi. Lệnh <Pre> ở trên được dùng để trình bày dữ liệu dạng mảng.
Chạy thử nghiệm với đường dẫn: http://localhost/zfexam/user/
Kết quả sẽ thông báo lỗi:
PHP Code:
<?phpclass Bootstrap extends Zend_Application_Bootstrap_Bootstrap{
protected function _initAutoload(){
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => dirname(__FILE__),
));
return $autoloader;
}
}Chạy thử nghiệm lại với đường dẫn: http://localhost/zfexam/user/
Kết quả sẽ hiển thị danh sách user dưới dạng mảng như ta mong đợi.
Một số phương thức thường dùng trong lớp Zend_Db_Table
1- Thao tác liệt kê và nhận dữ liệu:
Để có thể thực hiện các thao tác liệt kê dữ liệu đầy đủ và chi tiết. Chúng ta cần sử dụng phương thức:
PHP Code:
$this->select()
PHP Code:
$query=$this->select();
PHP Code:
$query->from('tên_bảng',array('cột 1','cột 2'));
PHP Code:
$query->where('cột =?','giá trị'); + Sắp xếp thông tin theo cột thuộc tính
PHP Code:
$query->order('tên_cột ASC hoặc DESC');
PHP Code:
$query->limit(vị trí bắt đầu, số record muốn hiển thị);
PHP Code:
$this->fetchall();
PHP Code:
$this->fetchall($query);
PHP Code:
$this->fetchRow();
PHP Code:
public function listuser()
{
$data=$this->select();
$data->from('user',array('username','id'));
$data->where('id > ?',1);
$data->order('username DESC');
$data->limit(3);
$data=$this->fetchAll($data);
return $data;
} 2- Thao tác thêm, xóa, sửa dữ liệu:
+ Thêm dữ liệu:
PHP Code:
$this->insert($data);
PHP Code:
public function insert_user($data){
$this->insert($data);
}
PHP Code:
$this->update($data,$where);
PHP Code:
public function update_user($data,$where){
$where="id='1'";
$this->update($data,$where);
}
PHP Code:
$this->delete($where);
PHP Code:
public function delinfo($id){
$where="id=".$id;
$this->delete($where);
}
Không có nhận xét nào:
Đăng nhận xét