译 PrestaShop开发者指南 第四篇 深入PrestaShop核心开发

## 访问数据库

### 数据库结构

PrestaShop的数据库表默认带有ps_的前缀,前缀在安装时可以自定义。

所有表名都是小写,以下划线分割。当一个表表示要在两个实体间建立连接时,表名中两个实体的的名称都要出现,比如ps_category_product表示将产品关联到对应的分类。

几点细节:
- 用id_lang字段来存储与一条记录相关的语言
- 用id_shop字段来存储与一条记录相关的店铺
- 以_lang后缀结尾的表表示包含翻译,如ps_product_lang表包含ps_product表的所有翻译
- 以_shop后缀结尾的表表示链接到指定店铺的记录,如ps_category_shop表包含每个分类对应在哪个店铺上

### ObjectModel 类

ObjectModel是一个AR类型的类,它的一个实例对应一条数据库记录。

当你继承了这个类以后,首先要定义模型的基本信息。


/**
* Example from the CMS model (CMSCore)
*/
public static $definition = array(
'table' => 'cms',
'primary' => 'id_cms',
'multilang' => true,
'fields' => array(
'id_cms_category' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
'position' => array('type' => self::TYPE_INT),
'active' => array('type' => self::TYPE_BOOL),
// Lang fields
'meta_description' =>
array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 255),
'meta_keywords' =>
array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 255),
'meta_title' =>
array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => true, 'size' => 128),
'link_rewrite' =>
array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isLinkRewrite', 'required' => true, 'size' => 128),
'content' =>
array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isString', 'size' => 3999999999999),
),
);

'multilang' => true,
'multishop' => true,
'multilang_shop' => true,

主要调用方法:

Method name and parameters
Description

__construct($id = NULL, $id_lang = NULL)

Build object.

add($autodate = true, $nullValues = false)

Save current object to database (add or update).

associateTo(integer|array $id_shops)

Associate an item to its context.

delete()

Delete current object from database.

deleteImage(mixed $force_delete = false)

Delete images associated with the object.

deleteSelection($selection)

Delete several objects from database.

getFields()

Prepare fields for ObjectModel class (add, update).

getValidationRules($className = _CLASS_)

Return object validation rules (field validity).

save($nullValues = false, $autodate = true)

Save current object to database (add or update).

toggleStatus()

Toggle object's status in database.

update($nullValues = false)

Update current object to database.

validateFields($die = true, $errorReturn = false)

Check for field validity before database interaction.

### DBQuery类

DBQuery是是一个用于构造SQL 查询语句的助手类。


$sql = new DbQuery();
$sql->select('*');
$sql->from('cms', 'c');
$sql->innerJoin('cms_lang', 'l', 'c.id_cms = l.id_cms AND l.id_lang = '.(int)$id_lang);
$sql->where('c.active = 1');
$sql->orderBy('position');
return Db::getInstance()->executeS($sql

译注:这个类的方法很基本,跟国产的比差了一截,但一般的应用够了。

## 路由分发

路由分发是v1.5引入的一项特性。

如下,当未启用Url重写时,


http://myprestashop.com/index.php?controller=category&id_category=3&id_lang=1
http://myprestashop.com/index.php?controller=product&id_product=1&id_lang=2

启用重写后:


http://myprestashop.com/en/3-music-ipods
http://myprestashop.com/fr/1-ipod-nano.html

路由分发器使用三个抽象类: Controller, FrontController 及d AdminController (后两个继承自第一个)。


可以通过重载loadRoutes()方法来创建新的路由。在后台界面的"Preferences"菜单下访问"SEO & URLs"页面就可以改变控制器的URL。

## 控制器

MVC的模式在此跳过不谈。

所有的控制器实际上都重载了Controller类,如AdminController, ModuleAdminController, FrontController, ModuleFrontController等。

### FrontController类

主要属性列表:


Property
Description
$template Template name for page content.
$css_files Array list of CSS files.
$js_files Array list of JavaScript files.
$errors Array of errors that have occurred.
$guestAllowed Whether a customer who has signed out can access the page.
$initialized Whether the init() function has been called.
$iso The ISO code of the currently selected language.
$n The number of items per page.
$orderBy The field used to sort.
$orderWay Whether to sort is ascending or descending ("ASC" or "DESC").
$p The current page number.
$ajax If the ajax parameter is detected in request, set this flag to true.

### 控制器内部函数执行顺序

1. __contruct(): Sets all the controller's member variables.
1. init(): Initializes the controller.
1. setMedia() or setMobileMedia(): Adds all JavaScript and CSS specifics to the page so that they can be combined, compressed and cached (see PrestaShop's CCC tool, in the back-office "Performance" page, under the "Advanced preferences" menu).
1. postProcess(): Handles ajaxProcess.
1. initHeader(): Called before initContent().
1. initContent(): Initializes the content.
1. initFooter(): Called after initContent().
1. display() or displayAjax(): Displays the content.

### 系统已有的控制器

控制器都在/controllers目录下,自己去看。

### 重载控制器

系统自带的控制器都带了一个Core的后缀,如


文件 /controllers/CategoryController.php
类名: CategoryControllerCore

要重载该控制器的话,


文件: /override/controllers/front/CategoryController.php
类名: CategoryController

## 视图

使用的Smarty引擎,文件扩展名.tpl。


## Cookies

统一使用/classes/Cookie.php中的类进行Cookie的读写。


$this->context->cookie->variable;

前端用户存储的cookie:



Token
Description
viewed The IDs of recently viewed products as a comma-separated list.
passwd The MD5 hash of the _COOKIE_KEY_ in config/settings.inc.php and the password the customer used to log in.
logged Whether the customer is logged in.
last_visited_category The ID of the last visited category of product listings.
id_wishlist The ID of the current wishlist displayed in the wishlist block.
id_lang The ID of the selected language.
id_guest The guest ID of the visitor when not logged in.
id_customer The customer ID of the visitor when logged in.
id_currency The ID of the selected currency.
id_connections The connection ID of the visitor's current session.
id_cart The ID of the current cart displayed in the cart block.
email The email address that the customer used to log in.
date_add The date and time the cookie was created (in YYYY-MM-DD HH:MM:SS format).
customer_lastname The last name of the customer.
customer_firstname The first name of the customer.
checksum The Blowfish checksum used to determine whether the cookie has been modified by a third party.
The customer will be logged out and the cookie deleted if the checksum doesn't match.
checkedTOS Whether the "Terms of service" checkbox has been ticked (1 if it has and 0 if it hasn't)
ajax_blockcart_display Whether the cart block is "expanded" or "collapsed".


后台用户存储的cookie:

Token
Description
date_add The date and time the cookie was created (in YYYY-MM-DD HH:MM:SS format).
id_lang The ID of the selected language.
id_employee The ID of the employee.
lastname The last name of the employee.
firstname The first name of the employee.
email The email address the employee used to log in.
profile The ID of the profile that determines which tabs the employee can access.
passwd The MD5 hash of the _COOKIE_KEY_ in config/settings.inc.php and the password the employee used to log in.
checksum The Blowfish checksum used to determine whether the cookie has been modified by a third party.
If the checksum doesn't match, the customer will be logged out and the cookie is deleted .


## 钩子

钩子是将你的代码与一些特定PrestaShop事件进行关联的机制。

主要的钩子有:


Hook name
Description
displayFooter
Displays the content in the page's footer area.
displayHeader Displays the content in the page's header area.
displayHome Displays the content in the page's central area.
displayLeftColumn Displays the content in the page's left column.
displayRightColumn Displays the content in the page's right column.
displayTop Displays the content in the page's top area.

### 使用钩子

在控制器中,


$this->context->smarty->assign('HOOK_LEFT_COLUMN', Module::hookExec('displayLeftColumn'));

在模块中,


public function hookDisplayNameOfHook($params)
{
// Your code.
}

为了让模块响应钩子的调用,要在模块安装代码中将钩子注册到PrestaShop中,


public function install()
{
return parent::install() && $this->registerHook('NameOfHook');
}

在视图中调用钩子,很容易,


{hook h='displayLeftColumn' mod='blockcart'}

### 创建自己的钩子

就是像上面模块安装代码中的一样,你想注册自己的钩子到系统中,只要简单的调用:


$this->registerHook('NameOfHook');

即可。他等同于:


INSERT INTO `ps_hook` (`name`, `title`, `description`) VALUES ('nameOfHook', 'The name of your hook', 'This is a custom hook!');

你可能感兴趣的:(译 PrestaShop开发者指南 第四篇 深入PrestaShop核心开发)