class CategoriesController extends AppController
{
var $scaffold; }
有关Scaffold,要注意一个重要的问题: Scaffold期望每个以_id结尾的filed name是一个外键并且指向一个table,table的名称和_id前方的一样(只不过是小写的)。所以,举个例子来说,如果你嵌套了分类,你最好有个列叫做parent_id。在这个版本中,最好能够命名为parentid.同样,在表中有一个外键(比如,titles table有个category_id),并且你已经合适的联结到models(查看6.2理解联结),在show/edit/newd的views中,选择的表将会和外键的表(category)一起自动的表现出来(原文:a select box will be automatically populated with the rows from the foreign table (category) in the show/edit/new views.)。在foreign model中设置$displayField来决定foreign中哪些field会被显示。继续我们的例子,category有个标题 class Title extends AppModel
{
var $displayField = ''title''; }
第六章 Models
本章内容:
1. Model Function
1.1 用户定义的Function
1.2 检索(retrieving)你的数据
1.3 保存你的数据
1.4 Model的回调(callbacks)
2. Model的变量
3. Associations
Model是什么,它是MVC模式中的M
Model做些什么。它使得domain logic和presentation分隔开,独立application logic(It separates domain logic from the presentation, isolating application logic.)
○返回特定的fields,fields由$limit(默认是50)个记录,匹配$conditions(如果有),从第$page(默认1)页开始列表,$conditions内容应该像SQL语句中的一样,比如:$cOnditions=”race = ‘wookie’ AND thermal_detonators>3”
$this->Post->findByTitle(''My First Blog Post''); $this->Author->findByLastName(''Rogers''); $this->Property->findAllByState(''AZ''); $this->Specimen->findAllByKingdom(''Animalia'');
function edit($id)
{
//Note: The property model is automatically loaded for us at $this->property.
// Check to see if we have form data
if (isset($this->params[''form''][''data''][''property'']))
{
// Here''s where we try to save our data
if ($this->property->save($this->params[''data'']))
{
//Show the user that her data has been saved
$this->flash(''Your information has been saved.'',
''/properties/edit/''.$this->params[''data''][''property''][''id''],2); exit(); }
else
{
//If the data couldn''t be validated, show the validation errors
//and repopulate form fields with submitted data
$this->set(''form'', $this->params[''data'']); $this->validateErrors($this->property); $this->render(); }
}
//If no form data was submitted, just render the edit view
$this->render(); }