Git Product home page Git Product logo

php-models's Introduction

PHP Models

A PHP library that allows you to easily create and define your models using PDO

Features

  • PDO wrapper
  • FETCH_INTO made easy
  • Model helper

Installation

$ composer require lodev09/php-models

Usage

DB.php

The main \Models\DB class is a PDO wrapper used to make CRUD much easier. It is a forked code from the php-pdo-wrapper-class

// connect to your database. Store the $db instance globally -- you only need to connect to your db ONCE!
$db = new \Models\DB(DB_HOST, DB_NAME, DB_USER, DB_PASSWORD);

Available CRUD methods

  • $db->insert($sql, $binds) or $db->insert($table, $values)
  • $db->select($sql, $binds)
  • $db->row($sql, $binds) (same with select but will return single row)
  • $db->update($sql, $binds) or $db->update('table', $values)
  • $db->delete($sql, $binds) or $db->delete('table', $filters)

The default style is PDO::FETCH_OBJ

Example:

$users = $db->select("SELECT * FROM users WHERE active = 1 AND username = :username", array('username' => 'lodev09'));
var_dump($users);

Model.php

The \Models\Model class is a parent class that can be inherited to a Model class. Inheriting this class allows you to automatically map the result "row" into your model class (table). This class basically uses the PDO::FETC_INTO style and made it easier for you. Here are the steps to link your table into a class:

  1. Initiate the \Models\DB instance (see above)
$db = new \Models\DB(DB_HOST, DB_NAME, DB_USER, DB_PASSWORD);
\Models\Model::setDb($db);
  1. Create your model class. For example, a User.php class
namespace Models;

class User extends Model {
    public function getName() {
        return $this->name;
    }
}
  1. Register your table to your custom class
// somewhere in your init.php
\Models\User::register('users');

Now, you can directly get the User instance from a query. Example:

$user = \Models\User::row("SELECT id, name FROM users WHERE id = 1 AND active = 1");
// you can call the getName() method now
if ($user) {
    $name = $user->getName();
    echo 'His name is '.$name;
}

Feedback

All bugs, feature requests, pull requests, feedback, etc., are welcome. Visit my site at www.lodev09.com or email me at [email protected]

Credits

© 2018 - Coded by Jovanni Lo / @lodev09

License

Released under the MIT License. See LICENSE file.

php-models's People

Contributors

lodev09 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

scopweb dovidezra

php-models's Issues

Remove persistence or make it optional

In php-models/src/DB.php, on line 67 is \PDO::ATTR_PERSISTENT => true. This is in the __construct function.
Setting the above option to true causes errors on some systems (for ex. MariaDB), it always throws a warning/notice.
Removing/making the attribute optional can solve the problem.

Php 8.2

Hi,

would like to migrate to PHP 8.2 but trying to answer with "Creation of dynamic properties...$id is deprecated".

Do you have any suggestion?

Bye.

Transactions

Hi,
Is it possible to use your model class with transactions? I imagine something like:
$sql->begin_transaction()
$sql->run(), $sql->update(), etc..
$sql->commit()
It would be really nice!

DB getter

Could you please add a getter method to php-models/src/Model.php? JetBrains doesn't recognise the static class field.

Error line 337 Model.php

Hello, could you look at line 337 of Model.php ? This error appears:

Parse error: syntax error, unexpected '?' in D:\Apache24\htdocs\abq_new\site\claq_php_model\vendor\lodev09\php-models\src\Model.php on line 337

PHP8 causes fatal error in DB.php

Hey there!

Looks like DB::query is not compatible with PDO::query. PHP8 changed this to a fatal error.

Fatal error: Declaration of Models\DB::query($sql, $bind = null, $args = null, $style = null) must be compatible with PDO::query(string $query, ?int $fetchMode = null, mixed ...$fetchModeArgs) in ...\lodev09\php-models\src\DB.php on line 137

I changed it to this but not sure the implications of it. Removed $style as well:

public function query(string $query, ?int $fetchMode = null, mixed ...$fetchModeArgs) {
  return $this->run($query, $fetchMode, $fetchModeArgs);
}

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.