业务对象: Difference between revisions
Jump to navigation
Jump to search
(→全部对象) |
Tag: 2017 source edit |
||
| (12 intermediate revisions by 3 users not shown) | |||
| Line 1: | Line 1: | ||
<!-- BEGIN interlang links --> | |||
<!-- Do NOT edit this section | |||
Links below are automatically managed by PolyglotBot | |||
You can edit links on the English source page : Business_Objects --> | |||
[[en:Business_Objects]] | |||
[[fr:Objets_métiers]] | |||
[[es:Objetos_de_negociado]] | |||
<!-- END interlang links --> | |||
{{Template开发文档}} | {{Template开发文档}} | ||
= 业务对象(简称‘对象’)= | =业务对象(简称‘对象’)= | ||
所有业务对象都由从 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() | |||
请参阅下面的示例... | |||
=全部对象= | |||
[https://doxygen.dolibarr.org/inherits.html 类的继承] | |||
*Societe | |||
*Product | |||
*Account | |||
*AccountingAccount | |||
=业务对象之间的链接= | |||
一些业务对象相互关联。这是在 Dolibarr 中通过 [[Table llx_element_element|表llx_element_element]] 完成的。但是,您不必读取此表来检索链接到另一个表的对象。下面的代码部分显示了如何执行此操作: | |||
如果是 Dolibarr 3.1及更高版本 | |||
<source lang="php"> | |||
// 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. | |||
// 重要提示:新填充的数组是关联的。主键是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将为NULL): | |||
$invoice_id = $order->linkedObjectsIds['facture'][0] | |||
// CORRECT way ($invoice_id will hold the ID): | |||
// 正确的方式($invoice_id将保存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(处理它) | |||
} | |||
</source> | |||
如果是 Dolibarr 3.1 之前的版本 | |||
<source lang="php"> | |||
// 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); | |||
</source> | |||
<source lang="php"> | <source lang="php"> | ||
// 将数组链接到与本对象相联系的的对象上。 | // 将数组链接到与本对象相联系的的对象上。 | ||
| Line 35: | Line 106: | ||
</source> | </source> | ||
= | =示例= | ||
==创建合作方的示例== | |||
以下是创建合作方(例如客户公司)的示例: | |||
如果您正在创建一个新脚本或外部脚本,则有必要在其代码的开头执行以下操作: | |||
<source lang="php"> | <source lang="php"> | ||
include('/pathofdolibarrhtdocs/master.inc.php'); | include('/pathofdolibarrhtdocs/master.inc.php'); | ||
</source> | </source> | ||
这将初始化 Dolibarr 环境对象($conf、$db、$lang和$user对象)。如果希望在跟踪中将操作(创建、更新)与特定用户关联,则必须在 include master.inc.php 之后执行: | |||
<source lang="php"> | <source lang="php"> | ||
$user->fetch(0,'loginuser');//loginuser 用户进行的该操作 | $user->fetch(0,'loginuser');//loginuser 用户进行的该操作 | ||
| Line 79: | Line 151: | ||
$mycompany->update($user); | $mycompany->update($user); | ||
</source> | </source> | ||
'''注:以上代码,可以编写在card.php文件中。响应操作按键的动作。''' | |||
== | ==其他示例== | ||
您可以在 Dolibarr 快照的 '''dev/samples''' 目录中找到 Dolibarr 对象操作(创建、读取、修改、删除)的更多示例。 | |||
Latest revision as of 06:16, 29 November 2023
业务对象(简称‘对象’)
所有业务对象都由从 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.
// 重要提示:新填充的数组是关联的。主键是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将为NULL):
$invoice_id = $order->linkedObjectsIds['facture'][0]
// CORRECT way ($invoice_id will hold the ID):
// 正确的方式($invoice_id将保存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对象)。如果希望在跟踪中将操作(创建、更新)与特定用户关联,则必须在 include 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 快照的 dev/samples 目录中找到 Dolibarr 对象操作(创建、读取、修改、删除)的更多示例。