Difference between revisions of "ビジネス オブジェクト"

From Dolibarr ERP CRM Wiki
Jump to navigation Jump to search
(WIP)
 
(日本語本文)
 
Line 2: Line 2:
 
=ビジネスオブジェクト(Business objects)=
 
=ビジネスオブジェクト(Business objects)=
 
すべての ビジネスオブジェクト は、PHPのクラスであり、 CommonObject (定義ファイルは、 commonobject.class.php) を継承する.
 
すべての ビジネスオブジェクト は、PHPのクラスであり、 CommonObject (定義ファイルは、 commonobject.class.php) を継承する.
それぞれの ビジネスオブジェクト は 4つの CRUD メソッドを持ち、データベースとの読込と書込に使用できる:
+
それぞれの ビジネスオブジェクト は 4つの CRUD メソッドを持ち、データベースとの読み書きに使用できる:
  
 
*作成(Create): create()
 
*作成(Create): create()
 
*読込(Read): fetch()
 
*読込(Read): fetch()
 
*更新(Update): update()
 
*更新(Update): update()
*書込(Delete): delete()
+
*削除(Delete): delete()
  
 
後述の具体例を参照...
 
後述の具体例を参照...
  
 
=ビジネスオブジェクト間のリンク=
 
=ビジネスオブジェクト間のリンク=
Some business objects are linked to other business objects. This is done in Dolibarr using the [[Table llx_element_element]]. But you don't have to read this table to get objects linked to another.
+
ビジネスオブジェトの中には、他のビジネスオブジェクトとリンクしているものもある。Dolibarr においては、テーブル [[Table llx_element_element]] を使うことで実現している. しかし他とリンクしているオブジェクトを取得するために、このテーブルを読む必要はない.
  
Here is how you can use this (Dolibarr 3.1 and higher):
+
ここで使い方を示す (Dolibarr 3.1 以上):
 
<source lang="php">
 
<source lang="php">
 
// get invoices from an already loaded order:
 
// get invoices from an already loaded order:
Line 37: Line 37:
 
</source>
 
</source>
  
Prior to Dolibarr < 3.1:
+
Dolibarr 3.1 より旧版:
 
<source lang="php">
 
<source lang="php">
 
// This load the array linked_object with objects that link to this one
 
// This load the array linked_object with objects that link to this one
Line 55: Line 55:
 
</source>
 
</source>
  
=具体例=
+
=事例=
  
==取引先に対する具体例==
+
==取引先に対する事例==
Here is an example for the creation of a third party (eg customer company):
+
ここで取引先の作成に関する事例を挙げる (例 顧客法人):
  
If we make a new script or an external script, we must add at the beginning, before making anything else:
+
新スクリプトや外部スクリプトを作る時、最初に他のものを作る前に:
 
<source lang="php">
 
<source lang="php">
 
include('/pathofdolibarrhtdocs/master.inc.php');
 
include('/pathofdolibarrhtdocs/master.inc.php');
Line 70: Line 70:
 
</source>
 
</source>
  
Now we can make our business code.
+
これで、ビジネスコードを作成することができる.
  
 
<source lang="php">
 
<source lang="php">
Line 99: Line 99:
 
</source>
 
</source>
  
==その他の具体例==
+
==その他の事例==
You may find other example for manipulating Dolibarr objects (Create, Read, Update, Delete) into directory '''dev/samples''' of Dolibarr snapshot.
+
他の事例を見つけるには、Dolibarr スナップショットの '''dev/samples''' ディレクトリを見るといいかも。そこには、Dolibarr オブジェクトの操作事例 (作成, 読込, 更新, 削除) があるはず .

Latest revision as of 10:08, 26 April 2023

ビジネスオブジェクト(Business objects)

すべての ビジネスオブジェクト は、PHPのクラスであり、 CommonObject (定義ファイルは、 commonobject.class.php) を継承する. それぞれの ビジネスオブジェクト は 4つの CRUD メソッドを持ち、データベースとの読み書きに使用できる:

  • 作成(Create): create()
  • 読込(Read): fetch()
  • 更新(Update): update()
  • 削除(Delete): delete()

後述の具体例を参照...

ビジネスオブジェクト間のリンク

ビジネスオブジェトの中には、他のビジネスオブジェクトとリンクしているものもある。Dolibarr においては、テーブル Table 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);

事例

取引先に対する事例

ここで取引先の作成に関する事例を挙げる (例 顧客法人):

新スクリプトや外部スクリプトを作る時、最初に他のものを作る前に:

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

This will initialize Dolibarr environment objects ($conf, $db, $lang and $user). If we want that actions (create, update) are tagged with a particular user as the create/update user, we must do after the include of master.inc.php and before calling any create or update method

$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 スナップショットの dev/samples ディレクトリを見るといいかも。そこには、Dolibarr オブジェクトの操作事例 (作成, 読込, 更新, 削除) があるはず .