ORM Classes need not to be written from scratch.they are automatically generated by reading ORM Maps. ORM Maps are textual, Highly Human readable configuration files that Manages different tables and links a connection against them.
Zigmoyd Parses those ORM Maps (Configuration Files) and generates the ORM Classes. Although the ORM Classes are auto generated, they <s>can be</s> meant to be edited if required.to handle Custom access methods, adding triggers etc.....
etc/conf.d
Directory with orm.map.php extensionAlthough ORM Maps are textual, Human readable configuration Files you dont need to bother about writting a configuration file from scratch. zigmoyd have Command line tools for that automatically too. However its worth to know the structure of that file cause without knowing that you cant edit that file maually.
This is an example ORM Map. where I am mapping two Tables std with alias name Student and std_score with alias Name Marks. and std Table have a Column called syd_name which I'll use with an alias name Name and std_id column is auto Incremented so Its readonly. and you can see how the validation Filters are applied. and these two shares same Connection attribute dataConn (dicsussed in the next section). Zigmoyd commander is capable to generates these coiguration files by itself even with the filters and Error messages.
connection as schoolConn{ } table std as Student{ readonly std_id as Id{ } writable std_name as Name{ required: Student's Name Must not be left Blank string: Student's Name Must be a String } } table std_score as Marks{ readonly id as Id{ } writable student_id as StudentId{ required: StudentId Must not be left Blank int: StudentId Must be an Integer } writable subject as Sub{ required: Subject Must not be left Blank string: Subject Name must be a valid string } writable marks as Marks{ optional: __NULL__ default: 0 int: Marks Obtained by the Student Must be an Integer } }
connection as schoolConn{ }
table admin as Admin{ readonly id as Id{ } writable usr as User{ required: Column `usr` on Table `admin` Must not be left Blank string: Column `usr` on Table `admin` Must be a String } writable psw as Password{ required: Column `psw` on Table `admin` Must not be left Blank string: Column `psw` on Table `admin` Must be a String } writable sign as Signature{ optional: __NULL__ __default: New User string: Column `psw` on Table `admin` Must be a String } }
you can mark a Column as readonly or writable. (e.g. auto incremented columns can be kept as readonly as you never write any data on those columns cause they are written automatically). just like here we did with id column.
You can set a default value that will be used that will be used if that field is left blank.
You can set Validation Rules and their corresponding error messages That will be checked for validation before writting anything on that column which will auto filter the data whenever you try to write something. which leads to no no unexpected value in the database even if you forget form validation at submitting time.
ormMapName.orm.php
in apps/model/orm
directory in your Project's Directory.and one file is created per ORM Map basis.
so if the above mentioned ORM Map's name is studentOrm.orm.map.php
ORM Classes for this Map will be created in a file Called studentOrm.orm.php
which will be stored in projects/home/apps/model/orm
Directory. and now the question is what will they contain.
In studentOrm.orm.php there will be two Classes.
Assume there is an ORM Map Like the following (N.B. My Project name is home).
connection as homeConn{ } table admin as Admin{ readonly id as Id{ } writable usr as User{ required: Column `usr` on Table `admin` Must not be left Blank string: Column `usr` on Table `admin` Must be a String } writable psw as Password{ required: Column `psw` on Table `admin` Must not be left Blank string: Column `psw` on Table `admin` Must be a String } }
<?php //{Generating the List of Classes in this file $__zigOrmLoadedClassList[] = 'Admin'; //} ?> <?php // This File is auto generated By Zigmoyd ORM Handler by reading the ORM Map on File // /media/SRV/zman/projects/home/etc/conf.d/AdminOrm.orm.map.php if(!class_exists('Admin'))://{ Writting Class Admin for Table admin class Admin extends manualOrmTable{// Class manualOrmTable ultimately inherits zOrm_inc Class var $connAlias = 'manualConn';//Connection Alias name var $realTableName = 'admin';//Real Table Name var $__struct = 'I7fX19fQ........';//Writting the serialized easy to parse Structure of the ORM Map. function Admin(){ //Writting The Constructor Method if(function_exists('overload'))overload(get_class($this)); $this->__struct = unserialize(base64_decode($this->__struct)); $this->init(); } } //} endif; if(!class_exists('AdminRowSet')): class AdminRowSet extends manualOrmRowSet{// class manualOrmRowSet ultimately inherits from zigRowSet class //you can write your own custom Access Methods methods here function AdminRowSet(){ if(function_exists('overload'))overload(get_class($this)); } } endif; ?>
class Admin extends homeOrmTable{ var $connAlias = 'schoolConn';//Connection Alias name var $realTableName = 'admin';//Real Table Name var $__struct = 'Tzo4OiZyI7fX19fQ........';//Writting serialized Structure of preparsed ORM Map function Admin(){ //Writting The Constructor Method if(function_exists('overload'))overload(get_class($this)); $this->__struct = unserialize(base64_decode($this->__struct)); $this->init(); } }
$this->db
in Controller. and while attaching zigmoyd does something liek $this->db->Admin = new Admin()
. Here the Admin Class represents the Admin Table. class AdminRowSet extends homeOrmRowSet{ //you can write your own custom Access Methods methods here function AdminRowSet(){ if(function_exists('overload'))overload(get_class($this)); } }
$this->db->Admin
to access the data of Admin Table. e.g. You can execute $this->db->Admin->findNameById(1)
will write similer to select name frrom admin where id = 1
the above operation will return you a string data representing the name returned, remember it will not return a rowset if only one column is fetched for the shake of simplicity.
On your Controller $this->db
acts as an container of all tabe level Objects which
as the ORM Class ultimately inherits zOrm_inc Class You can use all methods of that class and its parent classes. therre are magic methods on zOrm_magic Class. non-magic methods on zOrm_nomagic class and some other base methods too.
$this->db->Student->findNameById(1)
to get the Name of the student who's id is 1. You can execute $this->db->Student->findNameByIdGreaterThan(1)
find an array of strings holding names of students who's id is greater than 1. you can also execute $this->db->Student->findName()
to get an array of strings All Names e.g. you can execute $this->db->Student->findColBySearch(value)
or $this->db->Student->findColBySearchOp(value)
or $this->db->Student->findCol()
Col should be replaced by their Column names. Op Stands for Operator Equals = NotEquals != GreaterThan > GreaterThanEquals >= LessThan < LessThanEquals <= Like LIKEYou can take a look at the zOrm_magic Class to see all magic methods like identifyByCol() or identifyByColOp() or zOrm_magic::setCol() etc..
You can also use findAllById(1)
which returns row(s) who's id Is/are 1 or findAllByIdGreaterThan(1)
which returns an array of RowSet Objects or findAll()
which returns al rows.
find methods are easy to use and enough powerful while fetching values of one or all column and using only one where query. But if you need a custom fetch e.g. fetch just two columns with multiple Where Quiries. at that time you need to use fetch Methods. like fetchCol() or fetchColAsAlias() if you want to select two columns use fetchCol1() and fetchCol2() so that both Col1 and Col2 will be flagged as to be selected.
You need to execute zOrm_base::export() after you do all fetch()s. $this->db->Admin->fetchName()
marks that Name column will be selected.$this->db->Admin->fetchNameAsUser()
marks that Name column will be selected, But in the ResultSet the Name Column will be Named as User.
identifyBy methods are meant to be used with fetch methods to set where Clauses.you can set as many where Caluses as you wish identifyByCol(value)
: $this->db->Admin->identifyById(1)
set's an where clause id equals 1
fetchColAsAlias(aliasName)
: $this->db->Admin->fetchNameAsUser()
marks that Name column will be selected, But in the ResultSet the Name Column will be Named as User.
with set Methods you can set values to the fields for inserting or udating. setCol(Value)
set a Column's Value. to be updated/inserted
overload()
enabled or available on your PHP Installation which is installed in most of the cases but still as a very rare case its not installed you can not use magic methods. then you can use equivalent non-magic methods provided by zOrm_noMagic Class.
insert()
Inserts data into the fields that has been set using setCol() magic method or any other non-magic method to set that field.
remove()
Removes that Specific Row. '''Remember:''' You must use identifyBy()
method to identify that Row pirtuularly, else all rows might get deleted.
update()
Update the Specific Row. '''Remember:''' You must use identifyBy()
method to identify that Row pirtuularly, else all rows might get updated.
delete()
Same as remove() just a Different Name.
export()
a Select Query by selecting the fields fetch()
ed
limit()
Pass a Limit query in it.
group_by()
Pass a group by Query in it.
having()
Pass a having query in it.
order()
Pass an order Query in it.
distinct()
manage the distinct Flag. e.g. If its set to true the select query will be distinct else not.
index()
if your export()
returns an array by default they are numarically serially Indexed. However if you pass an Index Column it will be indexed with corresponding values from that field, e.g. index('Id') sometimes seems to be very usefull cause you get the corresponding Ids as array index
and when a Rowlevel Object is used you get some more control on that Row set. getCol()
You can execute getName()
Method if there is a Column called Name in the RowSet Object.
also you can do Row level save(update) or delete operation(s) which will be discussed on another page.