业务对象
Jump to navigation
Jump to search
业务对象(简称‘对象’)
所有业务对象都由从 CommonObject 类(在core/class/commonobject.class.php文件中定义)继承的PHP类定义。
要查找与您感兴趣的业务对象对应的类,请浏览Dolibarr的文件树,查找文件 */class/*.class.php,因为所有类都位于与对象相关的目录的 class 子目录中。例如:
- 对于合作方 htdocs/societe/class/societe.class.php
- 对于客户发票 htdocs/compta/facture/class/facture.class.php
- 对于供应商发票 htdocs/fourn/class/fournisseur.facture.class.php
...
每个业务类都有4种CRUD方法,用于执行数据库读取或更新:
- 创建(C表示创建):create()
- 读取(R表示读取):fetch()
- 更改(U代表更新):update()
- 删除(D代表删除):delete()
请参阅下面的示例...
全部对象
- Societe
- Product
- Account
- AccountingAccount
业务对象之间的链接
一些业务对象相互关联。这是在 Dolibarr 中通过 表llx_element_element 完成的。但是,您不必读取此表来检索链接到另一个表的对象。下面的代码部分显示了如何执行此操作:
如果是 Dolibarr 3.1及更高版本
// get invoices from an already loaded order:
$result = $order->fetchObjectLinked($order->id, 'commande', null, 'facture');
if ($result <= 0) {
// handle error
}
// important notice : the newly populated arrays are associative. Keys are llx_element_element row ids.
// now if we expect only one linked invoice:
// WRONG way to get it ($invoice_id will be NULL):
$invoice_id = $order->linkedObjectsIds['facture'][0]
// CORRECT way ($invoice_id will hold the ID):
$invoice_id = array_values($order->linkedObjectsIds['facture'])[0]
// Better (if there are more than one linked invoices) :
foreach ($order->linkedObjectsIds['facture'] as $element_element_id => $facture_id) {
// process it
}
如果是 Dolibarr 3.1 之前的版本
// This load the array linked_object with objects that link to this one
$object->load_object_linked($object->id, $object->element);
// To load array linked_object with objects that are linked by this one
$object->load_object_linked($object->id, $object->element);
// Then choose id with objects type you need
$org_order_array = $object->linked_object["commande"];
// Now use them
$thelinkedobjectid = $org_order_array[0];
// new order instance
$thelinkedobject = new Commande($object->db);
// load the object
$thelinkedobject->fetch($thelinkedobjectid);
// 将数组链接到与本对象相联系的的对象上。
$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.