Code Monkey home page Code Monkey logo

datamapper's Introduction

pulunomoe/datamapper

Super simple data mapper ORM for PHP.

Requirements

  • PHP 8+
  • PDO

Features

  • CRUD (wow!)
  • More incoming (soon-ish)

Usage

1. Install with composer

composer require pulunomoe/datamapper

2. Create your entity class

<?php

// 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;
}

3. Call the DataMapper

<?php

// 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);

API

  • Find All

findAll(string $orderBy = '', bool $desc = false, int $limit = 10, int $offset = 0): array

  • Find All By

findAllBy(string $column, string $value, string $orderBy = '', bool $desc = false, int $limit = 10, int $offset = 0): array

  • Find One

function findOne(int $id): ?EntityClass

  • Create

create(EntityClass $object): EntityClass

  • Update

update(EntityClass $object): EntityClass

  • Delete

function delete(int $id): void

Changelog

  • v0.1 : Initial version
  • v0.2 : Added ordering, limit, and find all by

Shameless plug

Like this library? Buy me some coffee or buy me some cendol

datamapper's People

Contributors

pulunomoe avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.