# Code Conventions ## Naming | Entity | Convention | Example | |--------|-----------|---------| | Classes | PascalCase | `ProductRepository`, `ShopCategoryController` | | Methods | camelCase | `getQuantity()`, `categoryDetails()` | | Admin action methods | snake_case | `view_list()`, `category_edit()` | | Variables | camelCase | `$mockDb`, `$formViewModel`, `$postData` | | Constants | UPPER_SNAKE_CASE | `MAX_PER_PAGE`, `SORT_TYPES` | | DB tables | `pp_` prefix + snake_case | `pp_shop_products` | | DB columns | snake_case | `price_brutto`, `parent_id`, `lang_id` | | File (new) | `ClassName.php` | `ProductRepository.php` | | File (legacy) | `class.ClassName.php` | (leave, do not rename) | | Templates | kebab-case | `shop-category/category-edit.php` | ## Medoo ORM Patterns ```php // Get single record — returns array or null $product = $this->db->get('pp_shop_products', '*', ['id' => $id]); // Get single column value $qty = $this->db->get('pp_shop_products', 'quantity', ['id' => $id]); // Select multiple records — always guard against false return $rows = $this->db->select('pp_shop_categories', '*', [ 'parent_id' => $parentId, 'ORDER' => ['o' => 'ASC'], ]); if (!is_array($rows)) { return []; } // Count $count = $this->db->count('pp_shop_products', ['category_id' => $catId]); // Update $this->db->update('pp_shop_products', ['quantity' => 10], ['id' => $id]); // Delete — ALWAYS 2 arguments, never 3! $this->db->delete('pp_shop_categories', ['id' => $id]); // Insert, then check ID for success $this->db->insert('pp_shop_products', $data); $newId = $this->db->id(); ``` **Critical pitfalls:** - `$mdb->delete()` takes **2 args** — passing 3 causes silent bugs - `$mdb->get()` returns `null` (not `false`) when no record found - Always check `!is_array()` on `select()` results before iterating ## Redis Cache Patterns ```php $cache = new \Shared\Cache\CacheHandler(); // Read (data is serialized) $raw = $cache->get('shop\\product:' . $id . ':' . $lang . ':' . $hash); if ($raw) { return unserialize($raw); } // Write $cache->set( 'shop\\product:' . $id . ':' . $lang . ':' . $hash, serialize($data), 86400 // TTL in seconds ); // Delete one key $cache->delete($key); // Delete by pattern $cache->deletePattern("shop\\product:$id:*"); // Clear all product cache variations \Shared\Helpers\Helpers::clear_product_cache($productId); ``` ## Template Rendering ```php // In controller — always return string return \Shared\Tpl\Tpl::view('module/template-name', [ 'varName' => $value, ]); // In template — variables available as $this->varName