附加字段: Difference between revisions

From Dolibarr ERP CRM Wiki
Jump to navigation Jump to search
mNo edit summary
Tag: 2017 source edit
Line 11: Line 11:
{{Template用户手册}}
{{Template用户手册}}


= 附加字段 =
=附加字段=


附加字段允许将其他数据添加到Dolibarr标准数据库模式中。在第3.2版中介绍了,在大多数业务对象中实现了附加字段:
附加字段允许您向标准Dolibarr数据库模式中添加更多数据。自版本3.2开始引入,附加字段现在在大多数业务对象中得以实现:
* Thirdparties & contacts
* Users
* Events
* Proposal
* Customer Invoices
* Orders
* Supplier Proposal
* Supplier Invoices
* Supplier Orders
* Products & services
* Member & member type
* Projects & project tasks (since 3.4)
* Categories
* Expense Report


== 技术实现 ==
*合作方 & 联系人
*用户
*事件
*报价单
*客户发票
*订单
*供应商报价单
*供应商发票
*供应商订单
*产品 & 服务
*会员 & 会员类型
*项目 & 项目任务 (自 3.4 起)
*类别
*费用报销单


每个对象在数据库中都有它的表来存储附加字段的值。
==技术实现方式==


== 数据表附加字段表模块 ==
每个对象在数据库中都有一个表用于存储ExtraFields的值。
 
==数据表的附加字段表的模板==


   llx_''BaseTableNAme''_extrafields
   llx_''BaseTableNAme''_extrafields
Line 42: Line 43:




创建一个MySQL表实例,用于实现对象上的附加字段:
用于在对象上实现 ExtraFields 的MySQL定义文件示例:


   create table llx_place_room_extrafields
   create table llx_place_room_extrafields
Line 52: Line 53:
   ) ENGINE=innodb;
   ) ENGINE=innodb;


== 加载附加字段类 ==
==加载ExtraFields类==
使用附加字段需要包括
要使用ExtraFields,所有页面必须包括:
 
<syntaxhighlight lang="PHP">
<syntaxhighlight lang="PHP">
require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
</syntaxhighlight>
</syntaxhighlight>


== 加载附加字段数组于对象 ==
==将ExtraField数组加载到对象中==
<syntaxhighlight lang="PHP">
<syntaxhighlight lang="PHP">
$extrafields = new ExtraFields($db);
$extrafields = new ExtraFields($db);
Line 64: Line 66:
</syntaxhighlight>
</syntaxhighlight>


$extralabels 将是带代码和附加字段标签的数组。
$extralabels 将是一个包含extrafields代码和标签的数组。此代码必须放在类中,例如放在“write_file”函数中。
 
这个代码必须被放置到类中,例如进入函数"write_file"。


== 对象加载附加字段 ==
==在对象中加载ExtraField==


附加字段的加载,使用 $object->array_options 数组。
把ExtraFields加载到数组 $object->array_options 中。


<syntaxhighlight lang="PHP">
<syntaxhighlight lang="PHP">
Line 77: Line 77:
</syntaxhighlight>
</syntaxhighlight>


这个代码必须放在上面的后面。
此代码应放在上述代码之后。


== 显示附加字段于 PDF 或 HTML ==
==显示附加字段于 PDF 或 HTML==
加载附加字段后
加载附加字段后


Line 94: Line 94:
此处 XXX 为附加字段代码
此处 XXX 为附加字段代码


== 显示附加字段列表于 PDF or HTML ==
==显示附加字段列表于 PDF or HTML==
加载附加字段后
加载附加字段后


Line 110: Line 110:




== 调用 $object->create() 或 $object->update()之前 ==
==调用 $object->create() 或 $object->update()之前==


表单发送的附加字段的值需要取回。为此,使用此方法:
表单发送的附加字段的值需要取回。为此,使用此方法:
Line 117: Line 117:
</syntaxhighlight>
</syntaxhighlight>


== 内部编辑页面显示附加字段 ==
==内部编辑页面显示附加字段==




Line 128: Line 128:
</syntaxhighlight>
</syntaxhighlight>


== 内部显示页面显示附加字段 ==
==内部显示页面显示附加字段==


<syntaxhighlight lang="PHP">
<syntaxhighlight lang="PHP">
Line 138: Line 138:
</syntaxhighlight>
</syntaxhighlight>


== 对象类的 fetch 方法之后 ==
==对象类的 fetch 方法之后==
<syntaxhighlight lang="PHP">
<syntaxhighlight lang="PHP">
require_once(DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php');
require_once(DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php');
Line 169: Line 169:
</syntaxhighlight>
</syntaxhighlight>


== 对象类的delete方法之后 ==
==对象类的delete方法之后==
<syntaxhighlight lang="PHP">
<syntaxhighlight lang="PHP">
// Removed extrafields
// Removed extrafields
Line 191: Line 191:
</syntaxhighlight>
</syntaxhighlight>


==修复数据库 ==
==修复数据库==
Search and find definition of $listofmodulesextra add the new extrafield declaration
Search and find definition of $listofmodulesextra add the new extrafield declaration
<syntaxhighlight lang="PHP">
<syntaxhighlight lang="PHP">
Line 200: Line 200:
</syntaxhighlight>
</syntaxhighlight>


== 如何在使用模型时更改列的标签 (propals, etc.) ==
==如何在使用模型时更改列的标签 (propals, etc.)==
在该模型的PHP代码中,您可以通过"_tableau(...)"的函数来更改附加字段的标签。
在该模型的PHP代码中,您可以通过"_tableau(...)"的函数来更改附加字段的标签。

Revision as of 06:29, 12 October 2023


附加字段

附加字段允许您向标准Dolibarr数据库模式中添加更多数据。自版本3.2开始引入,附加字段现在在大多数业务对象中得以实现:

  • 合作方 & 联系人
  • 用户
  • 事件
  • 报价单
  • 客户发票
  • 订单
  • 供应商报价单
  • 供应商发票
  • 供应商订单
  • 产品 & 服务
  • 会员 & 会员类型
  • 项目 & 项目任务 (自 3.4 起)
  • 类别
  • 费用报销单

技术实现方式

每个对象在数据库中都有一个表用于存储ExtraFields的值。

数据表的附加字段表的模板

 llx_BaseTableNAme_extrafields
 rowid                     integer AUTO_INCREMENT PRIMARY KEY,
 tms                       timestamp,
 fk_object                 integer NOT NULL,
 import_key                varchar(14)


用于在对象上实现 ExtraFields 的MySQL定义文件示例:

 create table llx_place_room_extrafields
 (
   rowid                     integer AUTO_INCREMENT PRIMARY KEY,
   tms                       timestamp,
   fk_object                 integer NOT NULL,
   import_key                varchar(14)                          		-- import key
 ) ENGINE=innodb;

加载ExtraFields类

要使用ExtraFields,所有页面必须包括:

require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';

将ExtraField数组加载到对象中

$extrafields = new ExtraFields($db);
$extralabels=$extrafields->fetch_name_optionals_label($object->table_element);

$extralabels 将是一个包含extrafields代码和标签的数组。此代码必须放在类中,例如放在“write_file”函数中。

在对象中加载ExtraField

把ExtraFields加载到数组 $object->array_options 中。

$object->fetch($rowid);
$object->fetch_optionals($rowid,$extralabels);

此代码应放在上述代码之后。

显示附加字段于 PDF 或 HTML

加载附加字段后

HTML 示例

print $object->array_options ['options_XXX'];

此处 XXX 为附加字段代码

PDF 示例

$pdf->MultiCell (0,5, $outputlangs->convToOutputCharset($object->array_options ['options_XXX']),0,'L'); )

此处 XXX 为附加字段代码

显示附加字段列表于 PDF or HTML

加载附加字段后

HTML 示例

print $object->lines[$i]->array_options['options_XXX'];

此处 XXX 为附加字段代码

PDF 示例

$pdf->MultiCell (0,5, $outputlangs->convToOutputCharset($object->lines[$i]->array_options['options_XXX']),0,'L');

此处 XXX 为附加字段代码


调用 $object->create() 或 $object->update()之前

表单发送的附加字段的值需要取回。为此,使用此方法:

$ret = $extrafields->setOptionalsFromPost($extralabels,$object);

内部编辑页面显示附加字段

$reshook=$hookmanager->executeHooks('formObjectOptions',$parameters,$object,$action);    // Note that $action and $object may have been modified by hook
if (empty($reshook) && ! empty($extrafields->attribute_label))
{
      print $object->showOptionals($extrafields,'edit');
}

内部显示页面显示附加字段

$reshook=$hookmanager->executeHooks('formObjectOptions',$parameters,$object,$action);    // Note that $action and $object may have been modified by hook
if (empty($reshook) && ! empty($extrafields->attribute_label))
{
      print $object->showOptionals($extrafields);
}

对象类的 fetch 方法之后

require_once(DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php');
$extrafields=new ExtraFields($this->db);
$extralabels=$extrafields->fetch_name_optionals_label($this->table_element,true);
if (count($extralabels)>0) {
	$this->fetch_optionals($this->id,$extralabels);
}

对象类的Update方法之后

// Actions on extra fields (by external module or standard code)
// FIXME le hook fait double emploi avec le trigger !!
$hookmanager->initHooks(array('HookModuleNamedao'));
$parameters=array('socid'=>$this->id);
$reshook=$hookmanager->executeHooks('insertExtraFields',$parameters,$this,$action);    // Note that $action and $object may have been modified by some hooks
if (empty($reshook))
{
	if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
	{
            $result=$this->insertExtraFields();
            if ($result < 0)
            {
            	$error++;
            }
        }
}
else if ($reshook < 0) $error++;

对象类的delete方法之后

// Removed extrafields
if (! $error)
{
      if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
      {
      $result=$this->deleteExtraFields();
      if ($result < 0)
      {
        $error++;
        dol_syslog(get_class($this)."::delete erreur ".$errorflag." ".$this->error, LOG_ERR);
      }
   }
}

管理页

$elementtype='facture'; //Must be the $table_element of the class that manage extrafield

修复数据库

Search and find definition of $listofmodulesextra add the new extrafield declaration

$listofmodulesextra=array('societe'=>'societe','adherent'=>'adherent','product'=>'product',
			'socpeople'=>'socpeople', 'commande'=>'commande', 'facture'=>'facture',
			'commande_fournisseur'=>'commande_fournisseur', 'actioncomm'=>'actioncomm',
			'adherent_type'=>'adherent_type','user'=>'user','projet'=>'projet', 'projet_task'=>'projet_task');

如何在使用模型时更改列的标签 (propals, etc.)

在该模型的PHP代码中,您可以通过"_tableau(...)"的函数来更改附加字段的标签。