dev-master
9999999-devBasic PHP model framework
proprietary
The Requires
- php >=7.0
- sergeytsalkov/meekrodb v2.3
by Antonio Giliberto
Wallogit.com
2017 © Pedro Peláez
Basic PHP model framework
, (*1)
Run:, (*2)
composer require giliweb/phusky
Open composer.json and add, (*3)
"scripts": {
"phusky_setup": "giliweb\\phusky\\Install::init"
}
Run, (*4)
composer phusky_setup -- -path=classes_folder -dbhost=db_host -dbname=db_name -dbuser=db_user -dbpassword=db_password
Phusky will now create a set of PHP classes inside the folder you indicated in the -path parameter, (*5)
How to use Phusky for your project:, (*6)
All of the following examples are based on the data available in the "example_db.sql", (*7)
Ok, let's say we want to insert in our DB a new Car object. Model's name is "Testarossa" and we already know the brand's ID is 3:, (*8)
<?php
$car = new Car([
"name" => "Testarossa",
"brands_id" => 3
]);
$car->create();
But what if the brand is not yet present in the "brands" table? Just this simple:, (*9)
<?php
$car = new Car([
"name" => "Testarossa",
"brand" => [
"name" => "Ferrari"
]
]);
$car->create();
Phusky will provide to create and insert in our DB the brand with name "Ferrari" and associate the new car object to the new created brand., (*10)
Same way we can bind some dependencies to the car object:, (*11)
<?php
$car = new Car([
"name" => "Testarossa",
"brand" => [
"name" => "Ferrari"
],
"colors" => [
[
"id" => 3
],
[
"name" => "Red"
]
]
]);
$car->create();
As you can see we are going to bind 2 colors to the car object. The first is already known and present in the DB, we know his ID is 3. The second is not yet in the DB, so we are going to create it. The output of $car will be:, (*12)
Car Object
(
[name] => Testarossa
[brand] => Brand Object
(
[name] => Ferrari
[id] => 26
)
[colors] => Array
(
[0] => Color Object
(
[id] => 3
[name] => Blue
)
[1] => Color Object
(
[id] => 13
[name] => Red
)
)
[brands_id] => 26
[id] => 55
)
With Phusky you can get a single record or an array of records. That data will be automatically converted to the right object type as defined in the PHP classes. To get a single record:, (*13)
<?php $car = Car::getById(3);
To get multiple records:, (*14)
<?php $cars = Car::read();
To search something you got to pass a closure to the read method, as explained in the MeekroDB documentation, ie:, (*15)
<?php
$cars = Car::read(function(){
$where = new \WhereClause('and');
$where->add("brands_id=%d", 3);
return $where;
});
The output will be something like this:, (*16)
Array
(
[0] => Car Object
(
[id] => 42
[name] => Testarossa
[brands_id] => 3
)
[1] => Car Object
(
[id] => 43
[name] => Enzo
[brands_id] => 3
)
[2] => Car Object
(
[id] => 44
[name] => LaFerrari
[brands_id] => 3
)
)
Supposing we want to know which colors are binded to those cars, we can proceed this way:, (*17)
<?php
$car->colors;
// OR
$cars->colors;
The output will include the colors info for each car object. To get a clean output it's advisable to use output method:, (*18)
<?php
$car->output();
// OR
$cars->output();
As for insert procedure, Phusky will dinamically create/update any dependencies of the main object we are going to update in the DB. The main difference is we can delete them too! If we call:, (*19)
<?php $car = Car::getById(3); // load car instance $car->colors = []; // empty the colors array $car->update(); // write on DB
Phusky will DELETE ALL of the colors from the $car object Here is another example:, (*20)
<?php $car = Car::getById(3); $car->colors; // load the colors for this car unset($car->colors[0]); // manually delete this specific color $car->update(); // write on DB
In this last case Phusky will delete just the first color., (*21)
<?php $car = Car::getById(3); $car->delete();
Basic PHP model framework
proprietary