// Define 'orders' table class Order extends DataObject { // ... public $customer_id; // ... function init() { parent::init(); $this->hasOne('Customer', 'customer_id'); } // ... } // Define 'customers' table class Customer extends DataObject { // ... public $id; // ... // ... }
$order = new Order(1); // Get the order with ID 1 echo $order->Customer->name; // Display the name of the customer associated with the order
// Define 'products' table class Product extends DataObject { // ... public $category_id; // ... function init() { parent::init(); $this->hasOne('Category', 'category_id'); } // ... } // Define 'categories' table class Category extends DataObject { // ... public $id; // ... // ... }
$product = new Product(1); // Get the product with ID 1 echo $product->Category->name; // Display the name of the category associated with the productThe DataObject package is a PHP library for simplified database access and manipulation. It provides an abstraction layer between PHP code and the database, allowing developers to work with databases using objects instead of SQL queries.