Skip to content
This repository was archived by the owner on Apr 23, 2024. It is now read-only.

rafayhingoro/vmodel-idiorm-extension

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

VModel - Idiorm/Paris Extension

https://github.com/rafayhingoro/vmodel-idiorm-extension


Built on top of Idiorm & Paris.

Tested on PHP 5.4.0+ - may work on earlier versions with PDO and the correct database drivers.

Released under a MIT license.

Documentation

Setup Guide


  1. Download release source code and move vmodel.php in your Models Directory

  2. on any model where you want to use vmodel simply use extends like this

     <?php 
      class YourModelName extends VModel {
        //your code 
      }
  3. for validating before save Model File YourModelName.php

    <?php 
     class YourModelName extends VModel {
         public $_isValid = true; //by default making it valid
         
         //fields which are in database
         public $_fields = array(
             'id',
             'name',
             'email',
             'contact_number',
             'description'
         );
         
         //adding rules for fields 
         protected $_rules = array(
             'category_id' => 'required',
             'name' => 'required',
             'email' => 'required|email',
             'contact_number' => 'required',
             'description' => 'required'
         );
     }
    ?>

    Controller File ControllerUser.php

     <?php 
     class ControllerUser {
          try {
          
             // $form is array of inputs here's an example 
             // $form['username'] = 'user1234';
             // $form['password'] = 'mySecretPassword';
             // $form['email'] = '[email protected]';
             // ... 
            
              $oModel = Model::factory("YourModelName")->create();
              $oModel->validateForm($form); //throws exception if form is not valid
              ...
          } catch (Exception $ex) {
             $ex->getMessage(); //validation error will be available here 
          }
      }

Validation Modal

class Item extends VModel {
    public $_fields = array(
        'id',
        'shop_id',
        'cat_id',
        'name',
        'description',
        'price',
        'created_on',
        'created_by',
        'updated_by',
        'updated_on'
    );
    public $_rules = array(
        'shop_id' => 'required',
        'cat_id'     => 'required|int',
        'name'       => 'required',
        'description' => 'required|limit<500',
        'price'       => 'required|int'
    );
}
$form = array(
    'shop_id' => 23,
    'cat_id' => 12,
    'name' => 'XYZ product',
    'description' => 'some description ...',
    'price' => '12.00'
);

$item = Model::factory('Item')
         ->saveForm($form);
//or
$item = Model::factory('Item')
         ->validateForm($form);
 $item->shop_id = $form['shop_id'];
 $item->cat_id = $form['cat_id'];
 $item->name = $form['name'];
 $item->description = $form['description'];
 $item->price = $form['price'];
 $item->save();

1.0.0 - released 2017-03-19

  • Initial release

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages