PHP code example of pavlyshyn / orm

1. Go to this page and download the library: Download pavlyshyn/orm library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pavlyshyn / orm example snippets


use Pavlyshyn\Orm;
use Pavlyshyn\DB\Driver\MySQL;
use Model\User;

$orm = new Orm(new MySQL('localhost', 'test_orm', 'root', 'password'));

OR 

use Pavlyshyn\DB\Driver\MongoDB;
$orm = new Orm(new MongoDB('localhost', 'test_orm'));



namespace Model;

use \Pavlyshyn\Model;

/**
 * @tableName users
 */
class User extends Model {

    protected $id;
    protected $name;
    protected $mail;
    protected $password;

    public function setId($id) {
        return $this->id = $id;
    }

    public function getId() {
        return $this->id;
    }

    public function setName($name) {
        return $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }

    public function setMail($mail) {
        return $this->mail = $mail;
    }

    public function getMail() {
        return $this->mail;
    }

    public function setPassword($password) {
        return $this->password = $password;
    }

    public function getPassword() {
        return $this->password;
    }

}

$user = new User();
$user->name = 'Username';
$user->mail = '[email protected]';
$user->password = sha1('password');
$orm->save($user);

//get user
$user = new User();
$user->id = 1;
$user = $orm->get($user);

//update
$user->password = sha1('new password');
$orm->save($user);

$users = new User();
$tabUsers = $orm->getAll($users);

var_dump($tabUsers);

foreach($tabUsers as $user){
    echo 'Name : ' . $user->name . '<br>';
    echo 'Email : ' . $user->mail . '<br>';
    echo 'Password : ' . $user->password . '<br>';
}

$users = new User();
$user->id = 1;
$orm->deleteById($users);

$users = new User();
$orm->delete($users, 'name', 'Username');

$users = new User();
$count = $orm->count($users);
var_dump($count);

$users = new User();
$res = $orm->exist($users, 'name', 'Username');
var_dump($res);