GithubHelp home page GithubHelp logo

php-orm-pdo's Introduction

Installing via composer and using as a lib

composer require arojunior/php-orm-pdo

create a file to overwrite the database config

use SimpleORM\core\model\Model;

class AppModel extends Model
{
    public $db_config = [
        'db_host' => '192.168.1.1',
        'db_name' => 'test',
        'db_user' => 'root',
        'db_pass' => ''
    ];
}

And then you can extend this class in your classes

use YourNamespace\AppModel;

class Example extends AppModel
{
    public $table = 't_user';
    public $pk    = 'user_id';

    public function getAll()
    {
        return $this->findAll();
    }
}

CRUD

namespace SimpleORM\app\model;

use SimpleORM\core\model\Model;

class Users extends Model
{
    	/*
        * * Basic configuration
        * These arguments are optionals
        * protected $table = 'users'; //just if the class name a table name are different
        * protected $pk = 'id'; //just if the primary key name is not id
      */	    	    
}

Creating a new user (without check)

$this->Users->create([
  'name' => 'Junior Oliveira',
  'email' => '[email protected]'
]);

Let the ORM choose if it will be created or updated. The ORM will execute the find method before to decide if will create or update data

Saving data

$this->Users->save([
  'id' => 1,
  'name' => 'Junior Oliveira'
]);

Retrieving the id

$this->Users->lastSavedId();

Updating a user with id = 1

$this->Users->update([
  'id' => 1,
  'email' => '[email protected]'
]);

Delete

$this->Users->delete(['id' => 1]);

Read

$this->Users->findAll(); // fetchAll

$this->Users->findOne(['email' => '[email protected]']);

$this->Users->findById($id);

Checking

$this->Users->exists($id);

in case of true, you cat get the data with:

$this->Users->fetch();

Functionalities if used as Framework

  • CRUD functions
  • Auto load Model classes in Controllers
  • To use the automatic functions you should use the filename and structure conventions
  • Just follow the example on /controller/UsersController.php
  • All controllers in /app/controllers folder
  • All models in /app/models folder

Convetions

  • All controllers in /app/controller path
  • All models in /app/model path
  • All views in /app/view path
  • Filenames and classes must has the same name

php-orm-pdo's People

Contributors

arojunior avatar bruno-farias avatar scrutinizer-auto-fixer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

php-orm-pdo's Issues

Composer version outdated

Repo composer says 1.2 but the project versions in composer include up to 1.1, and I also see a new release in GitHub 1.3 with interesting changes I would appreciate being pushed to composer.

bug found in Model.php

Hi
I found an error in Model.php class.
public function save($dados) { if (!isset($this->pk)) { $this->pk = 'id'; } if (isset($dados[$this->pk])) { **$this->find([$this->pk => $dados[$this->pk]]);** var_dump($this->count); exit; if ($this->count > 0) { $this->update($dados); } else { $this->create($dados); } } }
The Save method always asign 1 into count variable after you saved one record into database, which cause you can't save any other record because it enters in update statement.

how to use it as a framework

I've downloaded the package and i'm trying to make it work as a framework but I can´t get it done. I have the following folder structure
my_project/app/controllers
my_project/app/models
my_project/app/views

my_project/index.php

I just copied the folder app located in vendor\arojunior\php-orm-pdo\app into my_project folder but if I type localhost/my_project/users in the browser it doesn´t show the list of users in my db (index function) in users controllers
Is there anything to change in index.php file??

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.