-
Notifications
You must be signed in to change notification settings - Fork 12
Refresh README for current functionality #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,201 +1,275 @@ | ||||||
| Model | ||||||
| ===== | ||||||
| Model ORM | ||||||
| ========= | ||||||
|
|
||||||
| [](https://scrutinizer-ci.com/g/freshsauce/model-orm-php/build-status/master) | ||||||
|
|
||||||
| PHP Model class which provides | ||||||
|
|
||||||
| * table column/property mapping, | ||||||
| * CRUD | ||||||
| * dynamic finders on a database table | ||||||
| * dynamic counters on a database table | ||||||
| * raw database queries with escaped parameters | ||||||
| * helpers for finding one or more records returning rows as instances of the Model class | ||||||
| * throws exception on query error | ||||||
| * Requires PHP >= 8.3 | ||||||
| * Supports MySQL/MariaDB and PostgreSQL via PDO | ||||||
|
|
||||||
| Usage | ||||||
| ===== | ||||||
|
|
||||||
| With a MySQL database as such on your localhost | ||||||
| PHP Model class which provides: | ||||||
|
|
||||||
| CREATE DATABASE categorytest; | ||||||
| CREATE TABLE `categories` ( | ||||||
| `id` int(11) unsigned NOT NULL AUTO_INCREMENT, | ||||||
| `name` varchar(120) DEFAULT NULL, | ||||||
| `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP, | ||||||
| `created_at` timestamp NULL DEFAULT NULL, | ||||||
| PRIMARY KEY (`id`) | ||||||
| ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; | ||||||
| * table column/property mapping | ||||||
| * CRUD operations | ||||||
| * dynamic finders and counters | ||||||
| * raw queries with escaped parameters | ||||||
| * results as instances of the Model class | ||||||
| * exceptions on query error | ||||||
|
|
||||||
| require_once('vendor/autoload.php'); | ||||||
| Freshsauce\Model\Model::connectDb('mysql:dbname=categorytest;host=127.0.0.1','root',''); // db connection for all sub-classes | ||||||
| Requirements & support | ||||||
| ---------------------- | ||||||
|
|
||||||
| // minimum model definition | ||||||
| class Category extends Freshsauce\Model\Model { | ||||||
| static protected $_tableName = 'categories'; // database table name | ||||||
| } | ||||||
| * PHP >= 8.3 | ||||||
| * PDO with MySQL/MariaDB or PostgreSQL drivers | ||||||
| * SQLite is supported in code paths but not covered by tests | ||||||
|
|
||||||
| PostgreSQL example schema and connection | ||||||
|
|
||||||
| CREATE TABLE categories ( | ||||||
| id SERIAL PRIMARY KEY, | ||||||
| name VARCHAR(120) NULL, | ||||||
| updated_at TIMESTAMP NULL, | ||||||
| created_at TIMESTAMP NULL | ||||||
| ); | ||||||
| Usage | ||||||
| ===== | ||||||
|
|
||||||
| Freshsauce\Model\Model::connectDb('pgsql:host=127.0.0.1;port=5432;dbname=categorytest','postgres','postgres'); | ||||||
| With a MySQL database on localhost: | ||||||
|
|
||||||
| ```sql | ||||||
| CREATE DATABASE categorytest; | ||||||
| CREATE TABLE `categories` ( | ||||||
| `id` int(11) unsigned NOT NULL AUTO_INCREMENT, | ||||||
| `name` varchar(120) DEFAULT NULL, | ||||||
| `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP, | ||||||
| `created_at` timestamp NULL DEFAULT NULL, | ||||||
| PRIMARY KEY (`id`) | ||||||
| ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; | ||||||
| ``` | ||||||
|
|
||||||
| ```php | ||||||
| require_once('vendor/autoload.php'); | ||||||
| Freshsauce\Model\Model::connectDb('mysql:dbname=categorytest;host=127.0.0.1', 'root', ''); | ||||||
|
|
||||||
| // minimum model definition | ||||||
| class Category extends Freshsauce\Model\Model { | ||||||
| static protected $_tableName = 'categories'; | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| PostgreSQL schema and connection: | ||||||
|
|
||||||
| ```sql | ||||||
| CREATE TABLE categories ( | ||||||
| id SERIAL PRIMARY KEY, | ||||||
| name VARCHAR(120) NULL, | ||||||
| updated_at TIMESTAMP NULL, | ||||||
| created_at TIMESTAMP NULL | ||||||
| ); | ||||||
| ``` | ||||||
|
|
||||||
| ```php | ||||||
| Freshsauce\Model\Model::connectDb('pgsql:host=127.0.0.1;port=5432;dbname=categorytest', 'postgres', 'postgres'); | ||||||
| ``` | ||||||
|
|
||||||
| Testing | ||||||
| ======= | ||||||
|
|
||||||
| Run PHPUnit with optional environment overrides for the DB connection: | ||||||
|
|
||||||
| MODEL_ORM_TEST_DSN=mysql:host=127.0.0.1;port=3306 | ||||||
| MODEL_ORM_TEST_USER=root | ||||||
| MODEL_ORM_TEST_PASS= | ||||||
| ``` | ||||||
| MODEL_ORM_TEST_DSN=mysql:host=127.0.0.1;port=3306 | ||||||
| MODEL_ORM_TEST_USER=root | ||||||
| MODEL_ORM_TEST_PASS= | ||||||
|
|
||||||
| vendor/bin/phpunit -c phpunit.xml.dist | ||||||
| ``` | ||||||
|
|
||||||
| vendor/bin/phpunit -c phpunit.xml.dist | ||||||
| Static analysis: | ||||||
|
|
||||||
| ``` | ||||||
| vendor/bin/phpstan analyse -c phpstan.neon | ||||||
| ``` | ||||||
|
|
||||||
| Save & Update records | ||||||
| ===================== | ||||||
|
|
||||||
| $newCategory = new Category(array( | ||||||
| 'name' => 'test' | ||||||
| )); | ||||||
| $newCategory->save(); | ||||||
| ```php | ||||||
| $newCategory = new Category(array( | ||||||
| 'name' => 'test' | ||||||
| )); | ||||||
| $newCategory->save(); | ||||||
| ``` | ||||||
|
|
||||||
| as Id is not set inserts the data as a new table row, `$newCategory->id` is set that of the row inserted post insert | ||||||
|
||||||
| as Id is not set inserts the data as a new table row, `$newCategory->id` is set that of the row inserted post insert | |
| Since `id` is not set, this inserts the data as a new table row. After insertion, `$newCategory->id` is set to the id of the inserted row. |
davebarnwell marked this conversation as resolved.
Show resolved
Hide resolved
Copilot
AI
Jan 30, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Grammar issue: "return" should be capitalized since this is a standalone description. Change to "Return the last record in the table when sorted by ascending primary key as a Category object (or null)."
| return the last record in the table when sorted by ascending primary key as a Category object (or null) | |
| Return the last record in the table when sorted by ascending primary key as a Category object (or null) |
Copilot
AI
Jan 30, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Grammar issue: "run" should be capitalized since this is a standalone description. Change to "Run an arbitrary statement returning a PDO statement handle to issue fetch etc... on."
| run an arbitrary statement returning a PDO statement handle to issue fetch etc... on | |
| Run an arbitrary statement returning a PDO statement handle to issue fetch etc... on |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The README now states SQLite is supported, but
Model::insert()buildsINSERT INTO ... SET ...for all non-PostgreSQL drivers; that syntax is MySQL-specific and will fail on SQLite as soon as you insert a row with any column values. This means users trying SQLite will hit SQL errors during normal inserts, so the documentation is misleading unless the insert path is updated to use SQLite-compatible syntax.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@codex fix
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To use Codex here, create an environment for this repo.