### Configure Advanced PDO Settings Source: https://github.com/j4mie/idiorm/blob/master/docs/configuration.md Examples for configuring specific PDO behaviors such as error modes, driver options, and result set formatting. ```php // Enable result sets ORM::configure('return_result_sets', true); // Set PDO driver options ORM::configure('driver_options', array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8')); // Set PDO error mode ORM::configure('error_mode', PDO::ERRMODE_WARNING); ``` -------------------------------- ### Initialize Idiorm and Database Connection Source: https://github.com/j4mie/idiorm/blob/master/docs/configuration.md Demonstrates how to include the library and configure the database connection using DSN strings and authentication credentials. ```php require_once 'idiorm.php'; // SQLite connection ORM::configure('sqlite:./example.db'); // MySQL connection with credentials ORM::configure('mysql:host=localhost;dbname=my_database'); ORM::configure('username', 'database_user'); ORM::configure('password', 'top_secret'); ``` -------------------------------- ### Manage Configuration Settings Source: https://github.com/j4mie/idiorm/blob/master/docs/configuration.md Shows how to set and retrieve configuration options for the ORM, including using arrays for batch updates. ```php // Set a single option ORM::configure('setting_name', 'value_for_setting'); // Set multiple options ORM::configure(array( 'setting_name_1' => 'value_for_setting_1', 'setting_name_2' => 'value_for_setting_2' )); // Get current setting $isLoggingEnabled = ORM::get_config('logging'); ``` -------------------------------- ### Configure Custom Query Logger Source: https://github.com/j4mie/idiorm/blob/master/docs/configuration.md Registers a callback function to handle query logging. Requires the 'logging' setting to be enabled to function. ```php ORM::configure('logger', function($log_string, $query_time) { echo $log_string . ' in ' . $query_time; }); ``` -------------------------------- ### Access Underlying PDO Objects Source: https://github.com/j4mie/idiorm/blob/master/docs/configuration.md Methods for accessing the raw PDO instance or the last executed PDOStatement for low-level database operations. ```php // Access the PDO object $db = ORM::get_db(); // Access the last executed statement $statement = ORM::get_last_statement(); ``` -------------------------------- ### Accessing Model Data with Idiorm Source: https://github.com/j4mie/idiorm/blob/master/docs/models.md Demonstrates how to retrieve data from model objects using either the 'get' method or direct property access. Also shows how to get all data as an associative array using 'as_array', with an option to specify which columns to include. ```php find_one(5); // The following two forms are equivalent $name = $person->get('name'); $name = $person->name; $person = ORM::for_table('person')->create(); $person->first_name = 'Fred'; $person->surname = 'Bloggs'; $person->age = 50; // Returns array('first_name' => 'Fred', 'surname' => 'Bloggs', 'age' => 50) $data = $person->as_array(); // Returns array('first_name' => 'Fred', 'age' => 50) $data = $person->as_array('first_name', 'age'); ``` -------------------------------- ### Execute Raw SQL Queries with Placeholders in PHP Source: https://context7.com/j4mie/idiorm/llms.txt Shows how to execute custom SQL queries using `raw_query()` which returns ORM instances. Supports both positional (?) and named (:param) placeholders for safe query execution. Includes examples with joins, subqueries, and complex conditions. ```php raw_query( 'SELECT p.* FROM person p JOIN role r ON p.role_id = r.id WHERE r.name = :role', array('role' => 'admin') ) ->find_many(); // Raw query with positional parameters $orders = ORM::for_table('order') ->raw_query( 'SELECT * FROM `order` WHERE total > ? AND status = ? ORDER BY created_at DESC', array(100, 'completed') ) ->find_many(); // Complex query with joins and subqueries $stats = ORM::for_table('user') ->raw_query(' SELECT u.*, (SELECT COUNT(*) FROM order WHERE user_id = u.id) as order_count, (SELECT SUM(total) FROM order WHERE user_id = u.id) as total_spent FROM user u WHERE u.status = ? ORDER BY total_spent DESC LIMIT 10 ', array('active')) ->find_many(); ``` -------------------------------- ### Configure Primary Key Column Source: https://github.com/j4mie/idiorm/blob/master/docs/configuration.md Sets the primary key column name for all tables globally. Supports both single string identifiers and arrays for compound primary keys. ```php ORM::configure('id_column', 'primary_key'); // Compound primary key ORM::configure('id_column', array('pk_1', 'pk_2')); ``` -------------------------------- ### Filter records with inequality and pattern matching Source: https://github.com/j4mie/idiorm/blob/master/docs/querying.md Provides examples of using inequality methods (lt, gt, lte, gte) and string pattern matching methods (like, not_like) to filter database results. ```php $people_lt = ORM::for_table('person')->where_lt('age', 10)->find_many(); $people_gt = ORM::for_table('person')->where_gt('age', 5)->find_many(); $people_lte = ORM::for_table('person')->where_lte('age', 10)->find_many(); $people_gte = ORM::for_table('person')->where_gte('age', 5)->find_many(); $people_like = ORM::for_table('person')->where_like('name', '%fred%')->find_many(); $people_not_like = ORM::for_table('person')->where_not_like('name', '%bob%')->find_many(); ``` -------------------------------- ### Delete Records (delete, delete_many) in PHP Source: https://context7.com/j4mie/idiorm/llms.txt Illustrates how to remove records from the database using `delete()` for a single instance and `delete_many()` for multiple records matching specified conditions. Includes examples for complex conditions and deleting all records. ```php find_one(5); $person->delete(); // Delete multiple records matching conditions ORM::for_table('person') ->where_equal('status', 'deleted') ->delete_many(); // Delete with complex conditions ORM::for_table('session') ->where_lt('last_activity', date('Y-m-d H:i:s', strtotime('-30 days'))) ->delete_many(); // Delete all records in a table (use with caution!) ORM::for_table('temp_data')->delete_many(); ``` -------------------------------- ### Enable Query Caching Source: https://github.com/j4mie/idiorm/blob/master/docs/configuration.md Configures query caching behavior for the ORM. Enabling 'caching' stores SELECT results, while 'caching_auto_clear' ensures the cache is cleared upon save operations. ```php ORM::configure('caching', true); ORM::configure('caching_auto_clear', true); ``` -------------------------------- ### Configure Table-Specific Primary Key Overrides Source: https://github.com/j4mie/idiorm/blob/master/docs/configuration.md Maps specific table names to their respective primary key columns. This is useful when table naming conventions vary across the database schema. ```php ORM::configure('id_column_overrides', array( 'person' => 'person_id', 'role' => 'role_id', )); ``` -------------------------------- ### Configure Custom Idiorm Caching Functions (PHP) Source: https://github.com/j4mie/idiorm/blob/master/docs/configuration.md This snippet demonstrates how to set custom caching functions for Idiorm. It includes functions for storing query results, checking the cache, clearing the cache, and creating custom cache keys. These functions interact with a simple in-memory array `$my_cache`. ```php min('height'); ?> ``` -------------------------------- ### Configure and Use Multiple Database Connections in PHP Source: https://context7.com/j4mie/idiorm/llms.txt Explains how to configure and manage multiple named database connections within Idiorm. Shows how to set up default and named connections, query them explicitly, and retrieve information like the last query or connection names. ```php find_many(); // Query named connection $remote_users = ORM::for_table('user', 'remote')->find_many(); // Explicitly use default connection $users = ORM::for_table('user', ORM::DEFAULT_CONNECTION)->find_many(); // Get last query from specific connection $last_query = ORM::get_last_query('remote'); // Get all connection names $connections = ORM::get_connection_names(); ``` -------------------------------- ### Querying with PSR-1 and Default Styles Source: https://github.com/j4mie/idiorm/blob/master/docs/querying.md Demonstrates how to perform database queries using both the default underscore-based method naming and the PSR-1 compliant camelCase style. ```php // documented and default style $person = ORM::for_table('person')->where('name', 'Fred Bloggs')->find_one(); // PSR-1 compliant style $person = ORM::forTable('person')->where('name', 'Fred Bloggs')->findOne(); ``` -------------------------------- ### Execute Raw SQL Queries with Idiorm Source: https://github.com/j4mie/idiorm/blob/master/docs/querying.md Demonstrates how to execute raw SQL commands using raw_execute() and retrieve the resulting PDOStatement. This approach is recommended only when standard ORM methods are insufficient and requires manual handling of the statement object. ```php $res = ORM::raw_execute('SHOW TABLES'); $statement = ORM::get_last_statement(); $rows = array(); while ($row = $statement->fetch(PDO::FETCH_ASSOC)) { var_dump($row); } ``` -------------------------------- ### Configure Multiple Database Connections in PHP Source: https://github.com/j4mie/idiorm/blob/master/docs/connections.md Demonstrates how to configure both the default and named database connections using Idiorm. It shows setting up SQLite for the default connection and MySQL for a named connection 'remote'. ```php find_one(5); // Using default connection, explicitly $person = ORM::for_table('person', ORM::DEFAULT_CONNECTION)->find_one(5); // Using named connection $person = ORM::for_table('different_person', 'remote')->find_one(5); ``` -------------------------------- ### Perform SQL JOINs with Idiorm Source: https://github.com/j4mie/idiorm/blob/master/docs/querying.md Explains how to use join methods like join, inner_join, and left_outer_join. It highlights the recommended array-based syntax for automatic column quoting and demonstrates self-joins using table aliases. ```php $results = ORM::for_table('person')->join('person_profile', array('person.id', '=', 'person_profile.person_id'))->find_many(); $results = ORM::for_table('person') ->table_alias('p1') ->select('p1.*') ->select('p2.name', 'parent_name') ->join('person', array('p1.parent', '=', 'p2.id'), 'p2') ->find_many(); ``` -------------------------------- ### Using Result Sets for Batch Operations Source: https://github.com/j4mie/idiorm/blob/master/docs/querying.md Explains how to use result sets instead of arrays to perform efficient batch updates and iterations. ```php // Configure return_result_sets ORM::configure('return_result_sets', true); // Batch update ORM::for_table('person')->find_result_set() ->set('age', 50) ->save(); // Iterate over result set foreach(ORM::for_table('person')->find_result_set() as $record) { echo $record->name; } // Count result set echo count(ORM::for_table('person')->find_result_set()); ``` -------------------------------- ### Manage Database Transactions with Idiorm and PDO Source: https://github.com/j4mie/idiorm/blob/master/docs/transactions.md This snippet demonstrates how to access the underlying PDO object via ORM::get_db() to execute standard transaction control commands. These methods allow for atomic database operations by starting, committing, or rolling back changes as needed. ```php // Start a transaction ORM::get_db()->beginTransaction(); // Commit a transaction ORM::get_db()->commit(); // Roll back a transaction ORM::get_db()->rollBack(); ``` -------------------------------- ### Apply Equality and Inequality Filters in Idiorm Source: https://context7.com/j4mie/idiorm/llms.txt Demonstrates how to apply basic equality and inequality conditions using where(), where_equal(), and where_not_equal(). Supports single key-value pairs or associative arrays for multiple AND conditions. ```php $person = ORM::for_table('person') ->where('name', 'Fred') ->find_one(); $person = ORM::for_table('person') ->where('name', 'Fred') ->where('age', 25) ->find_one(); $people = ORM::for_table('person') ->where(array( 'name' => 'Fred', 'age' => 20, 'status' => 'active' )) ->find_many(); $people = ORM::for_table('person') ->where_not_equal('status', 'deleted') ->find_many(); ``` -------------------------------- ### Select Columns and Expressions in Idiorm Source: https://context7.com/j4mie/idiorm/llms.txt Shows how to control returned columns, apply aliases, use raw SQL expressions, and filter for distinct results. ```php $people = ORM::for_table('person') ->select('name') ->select('age') ->find_many(); $people = ORM::for_table('person') ->select('name', 'person_name') ->find_many(); $people = ORM::for_table('person') ->select_many('name', 'age', 'email') ->find_many(); $people = ORM::for_table('person') ->select_many(array('first_name' => 'name'), 'age', 'height') ->find_many(); $people = ORM::for_table('person') ->select_many('name', 'age') ->select_expr('NOW()', 'current_time') ->select_expr("CONCAT(first_name, ' ', last_name)", 'full_name') ->find_many(); $names = ORM::for_table('person') ->distinct() ->select('name') ->find_many(); ``` -------------------------------- ### Basic CRUD Operations with Idiorm Source: https://github.com/j4mie/idiorm/blob/master/README.markdown Demonstrates how to find a single record, update it, and retrieve multiple records with joins using Idiorm. It highlights the fluent interface for building queries and manipulating data. This snippet requires the Idiorm library and a configured PDO connection. ```php $user = ORM::for_table('user') ->where_equal('username', 'j4mie') ->find_one(); $user->first_name = 'Jamie'; $user->save(); $tweets = ORM::for_table('tweet') ->select('tweet.*') ->join('user', array( 'user.id', '=', 'tweet.user_id' )) ->where_equal('user.username', 'j4mie') ->find_many(); foreach ($tweets as $tweet) { echo $tweet->text; } ``` -------------------------------- ### Querying with Named Connections in PHP Source: https://github.com/j4mie/idiorm/blob/master/docs/connections.md Illustrates how to explicitly specify the connection name when querying tables using Idiorm. It also shows how to retrieve the last executed query, either for a specific connection or the most recent one across all connections. ```php find_one(5); // Using named connection $person = ORM::for_table('different_person', 'remote')->find_one(5); // Last query on *any* connection ORM::get_last_query(); // returns query on 'different_person' using 'remote' // returns query on 'person' using default by passing in the connection name ORM::get_last_query(ORM::DEFAULT_CONNECTION); ``` -------------------------------- ### Find Single Record using Idiorm (PHP) Source: https://context7.com/j4mie/idiorm/llms.txt Retrieves a single database record using Idiorm. It supports finding by primary key (single or compound) or by specified WHERE conditions. The method returns an ORM instance for the row or false if no record is found. Properties can be accessed directly or via the get() method. ```php find_one(5); // Find by condition $person = ORM::for_table('person') ->where('name', 'Fred Bloggs') ->find_one(); // Compound primary key lookup $user_role = ORM::for_table('user_role')->find_one(array( 'user_id' => 34, 'role_id' => 10 )); // Access properties if ($person) { echo $person->name; // Direct property access echo $person->get('name'); // Using get() method } ``` -------------------------------- ### Retrieving Multiple Records Source: https://github.com/j4mie/idiorm/blob/master/docs/querying.md Demonstrates how to fetch multiple records as an array using find_many() with optional filtering. ```php // Find all records $people = ORM::for_table('person')->find_many(); // Find with filter $females = ORM::for_table('person')->where('gender', 'female')->find_many(); ``` -------------------------------- ### Executing Raw Queries with Parameter Binding in PHP Source: https://github.com/j4mie/idiorm/blob/master/docs/querying.md The `raw_query` method enables the execution of completely custom SQL queries. It accepts a query string and an optional array of parameters. The query string can use either question mark or named placeholders for binding parameters. Returned instances will contain data for all selected columns. ```php raw_query('SELECT p.* FROM person p JOIN role r ON p.role_id = r.id WHERE r.name = :role', array('role' => 'janitor'))->find_many(); ?> ``` -------------------------------- ### Configure and Retrieve Query Logs Source: https://context7.com/j4mie/idiorm/llms.txt Shows how to enable query logging in Idiorm to monitor database activity. It covers retrieving the last executed query, fetching the full log, and implementing a custom logger callback for persistent storage. ```php ORM::configure('logging', true); $users = ORM::for_table('user')->where('active', 1)->find_many(); $count = ORM::for_table('order')->count(); echo ORM::get_last_query(); $log = ORM::get_query_log(); foreach ($log as $query) { echo $query . "\n"; } ORM::configure('logger', function($query, $query_time) { file_put_contents( 'query.log', sprintf("[%s] %s (%.4fs)\n", date('Y-m-d H:i:s'), $query, $query_time), FILE_APPEND ); }); ``` -------------------------------- ### Ordering and Limiting Results in Idiorm Source: https://context7.com/j4mie/idiorm/llms.txt Explains how to sort query results using ascending/descending order or expressions, and how to implement pagination using limit and offset. ```php $people = ORM::for_table('person') ->order_by_asc('name') ->find_many(); $people = ORM::for_table('person') ->order_by_desc('created_at') ->find_many(); $people = ORM::for_table('person') ->order_by_asc('last_name') ->order_by_asc('first_name') ->order_by_desc('age') ->find_many(); $people = ORM::for_table('person') ->order_by_expr('SOUNDEX(`name`)') ->find_many(); $page = 3; $per_page = 20; $people = ORM::for_table('person') ->order_by_asc('name') ->limit($per_page) ->offset(($page - 1) * $per_page) ->find_many(); ``` -------------------------------- ### Execute Raw SQL Directly (raw_execute) in PHP Source: https://context7.com/j4mie/idiorm/llms.txt Demonstrates using `raw_execute()` to run raw SQL statements directly through PDO for operations that do not return ORM instances, such as DDL (CREATE, DROP) and custom INSERT/UPDATE statements. Shows how to retrieve affected rows and fetch results. ```php ?', array('senior', 65)); $statement = ORM::get_last_statement(); echo "Updated " . $statement->rowCount() . " rows\n"; // Custom SELECT using raw_execute ORM::raw_execute('SHOW TABLES'); $statement = ORM::get_last_statement(); while ($row = $statement->fetch(PDO::FETCH_ASSOC)) { print_r($row); } ``` -------------------------------- ### Query by Primary Key in Idiorm Source: https://context7.com/j4mie/idiorm/llms.txt Demonstrates how to fetch single or multiple records using primary keys, including support for compound primary keys. ```php $person = ORM::for_table('person') ->where_id_is(5) ->find_one(); $people = ORM::for_table('person') ->where_id_in(array(1, 2, 3, 4, 5)) ->find_many(); $user_role = ORM::for_table('user_role') ->where_id_is(array('user_id' => 34, 'role_id' => 10)) ->find_one(); ``` -------------------------------- ### Apply Pattern Matching Filters with LIKE Source: https://context7.com/j4mie/idiorm/llms.txt Utilizes where_like() and where_not_like() to perform SQL pattern matching. These methods allow for wildcard searches on string columns. ```php $people = ORM::for_table('person') ->where_like('name', '%fred%') ->find_many(); $people = ORM::for_table('person') ->where_not_like('email', '%@spam.com') ->find_many(); ``` -------------------------------- ### Create and update records using create() and save() (PHP) Source: https://context7.com/j4mie/idiorm/llms.txt The `create()` method initializes a new ORM instance, which can then be populated with data and saved using the `save()` method. `save()` persists changes to the database, creating a new record if it doesn't exist or updating it if it does. The `set()` method can also be used to update multiple fields at once. ```php create(); $person->name = 'Joe Bloggs'; $person->age = 40; $person->email = 'joe@example.com'; $person->save(); // Get the auto-generated ID after insert $new_id = $person->id(); echo "Created person with ID: $new_id\n"; // Create with initial data $person = ORM::for_table('person')->create(array( 'name' => 'Jane Doe', 'age' => 30, 'email' => 'jane@example.com' )); $person->save(); // Update an existing record $person = ORM::for_table('person')->find_one(5); $person->name = 'Bob Smith'; $person->age = 20; $person->save(); // Update using set() with array $person = ORM::for_table('person')->find_one(5); $person->set(array( 'name' => 'Bob Smith', 'age' => 20, 'status' => 'active' )); $person->save(); ``` -------------------------------- ### Access Underlying PDO Instance Source: https://github.com/j4mie/idiorm/blob/master/docs/querying.md Explains how to retrieve the raw PDO object managed by Idiorm using the get_db() method. This allows developers to perform direct database operations while still leveraging Idiorm's connection and configuration management. ```php $pdo = ORM::get_db(); foreach($pdo->query('SHOW TABLES') as $row) { var_dump($row); } ``` -------------------------------- ### Creating New Model Records with Idiorm Source: https://github.com/j4mie/idiorm/blob/master/docs/models.md Details the process of creating new records in the database using Idiorm. This involves creating an empty object instance, setting its properties, saving it, and retrieving the auto-generated ID. It also shows how to use 'set_expr' for database expressions during creation. ```php create(); $person->name = 'Joe Bloggs'; $person->age = 40; $person->save(); $person = ORM::for_table('person')->create(); $person->set('name', 'Bob Smith'); $person->age = 20; $person->set_expr('added', 'NOW()'); $person->save(); ``` -------------------------------- ### Retrieving Single Records Source: https://github.com/j4mie/idiorm/blob/master/docs/querying.md Shows how to fetch a single database record using filters or by primary key, including support for compound primary keys. ```php // Find by column value $person = ORM::for_table('person')->where('name', 'Fred Bloggs')->find_one(); // Find by primary key $person = ORM::for_table('person')->find_one(5); // Find by compound primary key $person = ORM::for_table('user_role')->find_one(array( 'user_id' => 34, 'role_id' => 10 )); ``` -------------------------------- ### Grouping and Aggregates in Idiorm Source: https://context7.com/j4mie/idiorm/llms.txt Covers grouping results for aggregation and performing standard SQL aggregate functions like count, min, max, avg, and sum. ```php $counts = ORM::for_table('person') ->select('gender') ->select_expr('COUNT(*)', 'count') ->group_by('gender') ->find_many(); $total = ORM::for_table('person')->count(); $average_age = ORM::for_table('person') ->where('status', 'active') ->avg('age'); $total_sales = ORM::for_table('order') ->where('status', 'completed') ->sum('total'); ``` -------------------------------- ### Retrieve and Access Record Data Source: https://context7.com/j4mie/idiorm/llms.txt Explains various ways to access data from an ORM instance, including converting records to associative arrays, fetching specific columns, and using array or property access syntax. ```php $person = ORM::for_table('person')->find_one(5); $data = $person->as_array(); $data = $person->as_array('name', 'age'); $name = $person->get('name'); $values = $person->get(array('name', 'age', 'email')); echo $person['name']; $person['age'] = 31; echo $person->name; $person->age = 31; ``` -------------------------------- ### Apply DISTINCT keyword in Idiorm Source: https://github.com/j4mie/idiorm/blob/master/docs/querying.md Shows how to add the DISTINCT keyword to a query using the distinct() method in the chain. ```php $distinct_names = ORM::for_table('person')->distinct()->select('name')->find_many(); ``` -------------------------------- ### Executing Raw SQL Statements with PDO in PHP Source: https://github.com/j4mie/idiorm/blob/master/docs/querying.md The `raw_execute` method allows direct execution of raw SQL statements using the underlying PDO instance. This is useful for operations not directly supported by Idiorm, such as dropping tables. It maps directly to PDOStatement::execute() and requires careful handling to avoid errors. ```php ``` -------------------------------- ### Select multiple columns with Idiorm Source: https://github.com/j4mie/idiorm/blob/master/docs/querying.md Demonstrates the use of select_many and select_many_expr to retrieve multiple columns. Supports associative arrays for aliasing and chaining with other select methods. ```php $people = ORM::for_table('person')->select_many('name', 'age')->find_many(); $people = ORM::for_table('person')->select_many(array('first_name' => 'name'), 'age', 'height')->find_many(); $people = ORM::for_table('person')->select_many('name', 'age', 'height')->select_expr('NOW()', 'timestamp')->find_many(); ``` -------------------------------- ### Idiorm Configuration API Source: https://github.com/j4mie/idiorm/blob/master/docs/connections.md Configure database connections, including default and named connections, and set connection-specific parameters. ```APIDOC ## ORM::configure ### Description Configures ORM settings, including database connection strings and other parameters. Supports named connections. ### Method `ORM::configure(string $key, string|null $value = null, string|null $connection_name = null)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **key** (string) - The configuration key to set (e.g., 'sqlite:./example.db', 'username', 'password'). - **value** (string|null) - The value for the configuration key. Use `null` for the default connection string parameter when defining a new connection. - **connection_name** (string|null) - An arbitrary key identifying the named connection. Defaults to `ORM::DEFAULT_CONNECTION` if not provided or null. ### Request Example ```php // Default connection ORM::configure('sqlite:./example.db'); // A named connection ORM::configure('mysql:host=localhost;dbname=my_database', null, 'remote'); ORM::configure('username', 'database_user', 'remote'); ORM::configure('password', 'top_secret', 'remote'); ``` ### Response #### Success Response (200) No explicit return value documented for configuration. #### Response Example None ``` -------------------------------- ### Configure Idiorm Database Connection and Settings (PHP) Source: https://context7.com/j4mie/idiorm/llms.txt Configures the Idiorm ORM with database connection details (DSN, username, password) and various settings like logging, caching, and primary key column names. It accepts DSN strings, key/value pairs, or associative arrays for configuration. ```php 'mysql:host=localhost;dbname=my_database', 'username' => 'database_user', 'password' => 'top_secret', 'logging' => true, 'caching' => true, 'return_result_sets' => true, 'id_column' => 'id', 'driver_options' => array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8') )); // Custom primary key per table ORM::configure('id_column_overrides', array( 'person' => 'person_id', 'role' => 'role_id', )); // Compound primary key ORM::configure('id_column', array('pk_1', 'pk_2')); // Read configuration $isLoggingEnabled = ORM::get_config('logging'); ``` -------------------------------- ### Idiorm Connection Management Utilities Source: https://github.com/j4mie/idiorm/blob/master/docs/connections.md Utility methods for managing and inspecting database connections. ```APIDOC ## Idiorm Connection Management Utilities ### Description Provides utility methods for managing and inspecting the configured database connections. ### Methods #### `ORM::get_connection_names()` ##### Description Returns an array of all configured connection names. ##### Method `ORM::get_connection_names(): array` ##### Parameters None ##### Response Example ```php [ ORM::DEFAULT_CONNECTION, 'remote' ] ``` ### Notes - Multiple connections do not share configuration settings. - Caching needs to be enabled explicitly for each connection if desired. - Joins across different connections are not supported. ``` -------------------------------- ### Select and alias columns in Idiorm Source: https://github.com/j4mie/idiorm/blob/master/docs/querying.md Control returned columns using select and select_expr. Supports column aliasing and automatic quoting of identifiers. ```php $people = ORM::for_table('person')->select('name')->select('age')->find_many(); ``` ```php $people = ORM::for_table('person')->select('name', 'person_name')->find_many(); ``` ```php $people = ORM::for_table('person')->select('person.name', 'person_name')->find_many(); ``` ```php $people_count = ORM::for_table('person')->select_expr('COUNT(*)', 'count')->find_many(); ``` -------------------------------- ### Retrieve records as an associative array Source: https://github.com/j4mie/idiorm/blob/master/docs/querying.md Demonstrates how to fetch database records as a plain associative array instead of Idiorm objects using find_array(). This is useful for serializing data to formats like JSON. ```php $females = ORM::for_table('person')->where('gender', 'female')->find_array(); ``` -------------------------------- ### Filter records with equality conditions Source: https://github.com/j4mie/idiorm/blob/master/docs/querying.md Illustrates how to filter records using equality conditions. It demonstrates passing an associative array to the where() method to apply multiple AND conditions simultaneously. ```php $people = ORM::for_table('person') ->where(array( 'name' => 'Fred', 'age' => 20 )) ->find_many(); ``` -------------------------------- ### Constructing Raw JOIN Clauses in PHP Source: https://github.com/j4mie/idiorm/blob/master/docs/querying.md The `raw_join` method allows for specifying custom SQL fragments for JOIN clauses. It takes the SQL fragment, conditions as an array (column, operator, column), the table alias, and optional parameters for binding. This method can be chained with other query methods. It only supports question mark placeholders. ```php raw_join( 'JOIN (SELECT * FROM role WHERE role.name = ?)', array('person.role_id', '=', 'role.id'), 'role', array('role' => 'janitor')) ->order_by_asc('person.name') ->find_many(); // Creates SQL: // SELECT * FROM `person` JOIN (SELECT * FROM role WHERE role.name = 'janitor') `role` ON `person`.`role_id` = `role`.`id` ORDER BY `person`.`name` ASC ?> ``` -------------------------------- ### Manage Database Transactions with Idiorm Source: https://context7.com/j4mie/idiorm/llms.txt Demonstrates how to perform atomic database operations using Idiorm's access to underlying PDO transaction methods. It includes a try-catch block to ensure changes are committed only if all operations succeed, otherwise rolling back on failure. ```php ORM::get_db()->beginTransaction(); try { $order = ORM::for_table('order')->create(); $order->customer_id = 1; $order->total = 99.99; $order->save(); $item = ORM::for_table('order_item')->create(); $item->order_id = $order->id(); $item->product_id = 5; $item->quantity = 2; $item->save(); $product = ORM::for_table('product')->find_one(5); $product->set_expr('stock', 'stock - 2'); $product->save(); ORM::get_db()->commit(); echo "Order placed successfully\n"; } catch (Exception $e) { ORM::get_db()->rollBack(); echo "Order failed: " . $e->getMessage() . "\n"; } ``` -------------------------------- ### Add raw JOIN clause with parameter binding (PHP) Source: https://context7.com/j4mie/idiorm/llms.txt The `raw_join()` method allows for complex join conditions using raw SQL, including subqueries. It supports parameter binding for security and flexibility. This is useful when standard join syntax is insufficient. ```php raw_join( 'JOIN (SELECT * FROM role WHERE role.name = ?)', array('person.role_id', '=', 'role.id'), 'role', array('janitor') ) ->order_by_asc('person.name') ->find_many(); // SQL: SELECT * FROM `person` JOIN (SELECT * FROM role WHERE role.name = 'janitor') `role` // ON `person`.`role_id` = `role`.`id` ORDER BY `person`.`name` ASC ``` -------------------------------- ### Count database records Source: https://github.com/j4mie/idiorm/blob/master/docs/querying.md Shows how to retrieve the total count of rows for a specific table query using the count() method. ```php $number_of_people = ORM::for_table('person')->count(); ``` -------------------------------- ### Apply Raw SQL WHERE Clauses Source: https://context7.com/j4mie/idiorm/llms.txt Uses where_raw() to inject custom SQL fragments into the query. Includes parameter binding to prevent SQL injection when handling dynamic values. ```php $people = ORM::for_table('person') ->where('name', 'Fred') ->where_raw('(`age` = ? OR `age` = ?)', array(20, 25)) ->find_many(); ``` -------------------------------- ### Order query results with Idiorm Source: https://github.com/j4mie/idiorm/blob/master/docs/querying.md Methods to add ORDER BY clauses to queries. Use order_by_asc and order_by_desc for standard columns, or order_by_expr for custom SQL expressions. ```php $people = ORM::for_table('person')->order_by_asc('gender')->order_by_desc('name')->find_many(); ``` ```php $people = ORM::for_table('person')->order_by_expr('SOUNDEX(`name`)')->find_many(); ``` -------------------------------- ### Limit and offset result sets in PHP Source: https://github.com/j4mie/idiorm/blob/master/docs/querying.md The limit and offset methods are used to restrict the number of records returned and to skip a specific number of records, respectively. These are essential for implementing pagination. ```php $people = ORM::for_table('person')->where('gender', 'female')->limit(5)->offset(10)->find_many(); ``` -------------------------------- ### Idiorm Table and Connection Management Source: https://github.com/j4mie/idiorm/blob/master/docs/connections.md Methods for selecting tables and managing database connections, including explicit connection specification. ```APIDOC ## Idiorm Table and Connection Management ### Description Provides methods to start querying a table and to manage the underlying PDO connections. ### Methods #### `ORM::for_table($table_name, $connection_name = null)` ##### Description Starts a query on the specified table, optionally using a named connection. ##### Method `ORM::for_table(string $table_name, string|null $connection_name = null)` ##### Parameters - **table_name** (string) - The name of the table to query. - **connection_name** (string|null) - The name of the connection to use. Defaults to `ORM::DEFAULT_CONNECTION` if null. ##### Request Example ```php // Using default connection $person = ORM::for_table('person')->find_one(5); // Using default connection, explicitly $person = ORM::for_table('person', ORM::DEFAULT_CONNECTION)->find_one(5); // Using named connection $person = ORM::for_table('different_person', 'remote')->find_one(5); ``` #### `ORM::set_db($pdo, $connection_name = null)` ##### Description Sets a custom PDO instance for a connection. ##### Method `ORM::set_db(PDO $pdo, string|null $connection_name = null)` ##### Parameters - **pdo** (PDO) - The PDO instance to use. - **connection_name** (string|null) - The name of the connection to associate the PDO instance with. Defaults to `ORM::DEFAULT_CONNECTION`. #### `ORM::get_db($connection_name = null)` ##### Description Retrieves the PDO instance for a given connection. ##### Method `ORM::get_db(string|null $connection_name = null)` ##### Parameters - **connection_name** (string|null) - The name of the connection. Defaults to `ORM::DEFAULT_CONNECTION`. ##### Response Example ```php $default_pdo = ORM::get_db(); $remote_pdo = ORM::get_db('remote'); ``` ### Response #### Success Response (200) Methods return the ORM instance for chaining, a PDO instance, or void. #### Response Example None ``` -------------------------------- ### Check Record Status (is_dirty, is_new) in PHP Source: https://context7.com/j4mie/idiorm/llms.txt Demonstrates how to check if a record's properties have been modified since creation or last save using `is_dirty()` and if a record is new (not yet in the database) using `is_new()`. ```php find_one(5); // Check if a field was modified $person->name = 'New Name'; if ($person->is_dirty('name')) { echo "Name has been changed\n"; } // Check if object is new (not yet in database) $new_person = ORM::for_table('person')->create(); if ($new_person->is_new()) { echo "This is a new record\n"; } $new_person->save(); if (!$new_person->is_new()) { echo "Now it's saved in the database\n"; } ```