PHP code example of budgetdumpster / crud-service

1. Go to this page and download the library: Download budgetdumpster/crud-service library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

budgetdumpster / crud-service example snippets




use \BudgetDumpster\Services\CRUDService;
use \BudgetDumpster\Exceptions\ModelNotFoundException;
use \YourNameSpace\Models\Person;
use \Monolog\Logger;

$id = 'abcdef123456789';
$logger = new Logger('test');
// continue configuring monolog

$crudService = new CRUDService($logger);
$person = new Person();

try {
    model = $crudService->retrieve($person, $id);
} catch (ModelNotFoundException $e) {
   // log the error
}

 

use \BudgetDumpster\Services\CRUDService;
use \BudgetDumpster\Exceptions\ModelNotFoundException;
use \YourNameSpace\Models\Person;
use \Monolog\Logger;
use \RuntimeException;

$data = [
    'first_name' => 'Test',
    'last_name' => 'Person',
    'phone' => '555-555-5555',
    'email' => '[email protected]'
];

$logger = new Logger('test');
//continue configuring logger

$crudService = new CrudService($logger);
$person = new Person();
$id = 'abcdef123456789';

try {
    $model = $crudService->create($person, $data, $id);
} catch (RuntimeException $e) {
    // log error
}


use \BudgetDumpster\Services\CRUDService;
use \BudgetDumpster\Exceptions\ModelNotFoundException;
use \YourNameSpace\Models\Person;
use \Monolog\Logger;
use \RuntimeException;

$data = [
    'first_name' => 'Test',
    'last_name' => 'Person',
    'phone' => '555-555-5554',
    'email' => '[email protected]'
];

$logger = new Logger('test');
// continue configuring logger

$crudService = new CRUDService($logger);
$person = new Person;
$id = '123456789abcdef';

try {
    $model = $crudService->update($person, $data, $id);
} catch (ModelNotFoundException $e) {
    // log error
} catch (RuntimeException $e) {
    // log error
}


use \BudgetDumpster\Services\CRUDService;
use \BudgetDumpster\Exceptions\ModelNotFoundException;
use \YourNameSpace\Models\Person;
use \Monolog\Logger;
use \RuntimeException;

$logger = new Logger('test');
// continue configuring logger

$crudService = new CRUDService($logger);
$person = new Person;
$id = '123456789abcdef';

try {
    // returns a boolean value to identify whether the method call was successful
    $result = $crudService->delete($person, $id);
} catch (ModelNotFoundException $e) {
    // log error
} catch (RuntimeException $e) {
    // log error
}


use \BudgetDumpster\Services\CRUDService;
use \BudgetDumpster\Exceptions\ModelNotFoundException;
use \YourNameSpace\Models\Person;
use \Monolog\Logger;
use \RuntimeException;
use \InvalidArgumentException;

$logger = new Logger('test');
// continue configuring logger

$crudService = new CRUDService($logger);
$person = new Person;
$page = 1;
$per_page = 1;
// optional parameter - defaults to id != null
$filter = ['field' => 'first_name', 'operator' => '=', 'value' => 'Test'];

try {
    $collection = $crudService->retrieveAll($person, $page, $per_page, $filter);
} catch (InvalidArgumentException $e) {
    // log error
} catch (RuntimeException $e) {
    // log error
}