PHP code example of dominicgisler / spdo

1. Go to this page and download the library: Download dominicgisler/spdo 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/ */

    

dominicgisler / spdo example snippets




use \Gisler\Spdo\AbstractModel;

class Person extends AbstractModel
{
    /**
     * @var int
     */
    public $person_id;

    /**
     * @var string
     */
    public $name;
}



use \Gisler\Spdo\AbstractRepository;

class PersonRepository extends AbstractRepository
{
    /**
     * PersonRepository constructor.
     */
    public function __construct()
    {
        parent::__construct(
            new PDO('mysql:host=localhost;dbname=database', 'username', 'password'),
            'person',
            'person_id',
            Person::class
        );
    }
}



use \Gisler\Spdo\Collection;

$repo = new PersonRepository();

// get
$col = $repo->getAll(); // get all entities
$col = $repo->get(['name' => 'Max']); // get entities with name="Max"
$person = $repo->getObject(['person_id' => 1]); // get single entity where person_id=1

// save (insert / update)
$repo->save(new Person(['name' => 'Max Muster'])); // inserts a new entity
$repo->save(new Person(['person_id' => 1, 'name' => 'Max Muster'])); // updates entity with person_id=1

// delete
$repo->delete(new Person(['person_id' => 1, 'name' => 'Max Muster'])); // delete entity with person_id=1

// save collection
$col = new Collection();
$col->append(new Person(['name' => 'Max Muster']));
$col->append(new Person(['name' => 'Fritz Meier']));
$repo->insertCollection($col);