PHP Classes

File: lib/template/template_abstractclassrepo.php

Recommend this page to a friend!
  Classes of Jorge Castro   PDO One ORM   lib/template/template_abstractclassrepo.php   Download  
File: lib/template/template_abstractclassrepo.php
Role: Auxiliary script
Content type: text/plain
Description: Auxiliary script
Class: PDO One ORM
Access SQL database tables as object using PDO
Author: By
Last change:
Date: 1 year ago
Size: 14,451 bytes
 

Contents

Class file image Download
<?php http_response_code(404); die(1); // It is a template file, not a code to execute directly. This line is used to avoid to execute or read it. ?> /** @noinspection PhpUnusedParameterInspection * @noinspection PhpClassConstantAccessedViaChildClassInspection * @noinspection PhpClasspublic constantAccessedViaChildClassInspection * @noinspection NullCoalescingOperatorCanBeUsedInspection * @noinspection PhpPureAttributeCanBeAddedInspection * @noinspection PhpArrayShapeAttributeCanBeAddedInspection * @noinspection PhpMissingParamTypeInspection * @noinspection AccessModifierPresentedInspection * @noinspection PhpMissingReturnTypeInspection * @noinspection UnknownInspectionInspection * @noinspection PhpIncompatibleReturnTypeInspection * @noinspection ReturnTypeCanBeDeclaredInspection * @noinspection DuplicatedCode * @noinspection PhpUnused * @noinspection PhpUndefinedMethodInspection * @noinspection PhpUnusedLocalVariableInspection * @noinspection PhpUnusedAliasInspection * @noinspection NullPointerExceptionInspection * @noinspection SenselessProxyMethodInspection * @noinspection PhpParameterByRefIsNotUsedAsReferenceInspection */ {namespace} use eftec\PdoOne; use eftec\PdoOneQuery; {modelnamespace} {exception} // [EDIT:use] you can edit this part // Here you can add your custom use // [/EDIT] end of edit /** * Class Abstract{classname}. Copyright (c) Jorge Castro C. (https://github.com/EFTEC/PdoOne)<br> * Generated by PdoOne Version {version}.<br> * <b>DO NOT EDIT THE CODE OUTSIDE EDIT BLOCKS</b>. This code is generated<br> * If you want to make some changes, then add the changes to the Repository class.<br> * <pre> * $code=$pdoOne->generateCodeClass({args}); * </pre> */ abstract class Abstract{classname} extends {baseclass} { public const ENTITY = '{classname}'; public const TABLE = '{table}'; public const IDENTITY = {identity}; public const PK = {pk}; public const ME=__CLASS__; public const EXTRACOLS='{extracol}'; /** @var string|null $schema you can set the current schema/database used by this class. [Default is null] */ public static $schema; /** * It returns the definitions of the columns<br> * <b>Example:</b><br> * <pre> * self::getDef(); // ['colName'=>[php type,php conversion type,type,size,nullable,extra,sql],'colName2'=>..] * self::getDef('sql'); // ['colName'=>'sql','colname2'=>'sql2'] * self::getDef('identity',true); // it returns the columns that are identities ['col1','col2'] * </pre> * <b>PHP Types</b>: binary, date, datetime, decimal/float,int, string,time, timestamp<br> * <b>PHP Conversions</b>: datetime (datetime class), datetime2 (iso),datetime3 (human string) * , datetime4 (sql no conversion!), timestamp (int), bool, int, float<br> * <b>Param Types</b>: PDO::PARAM_LOB, PDO::PARAM_STR, PDO::PARAM_INT<br> * * @param string|null $column =['phptype','conversion','type','size','null','identity','sql'][$i] * if not null then it only returns the column specified. * @param string|null $filter If filter is not null, then it uses the column to filter the result. * * @return array|array[] */ public static function getDef($column = null, $filter = null): array { $r = {def}; if ($column !== null) { if ($filter === null) { foreach ($r as $k => $v) { $r[$k] = $v[$column]; } } else { $new = []; foreach ($r as $k => $v) { if ($v[$column] === $filter) { $new[] = $k; } } return $new; } } return $r; } /** * It converts a row returned from the database.<br> * If the column is missing then it sets the field as null. * * @param array $row [ref] */ public static function convertOutputVal(&$row) { if ($row === false || $row === null) { return; } {convertoutput} {linked} } /** * It converts a row to be inserted or updated into the database.<br> * If the column is missing then it is ignored and not converted. * * @param array $row [ref] */ public static function convertInputVal(&$row) { {convertinput} } /** * It gets all the name of the columns. * * @return string[] */ public static function getDefName() { return {defname}; } /** * It returns an associative array (colname=>key type) with all the keys/indexes (if any) * * @return string[] */ public static function getDefKey() { return {defkey}; } /** * It returns a string array with the name of the columns that are skipped when insert * @return string[] */ public static function getDefNoInsert() { return {defnoinsert}; } /** * It returns a string array with the name of the columns that are skipped when update * @return string[] */ public static function getDefNoUpdate() { return {defnoupdate}; } /** * It adds a where to the query pipeline. It could be stacked with many where() * <b>Example:</b><br> * <pre> * self::where(['col'=>'value'])::toList(); * self::where(['col']=>['value'])::toList(); // s= string/double/date, i=integer, b=bool * self::where(['col=?']=>['value'])::toList(); // s= string/double/date, i=integer, b=bool * </pre> * * @param array|string $sql =self::factory() * @param null|array|int $param * * @return PdoOneQuery */ public static function where($sql, $param = PdoOne::NULL) { return static::newQuery()->where($sql, $param,false,{classname}::TABLE); } public static function getDefFK($structure=false) { if ($structure) { return {deffk}; } /* key,refcol,reftable,extra */ return {deffktype}; } /** * It returns all the relational fields by type. '*' returns all types.<br> * It doesn't return normal columns. * * @param string $type=['*','MANYTOONE','ONETOMANY','ONETOONE','MANYTOMANY'][$i] * * @return string[] * @noinspection SlowArrayOperationsInLoopInspection */ public static function getRelations($type = 'all') { $r = {deffktype2}; if ($type === '*') { $result = []; foreach ($r as $arr) { $result = array_merge($result, $arr); } return $result; } return $r[$type] ?? []; } /** * @param array|int $filter (optional) if we want to filter the results. * @param array|null $filterValue (optional) the values of the filter * @return array|bool|null * @throws Exception */ public static function toList($filter=PdoOne::NULL,$filterValue=null) { if(self::$useModel) { return {classmodellist} } return self::_toList($filter, $filterValue); } /** * It sets the recursivity. By default, if we query or modify a value, it operates with the fields of the entity. * With recursivity, we could use the recursivity of the fields, for example, loading a MANYTOONE relation<br> * <b>Example:</b><br> * <pre> * self::recursive([]); // (default) no use recursivity. * self::recursive('*'); // recursive every MANYTOONE,ONETOONE,MANYTOONE and ONETOONE relations (first level) * self::recursive('MANYTOONE'); // recursive all relations of the type MANYTOONE (first level) * self::recursive(['/_relation1','/_relation2']); // recursive only the relations of the first level * self::recursive(['/_relation1','/_relation1/_subrelation1']); //recursive the relations (first and second level) * </pre> * If array then it uses the values to set the recursivity.<br> * If string then the values allowed are '*', 'MANYTOONE','ONETOMANY','MANYTOMANY','ONETOONE' (first level only)<br> * * @param string|array $recursive={factoryrecursive}; * * @return PdoOneQuery */ public static function recursive($recursive=[]) { if(is_string($recursive)) { $recursive={classname}::getRelations($recursive); } return parent::_recursive($recursive); } /** * It adds a "limit" in a query. It depends on the type of database<br> * <b>Example:</b><br> * <pre> * self::limit("10,20")->toList(); // start row 10th, fetches 20 next 20 rows * self::limit(10,20)->toList(); // start row 10th, fetches 20 next 20 rows * </pre> * * @param mixed $first the first (initial) value or the sql query expression * @param mixed|null $second the values to fetches. * * @return PdoOneQuery * @throws Exception * @test InstanceOf PdoOne::class,this('1,10') */ public static function limit($first,$second=null) : PdoOneQuery { return static::newQuery()->limit($first,$second); } /** * It returns the first row of a query.<br> * <b>Example:</b><br> * <pre> * Repo::first(); // it returns the first value encountered. * Repo::first(2); // it returns the first value where the primary key is equals to 2 (simple primary key) * Repo::first([2,3]); // it returns the first value where the primary key is equals to 2 (multiple primary keys) * Repo::first(['id'=>2,'id2'=>3]); // it returns the first value where id=2 and id2=3 (multiple primary keys) * </pre> * @param array|mixed|null $pk [optional] Specify the value of the primary key. * * @return array|bool It returns false if not file is found. * @throws Exception */ public static function first($pk = PdoOne::NULL) { if(self::$useModel) { /** @noinspection PhpIncompatibleReturnTypeInspection */ return {classmodelfirst} } return self::_first($pk); } /** * It returns true if the entity exists, otherwise false.<br> * <b>Example:</b><br> * <pre> * $this->exist(['id'=>'a1','name'=>'name']); // using an array * $this->exist('a1'); // using the primary key. The table needs a pks, and it only works with the first pk. * </pre> * * @param array|mixed $entity =self::factory() * * @return bool true if the pks exists * @throws Exception */ public static function exist($entity) { return self::_exist($entity); } /** * It inserts a new entity(row) into the database<br> * @param array|object $entity =self::factory() * @param bool $transactional If true (default) then the operation is transactional * * @return array|false=self::factory() * @throws Exception */ public static function insert(&$entity,$transactional=true) { return self::_insert($entity,$transactional); } /** * It merges a new entity(row) into the database. If the entity exists then it is updated, otherwise the entity is * inserted<br> * @param array|object $entity =self::factory() * @param bool $transactional If true (default) then the operation is transactional * * @return array|false=self::factory() * @throws Exception */ public static function merge(&$entity,$transactional=true) { return self::_merge($entity,$transactional); } /** * Updates an entity. It uses the primary key to determine the row to modify.<br> * If <b>$entity</b> has a missing field, then the missing field will not be updated.<br> * If <b>$entity</b> has an extra field (a field not defined in the table), then it will throw an error<br> * <b>Example:</b><br> * <pre> * $result=$this::factory(); * $result['name']='name changed'; * $ok=$this::update($customer); * </pre> * @param array|object $entity =self::factoryUtil() * @param bool $transactional If true (default) then the operation is transactional<br> * If false, then it allows to create your own transaction. * @return false|int=self::factoryUtil() * @throws Exception */ public static function update($entity,bool $transactional=true) { return self::_update($entity,$transactional); } /** * It deletes an entity by the primary key * * @param array|object $entity =self::factory() * @param bool $transactional If true (default) then the operation is transactional * * @return false|int * @throws Exception */ public static function delete($entity,$transactional=true) { return self::_delete($entity,$transactional); } /** * It deletes an entity by the primary key. * * @param array|mixed $pk =self::factory() * @param bool $transactional If true (default) then the operation is transactional * * @return int|false * @throws Exception */ public static function deleteById($pk,$transactional=true) { return self::_deleteById($pk,$transactional); } /** * Returns an array with the default values (0 for numbers, empty for string, and array|null if recursive) * * @param array|null $values =self::factory() * @param string $recursivePrefix It is the prefix of the recursivity. * * @return array */ public static function factory($values = null, $recursivePrefix = '') { $recursive=static::getRecursive(); static::recursive(); // reset the recursivity. $row= {array}; {linked} if ($values !== null) { $row = array_merge($row, $values); } return $row; } /** * It returns an empty array with null values and no recursivity. * @param array|null $values=self::factoryNull() * * @return array */ public static function factoryNull($values=null) { $row= {array_null}; if ($values !== null) { $row = array_merge($row, $values); } return $row; } // [EDIT:content] you can edit this part // Here you can add your custom content. // [/EDIT] end of edit }