Coder Social home page Coder Social logo

vsormom's Introduction

vsormom

Very Simple Object Relational Mapping over Medoo

Features

  • Based on Medoo TSD
  • Predefined CRUD methods in Model
  • Getter & Setter support
  • JSON friendly

Define Your Model Classes

// Just inherit from the Model class
class UserModel extends Model {
    // Declare default value for each field here
    protected $_properties = [
        'id' => null,
        'username' => '',
        'password' => '',
        'nick' => '',
        'balance' => 0.0,
        'is_blocked' => false,
        'create_time' => 0
    ];
    
    // Declare data type for each field here
    protected static $_meta = [
        'id' => 'int',
        'username' => 'str',
        'password' => 'str',
        'nick' => 'str',
        'balance' => 'float',
        'is_blocked' => 'bool',
        'create_time' => int
    ];
    
    // Override the `table_name` getter
    public function get_table_name() {
        return 'user';
    }
    
    // Exclude password from the object when json stringify
    // Include head_image into the object when json stringify
    protected $_exported = ['-password', '+head_image'];
    
    // A property getter
    public function get_head_image() {
        return $this->_new ?
            'http://www.example.com/anonymous.jpg' :
            ('http://www.example.com/' . $this->id % 10 . '.jpg');
    }
}

Init Medoo

Model::setMedoo(new medoo([
    'database_type' => 'sqlite',
    'database_file' => '../all.db'
]));

Use Model Object

Create

$user = Model::getInstance('User');
$user->username = 'Joe';
$user->password = 'abc';
$user->nick = 'CamelCat';
$user->create_time = time();
$user->save();

echo $user->id;

Read

$user = Model::getInstance('User')->one(['id' => 1]);

List

$users = Model::getInstance('User')->many([
    'username[~]' => 'th_'
]);

Update

$user = Model::getInstance('User')->one(['id' => 1]);
if ($user instanceof UserModel) {
    $user->nick = 'Cool Tool';
    $user->save();
}

Delete

Model::getInstance('User')->remove(1);

vsormom's People

Contributors

syutingsong avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

bludot

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.