CakePHP框架之Scaffolding

发布人:kimi  时间:2007-10-29 21:21  来源: 网络转载  查看: 440次   TAG: CakePHP框架  Scaffolding 



注意


Scaffolding是一个很棒的途径,使得早期开发的部分web应用能够运行起来。早期的数据库模式是不稳定的,很容易变化。Scaffolding有个下降趋势:web程序员憎恨创建以后可能根本用不到的forms。为了减少程序员的这种重复劳动, Cake中包含了Scaffolding。Scaffolding分析数据库,创建一些标准的使用add、delete、和edit按钮的lists,创建输入的forms,以及查看数据库中一个item的标准views。为了在程序中的controller中添加Scaffolding,需要添加$scaffold变量: Cake's scaffolding is pretty cool. So cool that you'll want to use it in production apps. Now, we think its cool, too, but please realize that scaffolding is... well... just scaffolding. Its a bunch of stuff you throw up real quick during the beginning of a project in order to get started. It isn't meant to be completely flexible. So, if you find yourself really wanting to customize your logic and your views, its time to pull your scaffolding down in order to write some code.


Scaffolding is a great way of getting the early parts of developing a web application started. Early database schemas are volatile and subject to change, which is perfectly normal in the early part of the design process. This has a downside: a web developer hates creating forms that never will see real use. To reduce the strain on the developer, scaffolding has been included in Cake. Scaffolding analyzes your database tables and creates standard lists with add, delete and edit buttons, standard forms for editing and standard views for inspecting a single item in the database. To add scaffolding for your application, in the controller, add the $scaffold variable:

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有个标题One important thing to note about scaffold: it expects that any field name that ends with _id is a foreign key to a table which has a name that precedes the underscore. So, for example, if you have nested categories, you'd probably have a column called parent_id. With this release, it would be best to call this parentid. Also, when you have a foreign key in your table (e.g. titles table has category_id), and you have associated your models appropriately (see Understanding Associations, 6.2), a select box will be automatically populated with the rows from the foreign table (category) in the show/edit/new views. To set which field in the foreign table is shown, set the $displayField variable in the foreign model. To continue our example of a category having a title:

class Title extends AppModel 
{
    var $name = 'Title';

    var $displayField = 'title';
}

相关阅读

本月热门