PHP Classes

Oracle PHP Class Model: Create model classes stored in Oracle using OCI

Recommend this page to a friend!
  Info   View files Documentation   View files View files (4)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 88 This week: 2All time: 9,957 This week: 96Up
Version License PHP version Categories
oracle_php 1.0.0Custom (specified...5PHP 5, Databases, Design Patterns
Description 

Author

It provides a base class that can be extended to create both model classes and controller classes for tables stored in Oracle using OCI, in order to access data records.

The model automatically extract metadata for the table, no need to describe them, just set the source table and you can start to retrieve and modify records in the database.

The base classes provide functions that can:

- Create new records
- Update records
- Delete records
- Find records that match given criterion
- Use transactions
- Skip columns

Innovation Award
PHP Programming Innovation award nominee
January 2020
Number 4
Most PHP applications need to store information in databases. Often they use model classes to map objects into database table records.

This package provides a base class to create model classes that can map object data into Oracle database table records.

These model classes require minimal configuration as they are able to extract the list of table fields from the database server automatically.

Manuel Lemos
Picture of Codreanu Florin
Name: Codreanu Florin <contact>
Classes: 1 package by
Country: Romania Romania
Age: ???
All time rank: 442880 in Romania Romania
Week rank: 106 Up3 in Romania Romania Up
Innovation award
Innovation award
Nominee: 1x

Documentation

oracle_php

Lighweight library to work with oracle using php

Automatically detects the columns of the table

Of course, you need to have enabled OCI8 php extension.

See wiki page for documentation. https://github.com/floorin/oracle_php/wiki

After the loadModel() call, results an object that contains the columns of the table as property, and the following methods: findFirst(), find(), next(), exportAsArray(), create(), update(), delete(), beginTransaction(), rollback(), fetchTable(), checkIfIsNull(), commit(), reset() and of course sql_query(). If you find useful and want to use this class but need new features, give me ideas.

https://codreanu.net/lighweight-library-to-work-with-oracle-using-php

A very basic example how to use.

Oracle table:

CREATE TABLE EMPLOYEE
(EMPNO NUMBER NOT NULL,
FIRSTNAME VARCHAR(30) NOT NULL,
LASTNAME VARCHAR(30) NOT NULL,
BIRTHDATE NOT NULL,
HIREDATE NOT NULL,
JOB VARCHAR(30) NOT NULL,
SALARY NUMBER(7,2)
)

Create php file "EmployeeModel.php":

<?php
require('path_to_\OCIdb.php');
class EmployeeModel extends OCIdb{
    public function setSource(){
        $this->_table_name='employee';
    }    
}
?>

Create php file "EmployeeController.php":

<?php
require('EmployeeModel.php');
$OCIDB=new OCIdb();
$employeeTable=$OCIDB->loadModel('EmployeeModel');
$employeeTable->empno=123;
$employeeTable->firstname='Florin';
$employeeTable->lastname='Florin';
$employeeTable->setDataFormat('dd.mm.yyyy');
$employeeTable->birthdate='10.05.1971';

if(!$employeeTable->create('commit')){
	$status="error";
	$messages = $employeeTable->_error_message; 
}else{
	$status="success";
}
?>

Another basic example:

<?php
require('EmployeeModel.php');
$OCIDB=new OCIdb();
$employeeTable=$OCIDB->loadModel('EmployeeModel');
$employeeTable->findFirst([
		    'conditions' => 'empno = :vempno',
		    'bind'       => [
					":vempno" => 123
				    ]
		    ]);
$employeeTable->lastname='Codreanu';
if(!$employeeTable->update('commit')){
	$status="error";
	$messages = $employeeTable->_error_message; 
}else{
	$status="success";
}
?>

Exporting json:

<?php
require('EmployeeModel.php');
$OCIDB=new OCIdb();
$response = new stdClass();
$response->status="getting salary greather than 1000";
$employeeTable=$OCIDB->loadModel('EmployeeModel');
$employeeTable->find([
		    'conditions' => 'salary>1000',
		    'order by'   =>'salary desc'
		    ]);
$response->rows=$employeeTable->exportAsArray();
die(json_encode($response));		    
?>

Just very stupid playing around:

<?php
require('EmployeeModel.php');
$OCIDB=new OCIdb();
$employeeTable=$OCIDB->loadModel('EmployeeModel');
$employeeTable->find([
		    'conditions' => 'lastname like :vlastname',
		    'bind'       => [
					":vlastname" => 'JOHN%'
				    ],
		    'order by'=>'firstname asc'
		    ]);

if($employeeTable->rowExists && $employeeTable->empno==123)
	{ 
	$employeeTable->lastname='Codreanu';
	$employeeTable->update();
	}
$employeeTable->next();
if($employeeTable->rowExists && $employeeTable->lastname='test';)
	{
	$employeeTable->delete();
	}

if($employeeTable->next())//or we can check if next() is still getting data
	{
	$employeeTable->setDataFormat('dd.mm.yyyy');
	$employeeTable->birthdate='25.05.1971';
	$employeeTable->update();
	}
$employeeTable->commit();
?>

Adding a bit of complexity declaring the model ("EmployeeModel.php"):

<?php
require('path_to_\OCIdb.php');
class EmployeeModel extends OCIdb{
    public function setSource(){
        $this->_table_name='employee';
    }    

      public function initialize()
    {
	//set format data
        $this->setDataFormat('dd.mm.yyyy');
	
	//skip selecting some columns
	$this->skipAttributes(['hiredate','salary']);
	
	//skip some columns on INSERT operation
	$this->skipAttributesOnCreate(['empno']);
	
	//skip some columns on UPDATE operation
	$this->skipAttributesOnUpdate(['empno','firstname']);
    }
    
    /*
    of course, you can redeclare, for instance, an insert/update/delete or whatever parent's method in this model
    and implement your validations.
    */
}
?>

  Files folder image Files  
File Role Description
Accessible without login Plain text file example.php Example example
Accessible without login Plain text file LICENSE Lic. License text
Plain text file OCIdb.php Class Class source
Accessible without login Plain text file README.md Doc. Read me

 Version Control Unique User Downloads Download Rankings  
 75%
Total:88
This week:2
All time:9,957
This week:96Up