附加字段

From Dolibarr ERP CRM Wiki
Jump to navigation Jump to search


附加字段

附加字段允許您向標準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 將是一個包含擴展字段的code和label的數組。此代碼必須放在類中,例如放在「write_file」函數中。

加載對象的ExtraField值

把ExtraFields加載到數組 $object->array_options 中。

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

此代碼應放在上述代碼之後。

以PDF或HTML格式顯示ExtraField值

加載ExtraFields後

HTML示例

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

此處 XXX 是extrafields代碼

PDF示例

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

此處 XXX 是extrafields代碼

以PDF或HTML格式顯示行的ExtraField值

加載ExtraFields後

HTML示例

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

此處 XXX 是extrafields代碼

PDF示例

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

此處 XXX 是extrafields代碼

如何在使用模板(propals等)時更改列的標籤

在模板的PHP代碼中,您可以通過修改名為「_tableau(...)」的函數來更改擴展字段的標籤。

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

必須檢索表單發送的extrafields值。為此,請使用以下方法:

$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

恢復數據庫

搜索並查找$listofmodulesextra的定義添加新的extrafield語句

$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');

相關視頻

Extrafields: (法語配音,但有字幕,並以法語校閱)