PHP code example of mfurman / pdomodel

1. Go to this page and download the library: Download mfurman/pdomodel 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/ */

    

mfurman / pdomodel example snippets


  $config = [
    'DB_CONNECTION'  => 'mysql',
    'DB_HOST'        => 'localhost',
    'DB_PORT'        => '3306',
    'DB_DATABASE'    => 'testdb',
    'DB_USERNAME'    => 'access',
    'DB_PASSWORD'    => 'password',
    'LOGGER_DIR'     =>  'logger directory started from base directory of project (default "./errors")',
  ];

   \mfurman\pdomodel\PDOAccess::get($config);

    use \mfurman\pdomodel\PDOAccess;
    use \mfurman\pdomodel\dataDb;

    class User extends dataDb 
    {
        private $users_table = 'users';

        public function __construct($commit=true) 
        {         
            parent::__construct(PDOAccess::get(), $this->users_table, $commit, false);
        }    
        
        // return associative array, table of users
        public function getAll() :array
        {
            return $this->read('*')->get();
        }

        // return associative array, users data
        public function getById(int $id) :array
        {
            return $this->read('*','id = '.$id)->get();
        }

        // return associative array, users data
        public function getByName(string $name) 
        {
            return $this->read('*','user_name = "'.$name.'"')->get();
        }

        // return id of new row,
        public function add(array $data) :int               
        {
            $this->set(array('user_name'=>$data['user_name']));        
            return $this->insert();
        }

        // return true or false
        public function updateOne(array $data, int $id) :bool
        {
            if (empty($this->read('*','id = '.$id)->get())) return false;
            $this->set(array('user_name'=>$data['user_name']));
            $this->update($id);
            return true;
        }  

        public function deleteById(int $id) :bool
        {
            if (empty($this->read('*','id = '.$id)->get())) return false;
            $this->delete_where('id = '.$id);
            return true;
        }
    }

    use Myvendor\Myapp\Models\Task;
    use Myvendor\Myapp\Models\User;

    class TaskRepository 
    {
        private $task;
        private $user;
        
        (...)
        
        public function store(array $data) :array
        {
            $this->task = new Task(false);                      // - set autocommit to false (true/false)
            $this->user = new User(false);                      // - set autocommit to false (true/false)
            
            // inform PDO about start transaction
            $this->task->begin();
            
            $this->user->getByName($data['user_name']);
            if ($this->user->is()) $data['user_id'] = $this->user->get('id');
            else {
                $this->user->reset();
                $data['user_id'] = $this->user->add($data);
            }

            $id = $this->task->add($data);
            
            // inform PDO to commit ealier started transaction, if something go wrong - commit will rollback
            $this->task->commit();
            
            // this toggle change model to autocommit transactions (true/false)
            $this->task->set_commit(true);
            return $this->task->getById($id);
        }        


    parent::__construct(object $dbaccess, $this->myTable, $commit=true, $log_rec=false);

    $this->set_commit(bool $flag=true);

    $this->reset();

    $this->set_table(string $table);

    $this->get_table();

    $this->is();

    $this->set('name','value');
    
    //or we can set multiple 
    $this->set(array(
        'name01' => 'value01'
        'name02' => 'value02'
        'name03' => 'value03'
        )
    );

    $this->get('name');
    
    //or if is multiple data table -  for eg. table of 'users'
    $this->get('name',0); // - get name of first user

    $this->get_flat();

    $this->del('name');

    $this->begin();

    $this->commit();

    $this->read('*', 'name = "John"') :object
    
    // or
    $this->read(['ID', 'name', 'surname', 'city'], 'name = "John" AND age > 23', 'DESC', 1) :object

    $this->insert() :int

    $this->update($id) :object

    $this->update_where('name = "John"') :object

    $this->delete_where('name = "John"') :object

    $this->exists(['name' => 'John', 'surname' => 'Smith']) :bool
    
    //or
    $this->exists(['name' => 'John', 'surname' => 'Smith'], 'ID != '.$id, $this->otherTable) :bool    

    $_SESSION['user_id'] = 12               // set when user log in - for eg ID from users table,
    $_SESSION['user_name'] = 'John Smith'   // set when user log in - username from users table,