|
| 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