PHP code example of pulunomoe / datamapper

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

    

pulunomoe / datamapper example snippets




// src/Entity/Car.php

namespace YourApp\Entity;

use Pulunomoe\DataMapper\Entity;
use Pulunomoe\DataMapper\EntityClass;
use Pulunomoe\DataMapper\Property;

#[Entity('tests')] // Put your table name here
class Car extends EntityClass
{
	#[Property('id')] // Put your column name here
	public int $id;   // "$id" is a special column for primary key

	#[Property('brand')]
	public string $brand;

	#[Property('model')]
	public string $model;
}



// Initialize the data mapper by passing a PDO instance and the entity class name
$dm = new DataMapper($pdo, Car::class);

// Retrieve all cars
$dm->findAll();

// Retrieve all cars by brand
$dm->findAllBy('brand', 'Danke Motoren Werke');

// Retrieve a single car with the id = 1
$dm->findOne(1);

// Save a car to the database
$car = new Car();
$car->brand = 'Honyabishi';
$car->model = 'Super 9001';
$car = $dm->create($car);

// Update a car
$car->model = 'Super 9001 Mark II Type R GT-MAXXX';
$car = $dm->update($car);

// Delete a car with the id = 1
$dm->delete(1);