1. Go to this page and download the library: Download genesis/persistence 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/ */
genesis / persistence example snippets
namespace myApp;
use Genesis\Services\Persistence\Model\BaseModel;
class MyItemModel extends BaseModel
{
protected $name = 'text not null unique';
protected $description = 'text default null';
protected $userId = 'integer not null';
/**
* Enforced by the constructor.
*
* @return array
*/
protected function getRequiredFields()
{
return [
'userId'
];
}
}
use Genesis\Services\Persistence;
// Configuration for the databaseService.
$dbParams = ['dbEngine' => 'sqlite', 'dbPath' => BASE . '/db.sqlite'];
// Create a database service.
$databaseService = Persistence\DatabaseService($dbParams);
// Create a mapper service which depends on the databaseService.
$mapperService = new Persistence\MapperService($databaseService);
namespace myApp;
class App
{
/**
* Inserting into the database.
*/
public function insert()
{
$mapperService = ...
// Create a new model object for insertion.
$myModel = MyItemModel::getNew(['userId' => 33432])
->setName('whatever you want.')
->setDescription('A great description');
// Insert new record in the database.
$mapperService->persist($myModel);
if ($myModel->getId()) {
$this->message('Record saved successfully.');
}
}
/**
* Updating the database.
*/
public function update()
{
$mapperService = ...
// Get a specific record from the database mapped to your model object.
$myModel = $mapperService->getSingle(MyItemModel::class, ['id' => $form->get('item_id')]);
// Update model with desired data. Note the setters/getters are provided out of the box by just
// extending the baseModel which are based on the properties your model has.
$myModel
->setName('whatever you want.')
->setDescription('A great description');
// Update the record in the database.
$mapperService->persist($myModel);
// Get all MyItemModels back with the name `howdy`, order them by the id descending.
$myItemModels = $mapperService->get(MyItemModel::class, ['name' => 'howdy'], ['id' => 'desc']);
// Use the retrieved models somehow.
...
}
}
namespace myApp;
use Genesis\Services\Persistence;
class SomeRepository
{
public function getSomeItemsForPastSevenDays(Persistence\Contracts\MapperService $mapperService)
{
// Get the database table name based on the model.
$table = $mapperService->getTableFromClass(Representations\Sale::class);
// Prepare a more complex query, we can only bind objects back if we have all columns corresponding to the model.
$query = "SELECT * FROM `{$table}` WHERE `dateSold` > (SELECT DATETIME('now', '-7 day')) AND `userId` = {$userId}";
// Execute the query using the databaseService layer and get the data back.
$data = $mapperService->getDatabaseService()->execute($query);
// Bind the data to the model and return the collection.
return $mapperService->bindToModel(Representations\Sale::class, $data);
}
}
namespace myApp;
class ExampleGetAssociated
{
public function getAssociatedUser()
{
$mapperService = ...
// Get a random model for this example.
$myItemModel = $mapperService->getSingle(MyItemModel::class, ['id' => 15]);
// Get the user associated with the $myItemModel object.
$user = $mapperService->getAssociated(User::class, $myItemModel);
...
}
}
namespace myApp;
use Genesis\Services\Persistence;
class App
{
public function deleteExampleOne()
{
$mapperService = ...;
// If say a delete request comes in.
if ($this->issetGet('delete')) {
// Get the product Id from the request.
$productId = $this->getParam('productId');
// Delete the record.
$mapperService->delete(Product::class, ['id' => $productId]);
}
...
}
// If say the product object was passed into the method.
public function deleteExampleTwo(Product $product)
{
$mapperService = ...;
// This will delete the record from the database as long as the getId() method on the object returns
// the id of the record.
$mapperService->delete($product);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.