业务对象

From Dolibarr ERP CRM Wiki
Jump to navigation Jump to search


业务对象(简称‘对象’)

所有的对象都由PHP类定义,PHP类继承了类CommonObject(定义在文件core/class/commonobject.class.php)中。 每个对象都有CRUD方法,可以用来读取或保存数据到数据库中:

  • Create: create()
  • Read: fetch()
  • Update: update()
  • Delete: delete()

稍后有举例。

全部对象

类的继承

  • Societe
  • Product
  • Account
  • AccountingAccount

对象之间的联系

一些对象联系到其他对象。这是Dolibarr使用Table llx_element_element完成的。但是你不必读这个表来让对象联系到另一个对象。 以下代码,说明如何做到这一点:

// 将数组链接到与本对象相联系的的对象上。
$object->load_object_linked($object->id, $object->element);

// 然后根据需要的对象类型选择ID
$org_order_array = $object->linked_object["commande"];

// 使用
$thelinkedobjectid = $org_order_array[0];
// 新订单实例
$thelinkedobject = new Commande($object->db);
// 加载对象
$thelinkedobject->fetch($thelinkedobjectid);

举例

'合伙人'举例

下面是创建合伙人(例如客户公司)的示例:

如果我们创建一个新的脚本或外部脚本,我们必须在开始之前添加下面的代码:

include('/pathofdolibarrhtdocs/master.inc.php');

这将初始化Dolibarr环境对象 ($conf, $db, $lang 和 $user)。

如果执行操作是另一个指定的用户,则在包含文件master.inc.php之后,且调用任何创建或更新方法之前执行:

$user->fetch(0,'loginuser');//loginuser 用户进行的该操作

现在我们可以编写业务代码了。

// We declare the class we want to use
include_once('/pathofdolibarrhtdocs/societe/class/societe.class.php');

// We create an instance of object to manipulate
$mycompany = new Societe($db);

// We set properties on object
$mycompany->nom='Big Brother';
$mycompany->client=1;

// If we want that creation is tagged as done by a particular user 'loginuser',
// you must uncomment this line to load all characteristics of this user.
// If we keep the comment, Dolibarr will use null into fields that should store the create or update user.
// $user->fetch('loginuser');

// We call method to create
$id=$mycompany->create($user);

// This is to read/load an already created object
$mycompany->fetch($id);

// To update object
$mycompany->nom='Big Brother 2';
$mycompany->update($user);

注:以上代码,可以编写在card.php文件中。响应操作按键的动作。

其他例子

您可以找到其他操作Dolibarr对象的示例。 (Create, Read, Update, Delete) into directory dev/samples of Dolibarr snapshot.