513
edits
(Created page with "{{Template开发文档}} {{Template用户手册}} {{TemplateMainPageZh}} 这是我们在Dolibarr项目中使用的语言、语法和规范的一些规则: = 版本 = * Doliba...") |
No edit summary |
||
| Line 206: | Line 206: | ||
为了与任何国家的金额要求的任何准确性兼容、与任何数据库语法兼容以及与Dolibarr升级框架兼容,我们在数据库中使用以下的字段类型: | 为了与任何国家的金额要求的任何准确性兼容、与任何数据库语法兼容以及与Dolibarr升级框架兼容,我们在数据库中使用以下的字段类型: | ||
- integer 外键 | - integer 外键 | ||
- double(24,8) 金额 | - double(24,8) 金额 | ||
- double(6,3) 税率 | - double(6,3) 税率 | ||
- real 数量 | - real 数量 | ||
- varchar 字符串 (另外,如果长度是1,char类型就越来越没用) | - varchar 字符串 (另外,如果长度是1,char类型就越来越没用) | ||
- timestamp 自动更新的日期+时间的字段 | - timestamp 自动更新的日期+时间的字段 | ||
- datetime 日期+时间的字段 | - datetime 日期+时间的字段 | ||
- date 日期 | - date 日期 | ||
- text or medium text 大字段 (不许索引) | - text or medium text 大字段 (不许索引) | ||
所有的业务规则必须位于相同的位置,进入PHP代码,不进入客户端,也不进入数据库,这就是为什么类型enum也不被允许。 | 所有的业务规则必须位于相同的位置,进入PHP代码,不进入客户端,也不进入数据库,这就是为什么类型enum也不被允许。 | ||
由于兼容性原因,其他类型是不允许的。 | 由于兼容性原因,其他类型是不允许的。 | ||
* | * 所有表都有一个前缀,以避免与其他项目的名称冲突。在当前版本中,这个前缀可以在安装Dolibarr时修改。其默认值为<tt>llx_</tt>。 | ||
== | == 主键 == | ||
表的主键必须为 <tt>rowid</tt>. | |||
一些旧表不使用此规则,并使用名为<tt>id</tt>(如[[Table llx_c_actioncomm]])的主键,但原因在于历史,并且不应该再发生这种情况。 | |||
== | == 外键 == | ||
外键的名称必须以前缀<tt>fk_</tt>开头,后跟表名(即引用表的名称)(这是为了避免项目中的重复名称,即使某些DBMS如Postgresql在不同的表中也不允许),然后是字段名称(即参考字段的名称)(这是为了允许在同一表中有几个外键)。 | |||
例如: | |||
''fk_facture_fourn_fk_soc'' | ''fk_facture_fourn_fk_soc'' 是表llx_facture_fourn中的外键,用于此表中的字段fk_soc(引用另一个表中的rowid字段) | ||
注意:如果您开发了自己的外部模块,则它必须没有指向Dolibarr标准表的外键。这将破坏标准Dolibarr升级,修复,备份和恢复工具,也可能破坏标准的功能。 | |||
== | == 替代关键字 == | ||
Sometimes, we need another unique keys than primary key. We can add in this case an alternate unique key. When we need this, we can create an alternate unique key. Such an index is called by a name that start by prefix <tt>uk_</tt> followed by an underscore, then the the table name (this is required to avoid duplicate names of unique keys that may create problems for some DBMS like Postgresql) and then another string to define the key (this is to allow to have several unique keys on same table). | Sometimes, we need another unique keys than primary key. We can add in this case an alternate unique key. When we need this, we can create an alternate unique key. Such an index is called by a name that start by prefix <tt>uk_</tt> followed by an underscore, then the the table name (this is required to avoid duplicate names of unique keys that may create problems for some DBMS like Postgresql) and then another string to define the key (this is to allow to have several unique keys on same table). | ||
例如: | |||
''uk_societe_code_client'' | ''uk_societe_code_client'' 是表llx_societe在code_client字段上的一个唯一性的关键字 | ||
== | == 索引性能 == | ||
Some fields are often used as search or order criteria, or for joins. In such case, we need to set a performance index on field to increase performances. Such indexes are named with a prefix <tt>idx_</tt> then the table name and then the field name. | Some fields are often used as search or order criteria, or for joins. In such case, we need to set a performance index on field to increase performances. Such indexes are named with a prefix <tt>idx_</tt> then the table name and then the field name. | ||
| Line 246: | Line 246: | ||
''idx_societe_user_creat'' is a performance index on table llx_societe for field user_creat | ''idx_societe_user_creat'' is a performance index on table llx_societe for field user_creat | ||
== | == SQL编码规则 == | ||
* Alias usage/Fields naming | * Alias usage/Fields naming | ||
When doing select, we can use alias to simplify writing/reading of requests: | When doing select, we can use alias to simplify writing/reading of requests: | ||
| Line 281: | Line 281: | ||
</source> | </source> | ||
例如,不要这样做: | |||
<source lang="php"> | <source lang="php"> | ||
$sql="SELECT rowid FROM table where DATEDIFF(table.datefield, NOW()) > 7"; | $sql="SELECT rowid FROM table where DATEDIFF(table.datefield, NOW()) > 7"; | ||
| Line 292: | Line 292: | ||
An other advantage of this rule, is that request benefits of index because we are making a compare of a field with a fixed value. When using datediff, you make an operation on field before comparison, this means database can't use the index on field, resulting on very bad performance compared to solution without the datediff. | An other advantage of this rule, is that request benefits of index because we are making a compare of a field with a fixed value. When using datediff, you make an operation on field before comparison, this means database can't use the index on field, resulting on very bad performance compared to solution without the datediff. | ||
== Mysql | == Mysql 特点 == | ||
* Tables must be declared with format InnoDB. | * Tables must be declared with format InnoDB. | ||
This format supports foreign keys and their restrictions, and transactions integrity is also supported. This guarantees that Dolibarr events keep all data with correct values between tables even when transaction modify different tables. | This format supports foreign keys and their restrictions, and transactions integrity is also supported. This guarantees that Dolibarr events keep all data with correct values between tables even when transaction modify different tables. | ||
| Line 332: | Line 332: | ||
</source> | </source> | ||
= HTML | = HTML 规范 = | ||
== | == 语法规则 == | ||
* HTML used must be HTML compliant and not XHTML. All attributes of HTML tags must be in lower case and quoted with ". | * HTML used must be HTML compliant and not XHTML. All attributes of HTML tags must be in lower case and quoted with ". | ||
| Line 356: | Line 356: | ||
* External scripts must be written in Perl if they can't be written in PHP. Usage of another language is not forbidden but must be argued before in the development mailing-list. | * External scripts must be written in Perl if they can't be written in PHP. Usage of another language is not forbidden but must be argued before in the development mailing-list. | ||
== | == 外部模板框架? == | ||
There is ton of frameworks to provide a templating language of an HTML page. The best and faster is just called "PHP", and because Dolibarr already depends on PHP, there is no need to introduce a dependency on a third language. | There is ton of frameworks to provide a templating language of an HTML page. The best and faster is just called "PHP", and because Dolibarr already depends on PHP, there is no need to introduce a dependency on a third language. | ||
| Line 368: | Line 368: | ||
There is a ton of other reasons to not use an external templating system and all arguments to use them are also the best argument to use PHP as our templating system. | There is a ton of other reasons to not use an external templating system and all arguments to use them are also the best argument to use PHP as our templating system. | ||
= Dolibarr | = Dolibarr 规范和代码框架 = | ||
== | == 框架代码 == | ||
To standardize the code, and to speed up the development of new components in Dolibarr, you'll find 4 skeletons fully prepared in the directory '''dev/skeletons'''. | To standardize the code, and to speed up the development of new components in Dolibarr, you'll find 4 skeletons fully prepared in the directory '''dev/skeletons'''. | ||
| Line 381: | Line 381: | ||
Note that the skeletons are also used by the PHP code generator, which is described in the development chapter of Dolibarr modules, to speed up your development. | Note that the skeletons are also used by the PHP code generator, which is described in the development chapter of Dolibarr modules, to speed up your development. | ||
== | == 日期和时区 == | ||
Dolibarr is an application that is multi-user and multi-location. It's therefore necessary to store dates in the right format. To avoid problems with conversions, the following rules should be applied: | Dolibarr is an application that is multi-user and multi-location. It's therefore necessary to store dates in the right format. To avoid problems with conversions, the following rules should be applied: | ||
| Line 394: | Line 394: | ||
* Manipulation date with PHP must be done using the Dolibarr date functions: '''dol_now(), dol_mktime(), dol_stringtotime(), dol_getdate(), dol_time_plus_duree()'''. You may also find other functions available into file '''date.lib.php'''. | * Manipulation date with PHP must be done using the Dolibarr date functions: '''dol_now(), dol_mktime(), dol_stringtotime(), dol_getdate(), dol_time_plus_duree()'''. You may also find other functions available into file '''date.lib.php'''. | ||
== UTF8/ISO | == UTF8/ISO 编码 == | ||
Dolibarr stores data in the following way: | Dolibarr stores data in the following way: | ||
* In database, data is stored in UTF8 or ISO. It depends on the database's pagecode therefore these options are set at creation time. In any case, Dolibarr's database driver (in lib/database) deals with it to convert from/to UTF8 at insertion and readout. | * In database, data is stored in UTF8 or ISO. It depends on the database's pagecode therefore these options are set at creation time. In any case, Dolibarr's database driver (in lib/database) deals with it to convert from/to UTF8 at insertion and readout. | ||
| Line 400: | Line 400: | ||
* Screen displayed web pages are UTF8 (for versions prior to 2.5.1, the deprecated $character_set parameter from conf.php file defines output format). | * Screen displayed web pages are UTF8 (for versions prior to 2.5.1, the deprecated $character_set parameter from conf.php file defines output format). | ||
== | == 浮点数,金额和计算 == | ||
With PHP, like other languages (Java for exemple), non integer data (float, real, double) are not reliable for calculation. | With PHP, like other languages (Java for exemple), non integer data (float, real, double) are not reliable for calculation. | ||
Try to make for example | Try to make for example | ||
| Line 413: | Line 413: | ||
If data manipulated is not an amount, then using MU, MT, MS has no sense, and you must use the function '''round()'''. | If data manipulated is not an amount, then using MU, MT, MS has no sense, and you must use the function '''round()'''. | ||
== | == 表的创建 == | ||
Do not create tables, on the fly, during execution by a standard user, we mean, during current usage of software. | Do not create tables, on the fly, during execution by a standard user, we mean, during current usage of software. | ||
If you create a module that uses its own table, not available in the default Dolibarr code, then take a look at tutorial [[Module development]]. It explains how to provide new tables with your module, so that, the tables will be created during module activation and not during module usage. | If you create a module that uses its own table, not available in the default Dolibarr code, then take a look at tutorial [[Module development]]. It explains how to provide new tables with your module, so that, the tables will be created during module activation and not during module usage. | ||
== | == 比较版本 == | ||
If your code need to make different things depending on Dolibarr version, you can use the following tip to detect and compare versions | If your code need to make different things depending on Dolibarr version, you can use the following tip to detect and compare versions | ||
<source lang="php"> | <source lang="php"> | ||
| Line 429: | Line 429: | ||
</source> | </source> | ||
== | == 日志 == | ||
Add logs to your code using function | Add logs to your code using function | ||
<source lang="php"> | <source lang="php"> | ||
| Line 435: | Line 435: | ||
</source> | </source> | ||
== | == 工作目录 == | ||
If you need to create a working directory, into your code, refer to it with | If you need to create a working directory, into your code, refer to it with | ||
'''DOL_DATA_ROOT.'/mymodule'''' | '''DOL_DATA_ROOT.'/mymodule'''' | ||
| Line 447: | Line 447: | ||
If you need a directory to store temporary data, this directory must be '''DOL_DATA_ROOT.'/mymodule/temp'''' | If you need a directory to store temporary data, this directory must be '''DOL_DATA_ROOT.'/mymodule/temp'''' | ||
= | = 设计模式与对象程序设计 = | ||
== | == 创建设计框 (GoF) == | ||
Design patterns defined by the Gang Of Four (see wikipédia on [[wikipedia:Design pattern|Design patters]]). | Design patterns defined by the Gang Of Four (see wikipédia on [[wikipedia:Design pattern|Design patters]]). | ||
No usage of such patterns is required. We found some objects next to Singletons or Factory but not completely compliant with syntax, this is to be compatible with PHP 4 that is not a pure object language. | No usage of such patterns is required. We found some objects next to Singletons or Factory but not completely compliant with syntax, this is to be compatible with PHP 4 that is not a pure object language. | ||
== | == 结构设计模式 (GoF) == | ||
Design patterns defined by the Gang Of Four (see wikipédia on [[wikipedia:Design pattern|Design patters]]). | Design patterns defined by the Gang Of Four (see wikipédia on [[wikipedia:Design pattern|Design patters]]). | ||
No usage of such patterns is required. | No usage of such patterns is required. | ||
== | == 行为设计模式 (GoF) == | ||
Design patterns defined by the Gang Of Four (see wikipédia on [[wikipedia:Design pattern|Design patters]]). | Design patterns defined by the Gang Of Four (see wikipédia on [[wikipedia:Design pattern|Design patters]]). | ||
No usage of such patterns is required. | No usage of such patterns is required. | ||
== | == 企业设计模式 (Martin Fowler) == | ||
=== | === 代码组织模式 === | ||
[[wikipedia:Martin Fowler|Martin Fowler]] has identified 3 ways to organize code: | [[wikipedia:Martin Fowler|Martin Fowler]] has identified 3 ways to organize code: | ||
* The '''Transaction Script''' (The source code is linear for each user action). | * The '''Transaction Script''' (The source code is linear for each user action). | ||
| Line 476: | Line 476: | ||
-> As shown in code skeletons (see previous chapter), Dolibarr uses concept of '''Table Module'''. | -> As shown in code skeletons (see previous chapter), Dolibarr uses concept of '''Table Module'''. | ||
=== | === 业务逻辑之间的通信 - data (ORM) === | ||
There are 3 ways to make links: | There are 3 ways to make links: | ||
edits