Skip to content

Commit 6afa376

Browse files
committed
Updated documentation
1 parent fd57b01 commit 6afa376

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

docs/10. Miscellaneous.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# PHPFUI\ORM Miscellaneous
22

3+
## SQLite Support
4+
While this library is tested against SQLite, there are differences between MySQL/MariaDB syntax and SQLite syntax. Most notabliy is insertOrUpdate() and insertOrIgnore() which are not supported for SQLite. Use the custom SQL query support below instead.
5+
36
## Custom SQL Queries
47
**PHFUI\ORM** supports raw string queries for special cases or complex queries. The following static methods of [\PHPFUI\ORM](http://phpfui.com/?n=PHPFUI&c=ORM) are supported:
58

docs/2. Active Record.md

+4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ $customer->read($value);
4444
- **insertOrUpdate**()
4545
* Will try to insert the record and on a duplicate key, will update the record with the current values.
4646
* insertOrUpdate() returns the same values as insert().
47+
* If the record only consists of primary keys, then this method is equivalent to insertOrIgnore().
48+
- **insertOrIgnore**()
49+
* Will try to insert the record and on a duplicate key, will not update.
50+
* insertOrIgnore() returns the same values as insert().
4751
- **read**(int | string | array $find)
4852
* Will try to load the first record matching the values passed in. If $find is an array, each key is used as a where condition equal to the value.
4953
* If not an array, read uses $find to search by primary key.

docs/5. Virtual Fields.md

+30-5
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33

44
You can define virtual fields with get and set semantics for any **\App\Record** class. Virtual fields are evaluated before any database field, so you can override the database defaults if needed, or create new functionality.
55

6-
Every **\App\Record** class has a static $virtualFields array defined. The key of the array is the name of the virtual key. The value for each virtual field is an array of strings. The first string is the virtual field class name. Subsequent parameters are are passed the the **getValue** and **setValue** methods.
6+
Every **\App\Record** class has a static $virtualFields array defined. The key of the array is the name of the virtual key. The value for each virtual field is an array of strings. The first string is the virtual field class name. Subsequent parameters are are passed the the **getValue** and **setValue** methods.
77

8-
## One To One and Parent Relationships
8+
The **VirtualField** class has two properties that will always be defined for use by the derived class:
9+
* $currentRecord is the current record that the virtual field should be based on.
10+
* $fieldName the field name that the VirtualField object was created from. This is the key of the $virtualFields array in the Record class.
11+
12+
## One To One and Parent Related Record Relationships
913
If a field is named the same way as a corresponding table and suffixed with the proper ID, **PHPFUI\ORM** will automatically generate a One To One or parent relationship for you.
1014

1115
A child record with an ID field of the parent record automatically has a parent relationship via the build in related record functionality.
@@ -20,6 +24,29 @@ Likewise, the Invoice record has a relationship to its Order record, and from th
2024
$invoice = new \App\Record\Invoice(7);
2125
echo $invoice->order->shipper->company;
2226
```
27+
## Custom Related Record Relationships
28+
Sometimes you can't name a related record with the name of the table. For example you might have an Employee table, but yet need to have several references to different employees in the same table. You might have the following fields which are all Employee Ids:
29+
* salesPerson_id
30+
* packedBy_id
31+
* inspectedBy_id
32+
33+
You can make them all return employees with the following virtual field definitions:
34+
```php
35+
class Order extends \Tests\App\Record\Definition\Order
36+
{
37+
protected static array $virtualFields = [
38+
'salesPerson' => [\PHPFUI\ORM\RelatedRecord::class, \App\Record\Employee::class],
39+
'packedBy' => [\PHPFUI\ORM\RelatedRecord::class, \App\Record\Employee::class],
40+
'inspectedBy' => [\PHPFUI\ORM\RelatedRecord::class, \App\Record\Employee::class],
41+
];
42+
}
43+
```
44+
### Usage
45+
```php
46+
echo 'Sales Person : ' . $order->salesPerson->fullName() . "\n";
47+
echo 'Packed By : ' . $order->packedBy->initials() . "\n";
48+
echo 'Inspected By : ' . $order->inspectedBy->first_name . "\n";
49+
```
2350

2451
## Custom Virtual Fields
2552
You can write custom classes to create virtual fields on any record. Here we are adding a **gross** virtual to the OrderDetail record.
@@ -28,7 +55,7 @@ class Gross extends \PHPFUI\ORM\VirtualField
2855
{
2956
public function getValue(array $parameters) : mixed
3057
{
31-
return number_format($parentRecord->unit_price * $parentRecord->quantity - $parentRecord->discount, 2);
58+
return number_format($currentRecord->unit_price * $currentRecord->quantity - $currentRecord->discount, 2);
3259
}
3360
}
3461

@@ -92,5 +119,3 @@ foreach ($suppliers as $supplier)
92119
echo $supplier->company . "\n";
93120
}
94121
```
95-
96-

0 commit comments

Comments
 (0)