[CakePHP] Model

I- Model là gì ?
- Model đại diện cho dữ liệu và được sử dụng trong các ứng dụng CakePHP cho việc truy xuất dữ liệu.

- 1 model thường đại diện cho 1 table nhưng có thể được dùng để truy cập bất cứ loại lưu trữ dữ liệu nào như : files, LDAP, CSV....

- 1 model có thể được kết hợp với các model khác.


II- Code
- giả sử chúng ta có 1 table với tên là 'recipes'( cột id, title, body ) thì tên của model sẽ là : Recipe ở trong file: /app/models/recipe.php
<?php
class Recipe extends AppModel {

public $name = 'Recipe';

}
?>

III- Plugin của model

- Nếu là một plugin của model, thì bạn nên đặt nó trong thư mục /app/plugins/recipes/models/

- một ví dụ về plug in của model
<?php
class RecipesController extends AppController {

public $uses = array(
'Recipe', // 'Recipes.Recipe' if it is from Recipes plugin
);

public function view($id) {
// retrieve record with ID 123 from recipes table
$recipe = $this->Recipe->findById(123);

// set the $recipe variable so it can be used by views later
$this->set('recipe', $recipe); // or $this->set(compact('recipe'));
}

}
?>

================================================
---
name: hoang nguyen
nickname: mathhoang
Y!M: vietnam_hoangminhnguyen@yahoo.com
mail: vietnam_hoangminhnguyen@Yahoo.com
site: http://tech-vnit.tk or kattyflea.co.cc
blog: mathhoang.tk or mathhoang.blogspot.com
================================================

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...