Chapter 6: Indexing, Searching, and Formatting Data
创建查找功能
首先建立表单类Square_Form_Search。然后在Catalog_ItemController里添加个新的action,叫searchAction,再给这个动作建立一个view。最后在master layout里添加一个新链接到这个新路径。有一点我想强调的是,这个部分的这些代码,最好是从书中copy,不要去到附带文件里直接抄,因为完全不同,文档里的文件是作者之后在实现全文搜索时建立的代码,取代了现在这个话题的。
上面这个例子中,我觉得很值得注意的是,这次没有去到application.ini里添加新的route。
全文搜索(Full-text Search)
这个地方我真不想花太多时间,因为又涉及到ZF里面一个叫Zend_Search_Lucene的组件,但是全文搜索在目前我的工作中并不需要。总之在跟着作者的指导操作之后,admin的界面中有一版有一个新的按钮,用来创建或更新索引:
创建成功之后:
会见到在索引路径下有如下文件被创建:
这个地方之所以我有看到最后,是因为后面部分作者有介绍通过ContextSwitchHelper来切换输出的文字模式,其中XML和JSON是预定义的模式之一。前台的全文搜索功能被切换为输出XML格式。
但是默认情况下,你将见到的输出仍然是以HTML:
按作者提示,你需要在URL参数中加上format=xml,才会将输出转为XML格式,这个部分是由ZF去自动处理的:
Chapter 7: Paging, Sorting, and Uploading Data
分页
这个东西很有用,作者首先做的是用一个比较简单的例子来介绍如何利用Doctrine_Pager(ZF框架自己也有个叫Zend_Paginator的东西,不过作者说通常分页这个功能是紧贴近数据访问层的,所以在这里为了保持一致性,还是用Doctrine自己的),通过在Square项目里加一个分页功能。这个地方的代码最好还是从书中copy,原因同上。
这个新的indexAction里比较大的不同是,当在通过Doctrine做数据库查询时,并不是像之前那样直截了当地做,而是通过一个Doctrine_Pager对象(当然之前有经过配置),于是作为这个查询的副产品,Doctrine_Pager会为我们生成一些与分页有关的数据。
成功之后:
之后要做的是给这个分页添加按照不同标准排序:
上载文件
ZF框架里有一些自带的validator和filter来处理上载的文件。
Validator名称 | 描述 |
Exists | 如果参数并非一个有效文件,那么就返回false。 |
Count | 如果上载的文件数量不在参数指定的范围内,那么就返回false。 |
Filter名称 | 描述 |
Encrypt | 将上载的文件加密。 |
好,那么现在来给Square增加上载图片的功能。在按照作者指示,将一切都准备就绪之后,重新创建一个item,并添加一张照片,得到如下结果:
从这篇文章:Zend_Form - The mimetype of file 'foto.jpg' could not be detected来看,我的服务器缺乏一些必要的东西。于是我尝试在Google里搜索“pecl install fileinfo windows apache”,结果我在PHP的官网发现:
This extension is enabled by default as of PHP 5.3.0. Before this time, fileinfo was a PECL extension but is no longer maintained there. However, versions prior to 5.3+ may use the » discontinued PECL extension.Windows users must include the bundled php_fileinfo.dll DLL file in php.ini to enable this extension.
我的PHP版本是5.3.6,所以我只需按照上面高亮部分指示操作就行了。
于是终于可以上载成功了:
通过管理员的身份通过之后,可以在前台浏览到这个项目了:
自定制配置数据
ZF框架有个组件Zend_Conf,它可以用来方便地访问配置文件,除了读以外,还可以写数据进去,目前ZF预设的支持INI和XML格式。
不论是INI还是XML格式,到了Zend_Config实例之后,都是以对象属性的形式被访问(就是用“->”),只是映射的方法有些不同。比如同一个写法:
<?php
class Sandbox_ExampleController extends Zend_Controller_Action
{
public function configAction()
{
// create configuration object
$config = new Zend_Config(array(), 1);
......
// create section
$config->calendar = array();
$config->calendar->weekStartsOn = 'Monday';
$config->calendar->highlightToday = 1;
// create subsection
$config->calendar->events = array();
$config->calendar->events->displayTitle = 1;
$config->calendar->events->displayStartTime = 1;
$config->calendar->events->displayEndTime = 0;
$config->calendar->events->displayLocation = 1;
// write data to file
$writer = new Zend_Config_Writer_Ini();
$writer->write(APPLICATION_PATH . "/configs/example.ini", $config);
// write data to file
$writer = new Zend_Config_Writer_Xml();
$writer->write(APPLICATION_PATH . "/configs/example.xml", $config);
}
}
将会生成的INI为:
[calendar]
weekStartsOn = "Monday"
highlightToday = 1
events.displayTitle = 1
events.displayStartTime = 1
events.displayEndTime = 0
events.displayLocation = 1
生成的XML为:
<?xml version="1.0"?>
<zend-config xmlns:zf="http://framework.zend.com/xml/zend-configxml/1.0/">
......
<calendar>
<weekStartsOn>Monday</weekStartsOn>
<highlightToday>1</highlightToday>
<events>
<displayTitle>1</displayTitle>
<displayStartTime>1</displayStartTime>
<displayEndTime>0</displayEndTime>
<displayLocation>1</displayLocation>
</events>
</calendar>
</zend-config>
之后按照作者的指导,在Square中加入新的配置功能就可以了。其中将会生成的文件square.ini为:
[global]
defaultEmailAddress = "kingkong@gmail.com"
logExceptionsToFile = "0"
[admin]
itemsPerPage = "5"
[user]
salesEmailAddress = "kingkong@gmail.com"
displaySellerInfo = "1"