{"id":22835271,"url":"https://github.com/gueff/mymvc_module_db","last_synced_at":"2026-04-29T19:32:43.717Z","repository":{"id":96371199,"uuid":"226175335","full_name":"gueff/myMVC_module_DB","owner":"gueff","description":"MySQL Database Module for myMVC","archived":false,"fork":false,"pushed_at":"2023-12-07T13:47:01.000Z","size":171,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"3.x","last_synced_at":"2025-06-20T17:50:40.458Z","etag":null,"topics":["framework","mymvc","mymvc-module","mysql","php","php-framework"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gueff.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-12-05T19:36:11.000Z","updated_at":"2023-05-29T14:15:26.000Z","dependencies_parsed_at":"2023-05-06T08:01:22.681Z","dependency_job_id":"6493470b-7235-40fb-a4f4-172f73be763c","html_url":"https://github.com/gueff/myMVC_module_DB","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/gueff/myMVC_module_DB","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gueff%2FmyMVC_module_DB","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gueff%2FmyMVC_module_DB/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gueff%2FmyMVC_module_DB/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gueff%2FmyMVC_module_DB/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gueff","download_url":"https://codeload.github.com/gueff/myMVC_module_DB/tar.gz/refs/heads/3.x","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gueff%2FmyMVC_module_DB/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32441023,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-29T18:12:22.909Z","status":"ssl_error","status_checked_at":"2026-04-29T18:11:33.322Z","response_time":110,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["framework","mymvc","mymvc-module","mysql","php","php-framework"],"created_at":"2024-12-12T22:08:52.281Z","updated_at":"2026-04-29T19:32:43.700Z","avatar_url":"https://github.com/gueff.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# myMVC_module_DB\n\n- [1. Requirements](#1)\n- [2. Repository](#2)\n- [3. Creation](#3)\n  - [3.1. Create DB Config](#3-1)\n  - [3.2. Creating a concrete Table Class](#3-2)\n  - [3.3. Creating a DBInit class that is used for each DB access](#3-3)\n  - [3.4. Let generate an openapi yaml schema file for data type classes](#3-4)\n- [4. Usage](#4)\n  - [4.1. create](#4-1)\n  - [4.2. retrieve](#4-2)\n  - [4.3. update](#4-3)\n  - [4.4. delete](#4-4)\n  - [4.5. count](#4-5)\n  - [4.6. checksum](#4-6)\n  - [4.7. getFieldInfo](#4-7)\n  - [4.8. SQL](#4-8)\n- [5. Events](#5)\n  - [5.1. Logging SQL](#5-1)\n\n---\n\n\u003ca id=\"1\"\u003e\u003c/a\u003e\n\n## 1. Requirements\n\n- Linux\n- php \u003e= 8\n  - `pdo` extension\n- myMVC 3.x\n  - `git clone --branch 3.x https://github.com/gueff/myMVC.git myMVC_3.x`\n  - Docs: \u003chttps://mymvc.ueffing.net/\u003e\n  - github: \u003chttps://github.com/gueff/myMVC/tree/3.3.x\u003e\n\n---\n\n\u003ca id=\"2\"\u003e\u003c/a\u003e\n\n## 2. Repository\n\n- \u003chttps://github.com/gueff/myMVC_module_DB\u003e\n\n---\n\n\u003ca id=\"3\"\u003e\u003c/a\u003e\n\n## 3. Creation\n\n\u003ca id=\"3-1\"\u003e\u003c/a\u003e\n\n### 3.1. Create DB Config\n\n\nIn your main module's config folder create your DB Config.\n(@see https://mymvc.ueffing.net/3.3.x/configuration#Modules-config-folder)\n\n\n_Db Config example for `develop` environments_\n~~~php\n//-------------------------------------------------------------------------------------\n// Module DB\n\n$aConfig['MODULE']['DB'] = array(\n\n    'db' =\u003e array(\n        'type' =\u003e 'mysql',\n        'host' =\u003e '127.0.0.1',\n        'port' =\u003e 3306,\n        'username' =\u003e getenv('db.username'),\n        'password' =\u003e getenv('db.password'),\n        'dbname' =\u003e getenv('db.dbname'),\n        'charset' =\u003e 'utf8'\n    ),\n    'caching' =\u003e array(\n        'enabled' =\u003e true,\n        'lifetime' =\u003e '7200'\n    ),\n    'logging' =\u003e array(\n        'log_output' =\u003e 'FILE',\n\n        // consider to turn it on for develop and test environments only\n        'general_log' =\u003e strtoupper('on'), # on | off\n\n        // 1) make sure write access is given to the folder\n        // as long as the db user is going to write and not the webserver user\n        // 2) consider a logrotate mechanism for this logfile as it may grow quickly\n        'general_log_file' =\u003e '/tmp/' . getenv('db.dbname') . '.log',\n    )\n);\n~~~\n- here we make use of `getenv()`, which means we store our secrets in the `/.env` file.\n\n\n\u003ca id=\"3-2\"\u003e\u003c/a\u003e\n\n### 3.2. Creating a concrete Table Class\n\n_PHP Class_\nas a Representation of the DB Table\n\n\n_file: `modules/Foo/Model/Table/User.php`_\n~~~php\n\u003c?php\n\nnamespace Foo\\Model\\Table;\n\nuse DB\\Model\\Db;\n\n\nclass User extends Db\n{\n    /**\n     * @var array\n     */\n    protected $aField = array(\n        'email'     =\u003e \"varchar(255) COLLATE utf8_general_ci NOT NULL\",\n        'active'    =\u003e \"int(1) DEFAULT '0' NOT NULL\",\n        'uuid'      =\u003e \"varchar(36) COLLATE utf8_general_ci COMMENT 'uuid permanent' NOT NULL\",\n        'uuidtmp'      =\u003e \"varchar(36) COLLATE utf8_general_ci COMMENT 'uuid; changes on create|login' NOT NULL\",\n        'password'  =\u003e \"varchar(60) COLLATE utf8_general_ci COMMENT 'password_hash()' NOT NULL\",\n        'nickname'  =\u003e \"varchar(10) COLLATE utf8_general_ci NOT NULL\",\n        'forename'  =\u003e \"varchar(25) COLLATE utf8_general_ci NOT NULL\",\n        'lastname'  =\u003e \"varchar(25) COLLATE utf8_general_ci NOT NULL\",\n    );\n\n    /**\n     * @param array $aDbConfig\n     * @throws \\ReflectionException\n     */\n    public function __construct(array $aDbConfig = array())\n    {\n        // basic creation of the table\n        parent::__construct(\n            $this-\u003eaField,\n            $aDbConfig\n        );\n    }\n}\n~~~\n\n- creates the Table `FooModelTableUser`\n  - Table has several fields from `email` ... `lastname` as declared in property `$aField`\n    - 🛈 The Table fields `id`, `stampChange` and `stampCreate` are added automatically\n    - do not add these fields by manually\n- generates a DataType Class `DataType/DTFooModelTableUser.php`\n\n---\n\n**Creating a Table and adding a Foreign Key**\n\n\n_file: `modules/Foo/Model/Table/User.php`_\n~~~php\n\u003c?php\n\nnamespace Foo\\Model\\Table;\n\nuse DB\\Model\\Db;\nuse DB\\DataType\\DB\\Foreign;\n\nclass User extends Db\n{\n    /**\n     * @var array\n     */\n    protected $aField = array(\n        'email'     =\u003e \"varchar(255) COLLATE utf8_general_ci NOT NULL\",\n        'active'    =\u003e \"int(1) DEFAULT '0' NOT NULL\",\n        'uuid'      =\u003e \"varchar(36) COLLATE utf8_general_ci COMMENT 'uuid permanent' NOT NULL\",\n        'uuidtmp'      =\u003e \"varchar(36) COLLATE utf8_general_ci COMMENT 'uuid; changes on create|login' NOT NULL\",\n        'password'  =\u003e \"varchar(60) COLLATE utf8_general_ci COMMENT 'password_hash()' NOT NULL\",\n        'nickname'  =\u003e \"varchar(10) COLLATE utf8_general_ci NOT NULL\",\n        'forename'  =\u003e \"varchar(25) COLLATE utf8_general_ci NOT NULL\",\n        'lastname'  =\u003e \"varchar(25) COLLATE utf8_general_ci NOT NULL\",\n    );\n\n    /**\n     * @param array $aDbConfig\n     * @throws \\ReflectionException\n     */\n    public function __construct(array $aDbConfig = array())\n    {\n        // basic creation of the table\n        parent::__construct(\n            $this-\u003eaField,\n            $aDbConfig\n        );\n        $this-\u003esetForeignKey(\n            Foreign::create()\n                -\u003eset_sForeignKey('id_FooModelTableGroup')\n                -\u003eset_sReferenceTable('FooModelTableGroup')\n        );\n    }\n}\n~~~\n\n- creates the Table `FooModelTableUser`\n  - Table has several fields from `email` ... `lastname` as declared in property `$aField`\n    - 🛈 The Table fields `id`, `stampChange` and `stampCreate` are added automatically\n    - do not add these fields by manually\n- The foreign key `id_FooModelTableGroup` -pointing to table `FooModelTableGroup`- is added by method `setForeignKey()`\n- generates a DataType Class `DataType/DTFooModelTableUser.php`\n\n---\n\n\u003ca id=\"3-3\"\u003e\u003c/a\u003e\n\n### 3.3. Creating a DBInit class that is used for each DB access\n\n\n_file: `modules/Foo/Model/DB.php`_\n~~~php\n\u003c?php\n\n/**\n * - register your db table classes as static properties.\n * - add a doctype to each static property\n * - these doctypes must contain the vartype information about the certain class\n * @example\n *      @var Foo\\Model\\Table\\User\n *      public static $oFooModelTableUser;\n * ---\n * [!]  it is important to declare the vartype expanded with a full path\n *      avoid to make use of `use ...` support\n *      otherwise the classes could not be read correctly\n */\n\nnamespace Foo\\Model;\n\nuse DB\\Model\\DbInit;\nuse DB\\Trait\\DbInitTrait;\n\nclass DB extends DbInit\n{\n    use DbInitTrait;\n\n    /**\n     * @var \\Foo\\Model\\Table\\User\n     */\n    public static $oFooModelTableUser;\n}\n~~~\n\n---\n\n\u003ca id=\"3-4\"\u003e\u003c/a\u003e\n\n### 3.4. Let generate an openapi yaml schema file for data type classes\n\ncreate a file `db.php` (you can name it as you like) in the event folder of your myMVC module and declare the bindings as follows.\n\n_file `/modules/{MODULE}/etc/event/db.php`_\n~~~php\n\u003c?php\n\n\\MVC\\Event::processBindConfigStack([\n\n    // let create an openapi yaml file\n    // according to DB Table DataType Classes\n    // when the DataBase Tables setup changes\n    'db.model.db.construct.saveCache' =\u003e array(\n        function(string $sTableName = '') {\n\n            // one-timer\n            if (false === \\MVC\\Registry::isRegistered('DB::openapi'))\n            {\n                \\MVC\\Registry::set('DB::openapi', true);\n\n                // generate /modules/{MODULE}/DataType/DTTables.yaml\n                $sYamlFile =\\DB\\Model\\Openapi::createDTYamlOnDTClasses(\n                    // pass instance of your concrete DB Class\n                    \\Foo\\Model\\DB::init()\n                );\n            }\n        }\n    ),\n]);\n~~~\n\n---\n\n\u003ca id=\"4\"\u003e\u003c/a\u003e\n\n## 4. Usage\n\n\nIn your main **Controller** class just create a new Instanciation of your DBInit class.\nA good place is the `__construct()` method.\n\n~~~php\nnamespace Foo\\Controller;\n\nuse Foo\\Model\\DB;\n\npublic function __construct ()\n{\n    DB::init();\n}\n~~~\n\nafter that you can access your TableClass from everywhere - even from frontend templates:\n\n\n_Usage_\n~~~php\nDB::$oFooModelTableUser-\u003e...\u003cmethod\u003e...\n~~~\n\n\u003ca id=\"4-1\"\u003e\u003c/a\u003e\n\n### 4.1. create\n\n_`create` (INSERT)_\ntherefore an object of its related Datatype must be instaciated and given to the method `create`.\nHere e.g. with Datatype \"DTFooModelTableUser\" to TableClass \"modules/Foo/Model/DB/TableUser\":\n\n~~~php\nDB::$oFooModelTableUser-\u003ecreate(\n    DTFooModelTableUser::create()\n        -\u003eset_id_FooModelTableGroup(1)\n        -\u003eset_uuid(Strings::uuid4())\n        -\u003eset_email('foo@example.com')\n        -\u003eset_forename('foo')\n        -\u003eset_lastname('bar')\n        -\u003eset_nickname('foo')\n        -\u003eset_password(password_hash('...password...', PASSWORD_DEFAULT))\n        -\u003eset_active(1)\n        -\u003eset_stampChange(date('Y-m-d H:i:s'))\n        -\u003eset_stampCreate(date('Y-m-d H:i:s'))\n);\n~~~\n\n\n\u003ca id=\"4-2\"\u003e\u003c/a\u003e\n\n#### 4.2. retrieve\n\n`retrieveTupel` asks for a specific Tupel and returns the DataType Object according to the requested Table.\n\n\n_`retrieveTupel` - identified by `id`_\n~~~php\n/** @var \\Foo\\DataType\\DTFooModelTableUser $oDTFooModelTableUser */\n$oDTFooModelTableUser = DB::$oFooModelTableUser-\u003eretrieveTupel(\n    DTFooModelTableUser::create()\n        -\u003eset_id(2)\n)\n~~~\n- get User Object whose id=2\n\n\n`retrieve` returns an array of DataType Objects according to the requested Table.\n\n_`retrieve`: get all Datasets_\n~~~php\n/** @var \\Foo\\DataType\\DTFooModelTableUser[] $aDTFooModelTableUser */\n$aDTFooModelTableUser = DB::$oFooModelTableUser-\u003eretrieveTupel();\n~~~\n\n_`retrieve`: get specific Datasets_\n~~~php\n/** @var \\Foo\\DataType\\DTFooModelTableUser[] $aDTFooModelTableUser */\n$aDTFooModelTableUser = DB::$oFooModelTableUser-\u003eretrieve(\n    DTArrayObject::create()\n        -\u003eadd_aKeyValue(\n            DTKeyValue::create()\n                -\u003eset_sKey('stampChange')\n                -\u003eset_mOptional1('LIKE')\n                -\u003eset_sValue('2021-06-19')\n            );\n);\n~~~\n\n_`retrieve`: get Datasets with sort order_\n~~~php\n/** @var \\Foo\\DataType\\DTFooModelTableUser[] $aDTFooModelTableUser */\n$aDTFooModelTableUser = DB::$oFooModelTableUser-\u003eretrieve(\n    DTArrayObject::create()\n        -\u003eadd_aKeyValue(\n            DTKeyValue::create()\n                -\u003eset_sKey('email')\n                -\u003eset_mOptional1('LIKE')\n                -\u003eset_sValue('%@example.com%')\n        ),\n    DTArrayObject::create()\n        -\u003eadd_aKeyValue(\n            DTKeyValue::create()\n                -\u003eset_sValue('ORDER BY id ASC')\n        )\n);\n~~~\n\n_`retrieve`: get first 30 Datasets (LIMIT 0,30)_\n~~~php\n/** @var \\Foo\\DataType\\DTFooModelTableUser[] $aDTFooModelTableUser */\n$aDTFooModelTableUser = DB::$oFooModelTableUser-\u003eretrieve(\n    null,\n    DTArrayObject::create()\n        -\u003eadd_aKeyValue(\n            DTKeyValue::create()\n                -\u003eset_sValue('LIMIT 0,30')\n        )\n)\n~~~\n\n\n\u003ca id=\"4-3\"\u003e\u003c/a\u003e\n\n#### 4.3. update\n\n\n_`updateTupel`: update a specific Tupel - identified by `id`_\n~~~php\n// get Tupel\n/** @var \\Foo\\DataType\\DTFooModelTableUser $oDTFooModelTableUser */\n$oDTFooModelTableUser = DB::$oFooModelTableUser-\u003eretrieveTupel(\n    DTFooModelTableUser::create()-\u003eset_id(2)\n)\n\n// modify Tupel\n$oDTFooModelTableUser-\u003eset_nickname('XYZ');\n\n// update Tupel\n/** @var boolean $bSuccess */\n$bSuccess = DB::$oFooModelTableUser-\u003eupdateTupel(\n    $oDTFooModelTableUser\n);\n~~~\n- the equivalent dataset tupel with object's `id` will be updated.\n\n_`update`: update all Tupel which are affected by the where clause_\n~~~php\n/** @var boolean $bSuccess */\n$bSuccess = DB::$oFooModelTableUser-\u003eupdate(\n    DTFooModelTableUser::create()\n        -\u003eset_active('1'),\n    // where\n    DTArrayObject::create()\n        -\u003eadd_aKeyValue(\n            DTKeyValue::create()\n                -\u003eset_sKey('active')\n                -\u003eset_mOptional1('=')\n                -\u003eset_sValue('0')\n        )\n);\n~~~\n\n\n\u003ca id=\"4-4\"\u003e\u003c/a\u003e\n\n#### 4.4. delete\n\n_`deleteTupel`: delete this specific Tupel - identified by `id`_\n~~~php\n/** @var boolean $bSuccess */\n$bSuccess = DB::$oFooModelTableUser-\u003edeleteTupel(\n    DTFooModelTableUser::create()\n        -\u003eset_id(2)\n)\n~~~\n\n_`delete`: delete all Tupel which are affected by the where clause_\n~~~php\n$bSuccess = DB::$oFooModelTableUser-\u003edelete(\n    // where\n    DTArrayObject::create()\n        -\u003eadd_aKeyValue(\n            DTKeyValue::create()\n                -\u003eset_sKey('stampCreate')\n                -\u003eset_mOptional1('\u003c')\n                -\u003eset_sValue('2023-06-19 00:00:00')\n        )\n);\n~~~\n\n\u003ca id=\"4-5\"\u003e\u003c/a\u003e\n\n### 4.5. count\n\n~~~php\n// Amount of all Datasets\n$iAmount = DB::$oFooModelTableUser-\u003ecount();\n\n// Amount of specific Datasets\n$iAmount = DB::$oFooModelTableUser-\u003ecount(\n    DTArrayObject::create()\n        -\u003eadd_aKeyValue(\n            DTKeyValue::create()\n                -\u003eset_sKey('stampChange')\n                -\u003eset_mOptional1('=')\n                -\u003eset_sValue('2021-06-19')\n    )\n);\n~~~\n\n\u003ca id=\"4-6\"\u003e\u003c/a\u003e\n\n### 4.6. checksum\n\n~~~php\n// Returns a checksum of the table\n$iChecksum = DB::$oFooModelTableUser-\u003echecksum();\n~~~\n\n\u003ca id=\"4-7\"\u003e\u003c/a\u003e\n\n### 4.7. getFieldInfo\n\nreturns array with table fields info\n\n~~~php\n$aFieldInfo = DB::$oFooModelTableUser-\u003egetFieldInfo();\n~~~\n\n_example return_\n\n~~~\n// type: array, items: 9\n[\n    'id_FooModelTableGroup' =\u003e [\n        'Field' =\u003e 'id_FooModelTableGroup',\n        'Type' =\u003e 'int(11)',\n        'Null' =\u003e 'YES',\n        'Key' =\u003e 'MUL',\n        'Default' =\u003e NULL,\n        'Extra' =\u003e '',\n        'php' =\u003e 'int',\n    ],\n    'email' =\u003e [\n        'Field' =\u003e 'email',\n        'Type' =\u003e 'varchar(255)',\n        'Null' =\u003e 'NO',\n        'Key' =\u003e '',\n        'Default' =\u003e NULL,\n        'Extra' =\u003e '',\n        'php' =\u003e 'string',\n    ],\n    'active' =\u003e [\n        'Field' =\u003e 'active',\n        'Type' =\u003e 'int(1)',\n        'Null' =\u003e 'NO',\n        'Key' =\u003e '',\n        'Default' =\u003e '0',\n        'Extra' =\u003e '',\n        'php' =\u003e 'int',\n    ],\n    'uuid' =\u003e [\n        'Field' =\u003e 'uuid',\n        'Type' =\u003e 'varchar(36)',\n        'Null' =\u003e 'NO',\n        'Key' =\u003e '',\n        'Default' =\u003e NULL,\n        'Extra' =\u003e '',\n        'php' =\u003e 'string',\n    ],\n    'uuidtmp' =\u003e [\n        'Field' =\u003e 'uuidtmp',\n        'Type' =\u003e 'varchar(36)',\n        'Null' =\u003e 'NO',\n        'Key' =\u003e '',\n        'Default' =\u003e NULL,\n        'Extra' =\u003e '',\n        'php' =\u003e 'string',\n    ],\n    'password' =\u003e [\n        'Field' =\u003e 'password',\n        'Type' =\u003e 'varchar(60)',\n        'Null' =\u003e 'NO',\n        'Key' =\u003e '',\n        'Default' =\u003e NULL,\n        'Extra' =\u003e '',\n        'php' =\u003e 'string',\n    ],\n    'nickname' =\u003e [\n        'Field' =\u003e 'nickname',\n        'Type' =\u003e 'varchar(10)',\n        'Null' =\u003e 'NO',\n        'Key' =\u003e '',\n        'Default' =\u003e NULL,\n        'Extra' =\u003e '',\n        'php' =\u003e 'string',\n    ],\n    'forename' =\u003e [\n        'Field' =\u003e 'forename',\n        'Type' =\u003e 'varchar(25)',\n        'Null' =\u003e 'NO',\n        'Key' =\u003e '',\n        'Default' =\u003e NULL,\n        'Extra' =\u003e '',\n        'php' =\u003e 'string',\n    ],\n    'lastname' =\u003e [\n        'Field' =\u003e 'lastname',\n        'Type' =\u003e 'varchar(25)',\n        'Null' =\u003e 'NO',\n        'Key' =\u003e '',\n        'Default' =\u003e NULL,\n        'Extra' =\u003e '',\n        'php' =\u003e 'string',\n    ],\n]\n~~~\n\n\u003ca id=\"4-8\"\u003e\u003c/a\u003e\n\n#### 4.8. SQL\n\n_`SQL` example using `$oPDO` query .. fetch_\n~~~php\n/**\n * @return \\Foo\\DataType\\DTFooModelTableUser\n * @throws \\ReflectionException\n */\npublic function getUserObject()\n{\n    // get result \u0026 cast to datatype object\n    $oDTFooModelTableUser = DTFooModelTableUser::create(\n        (array) DB::$oPDO\n            -\u003equery(\"SELECT * FROM `FooModelTableUser` WHERE `id` = '1'\")\n            -\u003efetch(\\PDO::FETCH_ASSOC)\n    );\n    \n    return $oDTFooModelTableUser\n}\n~~~\n~~~\n// type: object\n\\Foo\\DataType\\DTFooModelTableUser::__set_state(array(\n      'id' =\u003e 1,\n      'stampChange' =\u003e '2023-09-28 10:18:03',\n      'stampCreate' =\u003e '2023-09-28 10:16:14',\n      'id_TableGroup' =\u003e 1,\n      'email' =\u003e 'admin@example.com',\n      'active' =\u003e 1,\n      'uuid' =\u003e '8b838038-5dd7-11ee-8620-2cf05d0841fd',\n      'uuidtmp' =\u003e '8b838839-5dd7-11ee-8620-2cf05d0841fd',\n      'password' =\u003e '*******************************************',\n      'nickname' =\u003e 'admin',\n      'forename' =\u003e 'foo',\n      'lastname' =\u003e 'bar',\n))\n~~~\n\n_`SQL` example using `$oPDO` query .. fetchAll_\n~~~php\n/**\n * @return array|\\Foo\\DataType\\DTFooModelTableUser[]\n * @throws \\ReflectionException\n */\npublic function getUserObjectsArray()\n{\n    // get result \u0026 cast all results to datatype objects\n    $aDTFooModelTableUser = array_map(\n        function($aData){\n            return DTFooModelTableUser::create($aData);\n        },\n        (array) DB::$oPDO\n            -\u003equery(\"SELECT * FROM `FooModelTableUser` WHERE `active` = '1'\")\n            -\u003efetchAll(\\PDO::FETCH_ASSOC)\n    );\n    \n    return $aDTFooModelTableUser\n}\n~~~\n~~~\n// type: array, items: 2\n[\n    0 =\u003e [\n    \\Foo\\DataType\\DTFooModelTableUser::__set_state(array(\n          'id' =\u003e 1,\n          'stampChange' =\u003e '2023-09-28 10:18:03',\n          'stampCreate' =\u003e '2023-09-28 10:16:14',\n          'id_TableGroup' =\u003e 1,\n          'email' =\u003e 'admin@example.com',\n          'active' =\u003e 1,\n          'uuid' =\u003e '8b838038-5dd7-11ee-8620-2cf05d0841fd',\n          'uuidtmp' =\u003e '8b838839-5dd7-11ee-8620-2cf05d0841fd',\n          'password' =\u003e '*******************************************',\n          'nickname' =\u003e 'admin',\n          'forename' =\u003e 'foo',\n          'lastname' =\u003e 'bar',\n    )],\n    1 =\u003e [\n    \\Foo\\DataType\\DTFooModelTableUser::__set_state(array(\n          'id' =\u003e 2,\n          'stampChange' =\u003e '2023-09-28 10:18:03',\n          'stampCreate' =\u003e '2023-09-28 10:16:14',\n          'id_TableGroup' =\u003e 1,\n          'email' =\u003e 'foo@example.com',\n          'active' =\u003e 1,\n          'uuid' =\u003e '1b838038-5dd7-11ee-8620-2cf05d0841fd',\n          'uuidtmp' =\u003e '2b838839-5dd7-11ee-8620-2cf05d0841fd',\n          'password' =\u003e '*******************************************',\n          'nickname' =\u003e 'foo',\n          'forename' =\u003e 'foo2',\n          'lastname' =\u003e 'bar2',\n    )],\n]\n~~~\n\n---\n\n\u003ca id=\"5\"\u003e\u003c/a\u003e\n\n## 5. Events\n\n~~~text\ndb.model.db.construct.saveCache\ndb.model.db.setSqlLoggingState.exception\ndb.model.db.setForeignKey.exception\ndb.model.db.checkIfTableExists.exception\ndb.model.db.createTable.exception\ndb.model.db.synchronizeFields.exception\ndb.model.db.synchronizeFields.delete.exception\ndb.model.db.synchronizeFields.insert.exception\ndb.model.db.synchronizeFields.update.exception\ndb.model.db.create.sql\ndb.model.db.createTable.sql\ndb.model.db.insert.sql\ndb.model.db.create.exception\ndb.model.db.retrieve.sql\ndb.model.db.retrieve.exception\ndb.model.db.count.sql\ndb.model.db.count.exception\ndb.model.db.update.sql\ndb.model.db.update.exception\ndb.model.db.delete.sql\ndb.model.db.delete.exception\n~~~\n\n\u003ca id=\"5-1\"\u003e\u003c/a\u003e\n\n### 5.1. Logging SQL\n\nyou can log SQL queries by listening to events.\n\ncreate a file `sql.php` in the event folder of your myMVC module\nand declare the bindings as follows.\n\n_`/modules/{MODULE}/etc/event/sql.php`_\n~~~php\n#-------------------------------------------------------------\n# declare bindings\n\n$aEvent = [\n    'db.model.db.create.sql' =\u003e array(\n        function(\\MVC\\DataType\\DTArrayObject $oDTArrayObject) {\n            \\MVC\\Log::write($oDTArrayObject-\u003egetDTKeyValueByKey('sSql')-\u003eget_sValue(), 'sql.log');\n        }\n    ),\n    'db.model.db.insert.sql' =\u003e array(\n        function(\\MVC\\DataType\\DTArrayObject $oDTArrayObject) {\n            \\MVC\\Log::write($oDTArrayObject-\u003egetDTKeyValueByKey('sSql')-\u003eget_sValue(), 'sql.log');\n        }\n    ),\n    'db.model.db.retrieve.sql' =\u003e array(\n        function(\\MVC\\DataType\\DTArrayObject $oDTArrayObject) {\n            \\MVC\\Log::write($oDTArrayObject-\u003egetDTKeyValueByKey('sSql')-\u003eget_sValue(), 'sql.log');\n        }\n    ),\n    'db.model.db.update.sql' =\u003e array(\n        function(\\MVC\\DataType\\DTArrayObject $oDTArrayObject) {\n            \\MVC\\Log::write($oDTArrayObject-\u003egetDTKeyValueByKey('sSql')-\u003eget_sValue(), 'sql.log');\n        }\n    ),\n    'db.model.db.delete.sql' =\u003e array(\n        function(\\MVC\\DataType\\DTArrayObject $oDTArrayObject) {\n            \\MVC\\Log::write($oDTArrayObject-\u003egetDTKeyValueByKey('sSql')-\u003eget_sValue(), 'sql.log');\n        }\n    ),\n    'db.model.db.createTable.sql' =\u003e array(\n        function(\\MVC\\DataType\\DTArrayObject $oDTArrayObject) {\n            \\MVC\\Log::write($oDTArrayObject-\u003egetDTKeyValueByKey('sSql')-\u003eget_sValue(), 'sql.log');\n        }\n    ),\n];\n\n#-------------------------------------------------------------\n# process: bind the declared ones\n\n\\MVC\\Event::processBindConfigStack($aEvent);\n~~~\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgueff%2Fmymvc_module_db","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgueff%2Fmymvc_module_db","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgueff%2Fmymvc_module_db/lists"}