Napravih jedan PHP alatić, pa reko da ga podijelim…pa da me malo drvljem i kamenjem kao i obično.
Znači ideja je već poznata, razni database model-engini postoje …svejedno mi nešto nije dalo mira da napravim svoju jednostavniju verziju.
Jednostavniju sa aspekta mogućnosti, ali i sa aspekta korištenja. … ovo sam sveo doslovno na razinu da mi je isti vrag upravljati listom u bazi, kao i klasičnom PHP listom. Možda jednog dana, preko kompajlera to svedem i na to da će se pisati i identična sintaksa za obje varijante …a za sada je sintaksa za db-liste malkoc drugačija, pošto bez prekompajliranja code-a postoje ograničenja u igranji sa sintaksom.
Unatoč tome, metode nad db-listom su u suštini iste kao i metode nad klasičnim listama. (Sem par sitnica/inovacija sa kojima sam se poigrao. “Double-index” aproach)
Nadalje, ovo nije ni zamišljeno kao full database model engine …nego samo pojednostavljenje za handlanje listi koje imamo negdje u bazi. “one-to-many” relacije. Ako bi se već radio full database model engine, onda je ovo tek jedna od puzzli sa kojom bi se hendlao cijeli engine. A ja sam htio imati baš ovu puzzlu izdvojenu, funkcionalnu samu za sebe.
Ako se nekome učini to korisno na taj način, može pitat za code…poslat ću mu.
P.S. Unaprijed isprika nad lošim engleskim, al nekako sam dokumentaciju navikao praviti na engleskom unatoč nepoznavanju istoga. Preživit ćete to…kome će se dati zaviriti.
Ako sam propustio ugraditi neku metodu koju smatrate da nebi trebala biti propuštena, vičite.
Ili ako smatrate da se nepisanom konvencijom neka metoda treba drugačije zvati ili parametrizirati, vičite.
Lets journy begin
// dblist - elegant way to manage records "one-to-many";
$x = new dblist("Users","John","telefons");
/*
We can imagine $x as array for all telefons numbers from user Jhon from table "Users".
All methods on $x is similar like usual methods on some array. Only what we got all synchronized recorded to database.
Lets see parametars for initialization some dblist instance: // $x = new dblist($tableName, $entitetID, $listName);
"Users" -> its base for create table. Table will named as "Users_dblist_attributes"
"Jhon" -> its some ID. In this case, its ID of user from table "Users"
"telefons" -> this is attribute of Users, but this attribute we can't put in origin table "Users" cause any user can have multiple telefon numbers. So dblist will manage for us table "Users_dblist_attributes" and there will be all records for "one-to-many" relation data associated with table "Users".
So, if we have more "one-to-many" attributes related to table "Users", they all will be recorded in single table "Users_dblist_attributes". In this way we dont grow up database with new table for every new relation "one-to-many"
So let's see how manipulate with instance of dblist.
NOTE: dblist will not change on any way origin table "Users". All records refferenced with table "Users" will live in table "Users_dblist_attributes";
*/
$x->set(['091/111-111', '092/222-222', '097/777-777']); // We can simple set some list. Standard PHP syntax would be:: $x = [...];
//For retrieving data ... there is get() method.
print_r($x->get()); // >> Array ( [0] => 091/111-111 [1] => 092/222-222 [2] => 097/777-777 ) // Standard PHP syntax would be: print_r($x);
print_r($x->get(0)); // >> 091/111-111 // retrieving data from the first position.Standard PHP syntax would be: print_r($x[0]);
$x->set(2, '098/888 - 888'); //seting element at index=2. Standard PHP syntax would be: $x[2] = '098/888 - 888';
print_r($x->get()); // >> Array ( [0] => 091/111-111 [1] => 092/222-222 [2] => 098/888 - 888 )
// So lets explore some different behaviour unlike standard PHP array. This approach is named "Double-index" and it's mean that you can retrieve any element of list
// by they order-index (position) , or by associative index. But you can't manualy manage order-index, cause it is allways order of element in list. Anyway, it's not so big differents...
//$x->set(10, '098/888 - 888'); // "Error cause there is no any element on index 10." You cant manualy adding new order index!
// ..so there is assoc index available...
$x->set('Tele2', '095/555-555');
print_r($x->get()); // >> Array ( [0] => 091/111-111 [1] => 092/222-222 [2] => 098/888 - 888 [Tele2] => 095/555-555 )
print_r($x->get('Tele2')); // >> 095/555-555
print_r($x->get(3)); // >> 095/555-555
print_r($x->get('3')); // >> null // unlike integer, string 3 reffers at assoc index which is not defined
print_r($x->length()); // >> 4
$x->set(['vip'=>'091/111-111', 'tomato'=>'092/222-222', 'bonbon'=>'097/777-777']);
print_r($x->get()); // >> Array ( [vip] => 091/111-111 [tomato] => 092/222-222 [bonbon] => 097/777-777 )
print_r($x->get(1)); // >> 092/222-222
print_r($x->get('tomato')); // >> 092/222-222
print_r($x->length()); // >> 3
//The rules about "Double-index" is simple. Integr allways reffers to order index, string reffers to assoc index. You cant on your own make new order index, it's happen automatically while adding some item. But you have choice to add any assoc index if you want.
//And there is no empty index between min and max index.
//Negative index reffers from back side of list:
$x->set(-1, '097/123-456');
print_r($x->get()); // >> Array ( [vip] => 091/111-111 [tomato] => 092/222-222 [bonbon] => 097/123-456 )
$x->unset(); // $x->get() >> [] // unset make list empty. There is no any records in table, but $x instance is still here for manipulating with array.
$x->push(1); // $x->get() >> [1]
$x->addRange([5,6]); // $x->get() >> [1,5,6]
$x->addRange(1, [2,3]); // $x->get() >> [1,2,3,5,6] // Adding range on custom index position
$x->insert(-3, 4); // $x->get() >> [1,2,3,4,5,6] // Inserting value 4 on index -3
$x->indexOf(2); // >> 1
$x->indexOf("2"); // >> -1 // cause, it is different type, 2 as string is not in list.
$x->contains(2); // >> true
$x->contains("2"); // >> false
gettype($x->get(0)); // >> 'integer' // so, wen you retrive element from list, you dont lose origin type of inserted value.
$x->set([1,2,3,3,3,3,4]);
$x->countOf(3); // >> 4 // number of occurence number 3 in list
$x->deleteByIndex(0); // $x->get() >> [2,3,3,3,3,4];
$x->deleteByValue(3); // $x->get() >> [2,3,3,3,4]; // deleted only first occurence of targeted value.
$x->deleteByValue(3,2); // $x->get() >> [2,3,4]; // deleted 2 occurence, as setted 2
$x->deleteByValue(3, true); // $x->get() >> [2,4]; // true -> for delete all occurence
$x->set([1,2,3,4,5]);
$x->deleteRange(1,2); // $x->get() >> [1,4,5] // deleted range start from index=1 and delete count = 2.
$x->deleteByIndex(1); // $x->get() >> [1,5]
$x->isset(1); // >> true // cause there is element at index 1. Even we just deleted element at index 1, the index are automatic re-range and there is no empty index between min and max index.
$x->any(); // >> true // true if we have any item in list
// ..and some standard array methods
$x->set([1,2,3,4,5]);
$x->shift(); // $x->get() >> [2,3,4,5] //removed first element
$x->first(); // >> 2
$x->last(); // >> 5
$x->pop(); // >> 5
$x->get(); // >> [2,3,4]
$x->unshift(1); // >> [1,2,3,4] //push element at beggining of array
//Nested array
$x->set([[1,2,3], [4,5,6]]);
$x->get(0); // >> [1,2,3] //So, you can record or retrive nested elements. But you cant apply any dblist methods direct on nested value.
$x->get(0)->push(4); //Error, cause this is not suported. You should do that as...
$nestedList = $x->get(0);
array_push($nestedList, 4);
$x->set(0, $nestedList);
// === Reffer to other attributes in same table ===
//First, lets return telefon numbers in game:
$x->set(['091/111-111', '092/222-222', '097/777-777']);
// IF we want in same table for same entitetID (Jhon) add some items in other attribute ..we can do that on standard way:
$x = new dblist("Users",'John',"friends");
// ...after we make new instance that reffers to John list of "friends" ...everything is already known...
$x->set(['Mark', 'Laura']);
// But we could done that in a simpler way!
// ... if we already have instance of dblist, we can just change attribute of already existing dblist instance:
$x->setAttr("friends");
$x->set(['Mark', 'Laura']);
//we also could done that one-line chained:
$x->setAttr("friends")->set(['Mark', 'Laura']);
//Lets se what have at $x
print_r($x->get()); // >> ['Mark', 'Laura']
//But what if we want approach to some other attributes with out change $x ..let's see
$x->attr('telefons')->set(['092/222-222', '097/777-777']);
$x->get(); // >> ['Mark', 'Laura'] // !!! $x is not modified, and still refers to list of "friends"
$x->attr('telefons')->get(); // >> ['092/222-222', '097/777-777'] // So, use method "attr" if you dont want to modify $x, or method "setAttr" if you want modify $x attributes refference.
// And some global actions:
dblist::delete('Users', 'Jhon', 'telefons'); // delete all records from list 'telefons' associated with Jhon from table "Users";
dblist::delete('Users', 'Jhon'); // delete all records associated with Jhon from table "Users". Any list attached to Jhon will be deleted.
dblist::delete('Users'); // delete all of dblist records associated with table 'Users'. This will no affect table "Users", as any of dblist action will not.
// pre-creating table is optional. dblist manage table on its own. But we can pre-create table with some additional option included per any of table. Let's see..
dblist::createTables(["table1", ['table'=>'table2','searchable_boost'=>true]]);
// ... we have make referenced table for "table1" and for "table2";
// In this case, there is added searchable_boost to 'table2'. This boost speed for methods which deal with "serach_by_value" operation. These methods are: indexOf($value), contains($value)
// basicly it is just database index on "value" filed in table.