Implementação do padrão de projeto active record em PHP

v2.0 2019-03-30 12:16 UTC

This package is auto-updated.

Last update: 2024-04-29 04:09:12 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

Implementação do padrão de projeto active record em PHP

Table of Contents

Prerequisites

  • PHP 7.1+
  • PDO driver for your respective database

Supported Databases

  • MySQL
  • SQLite
  • PostgreSQL
  • Oracle

Installation

Require via composer

$ composer require edsononildo/orm

Create an index.php file and require the autoload.php of composer

<?php

include 'vendor/autoload.php';

After that, let's to do all necessary configuration

use Bonfim\ActiveRecord\ActiveRecord;

ActiveRecord::config('mysql:host=localhost;dbname=testdb', 'username', 'password');

Basic CRUD

Retrieve

These are your basic methods to find and retrieve records from your database:

// Retrieve all records
$posts = Post::all();

// Retrieve records with specific keys
$posts = Post::select(['title']);

// Find records with a condition
$posts = Post::find('WHERE id = ?', [1]);

Create

Here we create a new post by instantiating a new object and then invoking the save() method:

$post = new Post();
$post->title = 'My first blog post!!';
$post->author_name = 'Edson Onildo';
$post->save();
INSERT INTO `posts` (`title`, `author_name`) VALUES ("My first blog post!!", "Edson Onildo");

Update

To update you would just need to find a record first and then change one of its attributes.

$post = Post::find('WHERE `id` = ?', [1])[0];
echo $post->title; // 'My first blog post!!'
$post->title = 'Some title';
$post->save();
UPDATE `posts` SET title='Some title' WHERE id=1;

Delete

Deleting a record will not destroy the object. This means that it will call sql to delete the record in your database but you can still use the object if you need to.

$post = Post::find('WHERE `id` = ?');
$post->delete();
DELETE FROM `posts` WHERE id=1;

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email inbox.edsononildo@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.