业务对象
Jump to navigation
Jump to search
业务对象(简称‘对象’)
所有的对象都由PHP类定义,PHP类继承了类CommonObject(定义在文件commonobject.class.php)中。 每个对象都有CRUD方法,可以用来读取或保存数据到数据库中:
- Create: create()
- Read: fetch()
- Update: update()
- Delete: delete()
例子:
对象之间的联系
一些对象联系到其他对象。这是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 and $user)。 如果我们希望‘操作’是由将特定的用户标记的,则调用任何创建或更新方法之前必须包含文件master.inc.php。
$user->fetch(0,'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);
其他例子
您可以找到其他操作Dolibarr对象的示例。 (Create, Read, Update, Delete) into directory dev/samples of Dolibarr snapshot.