Skip to content

Commit 3f1c54c

Browse files
committed
More basic examples
1 parent a97e7a9 commit 3f1c54c

File tree

3 files changed

+88
-2
lines changed

3 files changed

+88
-2
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ $pdo = new \PHPFUI\ORM\PDOInstance($yourConnectionString);
3333
\PHPFUI\ORM::addConnection($pdo);
3434
```
3535

36+
See some basic usage examples in [scripts/examples.php](<https://github.com/phpfui/ORM/blob/main/scripts/examples.php>)
37+
3638
### Active Record Example
3739
```php
3840
$book = new \App\Record\Book();

composer.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"psr/log": "^3.0"
1818
},
1919
"require-dev": {
20-
"phpunit/phpunit": "<=11.99",
20+
"phpunit/phpunit": "<=12.0",
2121
"phpfui/phpunit-syntax-coverage": "^1.0",
2222
"roave/security-advisories": "dev-latest",
2323
"friendsofphp/php-cs-fixer": "*",
@@ -28,6 +28,9 @@
2828
"psr-4": {"PHPFUI\\": "src/PHPFUI/"}
2929
},
3030
"autoload-dev": {
31-
"psr-4": {"Fixtures\\": "tests/Fixtures/"}
31+
"psr-4": {
32+
"Fixtures\\": "Tests/Fixtures/",
33+
"Tests\\App\\": "Tests/App/"
34+
}
3235
}
3336
}

scripts/examples.php

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
/**
4+
* Usage examples
5+
*
6+
* You must run unit tests first to generate the models
7+
*/
8+
9+
include __DIR__ . '/../vendor/autoload.php';
10+
11+
// create the PDO object to use
12+
$pdo = new \PHPFUI\ORM\PDOInstance('sqlite:' . __DIR__ . '/../northwind/northwind.db');
13+
14+
// register the PDO object with the ORM
15+
\PHPFUI\ORM::addConnection($pdo);
16+
17+
// Set model directories
18+
\PHPFUI\ORM::$namespaceRoot = __DIR__ . '/..';
19+
\PHPFUI\ORM::$recordNamespace = 'Tests\\App\\Record';
20+
\PHPFUI\ORM::$tableNamespace = 'Tests\\App\\Table';
21+
\PHPFUI\ORM::$migrationNamespace = 'Tests\\Fixtures\\Migration';
22+
23+
// specify the related record id format
24+
\PHPFUI\ORM::$idSuffix = '_id';
25+
26+
// add English translations for the error messages
27+
\PHPFUI\ORM::setTranslationCallback(\PHPFUI\Translation\Translator::trans(...));
28+
\PHPFUI\Translation\Translator::setTranslationDirectory(__DIR__ . '/../translations');
29+
\PHPFUI\Translation\Translator::setLocale('en_US');
30+
31+
// Simple select example
32+
$customerTable = new \Tests\App\Table\Customer();
33+
34+
echo "\n\nCustomers in default data order:\n\n";
35+
foreach ($customerTable->getRecordCursor() as $customer)
36+
{
37+
echo "{$customer->first_name} {$customer->last_name}, {$customer->job_title}\n";
38+
}
39+
40+
$customerTable->addOrderBy('last_name');
41+
echo "\n\nCustomers in last_name order:\n\n";
42+
foreach ($customerTable->getRecordCursor() as $customer)
43+
{
44+
echo "{$customer->first_name} {$customer->last_name}, {$customer->job_title}\n";
45+
}
46+
47+
$customerTable->addOrderBy('last_name', 'desc');
48+
$customerTable->setLimit(10);
49+
50+
echo "\n\nLast 10 Customers in last_name order:\n\n";
51+
foreach ($customerTable->getRecordCursor() as $customer)
52+
{
53+
echo "{$customer->first_name} {$customer->last_name}, {$customer->job_title}\n";
54+
}
55+
56+
$customerTable->setLimit(0);
57+
$customerTable->addGroupBy('company');
58+
$customerTable->addSelect(new \PHPFUI\ORM\Literal('count(*)'), 'count');
59+
$customerTable->addSelect('company');
60+
$customerTable->setOrderBy('company');
61+
echo "\n\nCount of Customers by company:\n\n";
62+
63+
foreach ($customerTable->getDataObjectCursor() as $customer)
64+
{
65+
echo "{$customer->company}: {$customer->count}\n";
66+
}
67+
68+
69+
$orderTable = new \Tests\App\Table\Order();
70+
// addJoin defaults to using the primary key of the related table
71+
$orderTable->addJoin('customer');
72+
$orderTable->addJoin('employee');
73+
$orderTable->addJoin('order_status');
74+
75+
echo "\n\nOrders with customer, employee and order status joins:\n\n";
76+
foreach ($orderTable->getDataObjectCursor() as $order)
77+
{
78+
echo "Order {$order->order_id}: Sold to: {$order->company} Sold by: {$order->first_name} {$order->last_name}, Status: {$order->order_status_name}\n";
79+
}
80+
81+

0 commit comments

Comments
 (0)