EllisLab text mark
Advanced Search
2 of 18
2
   
Gas ORM
Posted: 08 November 2011 01:42 PM   [ Ignore ]   [ # 21 ]   [ Rating: 0 ]
Avatar
Joined: 2010-12-20
1586 posts

Updated to v.1.1.2

Changes :
Support custom key name and cutom table name, in relationship properties.

For convention and example, see public gist for Gas.

 

 Signature 

“In Code We Trust.”


CI Library : Gas ORM | Proxy

 
Posted: 09 November 2011 06:54 AM   [ Ignore ]   [ # 22 ]   [ Rating: 0 ]
Avatar
Joined: 2008-03-19
163 posts

Just updating and custom foreign key works perfectly.
But, I got one more question or maybe asking for a new feature. My table name contains prefix and suffix. Prefix is configurable via config.php which is

$config['my_db_prefix''qe_'

Also, my suffix is depends on user’s session data.
Below script can describe my problem (for simplicity I made my prefix and suffix has empty value).

This is models/wifes_gas.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Wifes extends Gas {

 
//public $table = 'wife';
 
 
public $relations = array(
    
'belongs_to' => array('user' => array()),
 );

 function 
__construct() 
 
{
  $prefix 
''// just dummy for test purpose
  
$suffix ''// just dummy for test purpose
  
$CI =& get_instance();
  
//$prefix = $CI->load->config->item('my_db_prefix'); // to get prefix 
  //$suffix = $CI->tablename->get_suffix(); // to get suffix
  
$this->table $prefix.'wife'.$suffix;
 
}
 
 
 
function _init()
 
{

 }

And, this is my controller

$users Gas::factory('user')->with('wifes')->all();

  foreach(
$users as $single_user)
  
{
   
//var_dump($single_user->wifes);
   
echo $single_user->name;
   
$wife_name = ($single_user->wifes == FALSE) ? '' $single_user->wifes->name// dipakai bila menggunakan with('user_profiles');

   
echo ' - '.$wife_name;
   echo 
'<br>';
  

If I run that controller, I can got all user’s name but without the wife’s name. Please help me

Many many thanks…

 Signature 

AutoACL - Access Control Library++ with Very Private Page (VPP) Control
AutoCRUMB - The simplest breadcrumb helper ever
Autocrumb on bitbucket
Integrate CI 2 with Eclipse
———————————————————————————————-
Website: ardinoto.blogspot.com

 
Posted: 09 November 2011 12:20 PM   [ Ignore ]   [ # 23 ]   [ Rating: 0 ]
Avatar
Joined: 2010-12-20
1586 posts

@ardinotow

All of your tables have same prefix and suffix? Or is it different between each other?

Or, you mean database prefix in config/database.php, this line :

$db['default']['dbprefix'''

yes?

If yes, because Gas is using CI AR, it will automatically doing that for you. So if you have set above setting(dbprefix), your $table properties can contain only ‘wifes’ (or leave it blank, if it same with your Gas model’s name), without any prefix even if your real table in your database have prefix. Also, you shouldnt have constructor in your Gas models, to craft your table’s name like that, remove it.

 

 Signature 

“In Code We Trust.”


CI Library : Gas ORM | Proxy

 
Posted: 09 November 2011 05:33 PM   [ Ignore ]   [ # 24 ]   [ Rating: 0 ]
Avatar
Joined: 2008-03-19
163 posts
toopay - 09 November 2011 12:20 PM

@ardinotow

All of your tables have same prefix and suffix? Or is it different between each other?

Or, you mean database prefix in config/database.php, this line :

$db['default']['dbprefix'''

yes?

If yes, because Gas is using CI AR, it will automatically doing that for you. So if you have set above setting(dbprefix), your $table properties can contain only ‘wifes’ (or leave it blank, if it same with your Gas model’s name), without any prefix even if your real table in your database have prefix. Also, you shouldnt have constructor in your Gas models, to craft your table’s name like that, remove it.

 

All my tables have the same prefix, and yes I will use

$db['default']['dbprefix'''


But, how about suffix?
The suffix is depends on user’s session, so I create a library to get table’s suffix. My company made an ERP that can hold several companies in one installment. We decide to store different company’s data on one database with different suffix. So, If users are from company A, they will get data from qe_vendor_001, for users from company B, they will get data from qe_vendor_002, and so on.
I use constructor because I think from it I can load my suffix form library : $CI->tablename->get_suffix().
Thanks.

 Signature 

AutoACL - Access Control Library++ with Very Private Page (VPP) Control
AutoCRUMB - The simplest breadcrumb helper ever
Autocrumb on bitbucket
Integrate CI 2 with Eclipse
———————————————————————————————-
Website: ardinoto.blogspot.com

 
Posted: 09 November 2011 10:12 PM   [ Ignore ]   [ # 25 ]   [ Rating: 0 ]
Avatar
Joined: 2010-12-20
1586 posts

Updated to v.1.2.0

Changes :
- Custom relationship setting (through, foreign_key, foreign_table)
- Hooks points callback(_before_check, _after_check, _before_save, _after_save, _before_delete, _after_delete)

@ardinotow, if you need to load library/helper or set/overide class properties to used later, use _init (it works similar with constructor)

 Signature 

“In Code We Trust.”


CI Library : Gas ORM | Proxy

 
Posted: 10 November 2011 04:23 AM   [ Ignore ]   [ # 26 ]   [ Rating: 0 ]
Avatar
Joined: 2008-03-19
163 posts

Wow, fast reply and solved all my problem as well. Now, It ready to use on my project.
Many many thanks. Keep it update

 Signature 

AutoACL - Access Control Library++ with Very Private Page (VPP) Control
AutoCRUMB - The simplest breadcrumb helper ever
Autocrumb on bitbucket
Integrate CI 2 with Eclipse
———————————————————————————————-
Website: ardinoto.blogspot.com

 
Posted: 10 November 2011 09:07 PM   [ Ignore ]   [ # 27 ]   [ Rating: 0 ]
Avatar
Joined: 2007-03-17
6 posts

am new with this and enjoying it, thanks for this great library

 
Posted: 11 November 2011 12:02 AM   [ Ignore ]   [ # 28 ]   [ Rating: 0 ]
Joined: 2011-10-20
18 posts

Good job, I will begin to use it in my projects. Thanks.

 
Posted: 11 November 2011 10:18 AM   [ Ignore ]   [ # 29 ]   [ Rating: 0 ]
Avatar
Joined: 2010-12-20
1586 posts

Updated to v.1.2.1

Changes :
- Self-referential and adjacency column(hierarchical data).
- Adding version constant.
- Small refactoring (just to keep it small)

For you, that questioning what is adjacency column, or what is self referential, the best example of this kind of data is something like comments. In this case, each comment can be a reply to other comment, mean they reference themself within one table. If you have this kind of table, you can working on it by specify self option in your relations properties(it can be has_one, has_many or belongs_to, depend on how you want to modelling your data). Usage example is included in unit testing controller.

 Signature 

“In Code We Trust.”


CI Library : Gas ORM | Proxy

 
Posted: 11 November 2011 10:30 AM   [ Ignore ]   [ # 30 ]   [ Rating: 0 ]
Joined: 2010-08-15
154 posts

Does the orm support table/model properties - i.e. a whitelist of fields that can be saved? This should be run before any insert or update actions.

Something like:

private function filter_attributes($data)
{
 
//if no attributes have been set up, then just return the data
 
if (empty($data) OR empty($this->attributes) OR ! is_array($this->attributes))
 
{
  
return $data;
 
}

 
// loop through all the items in the array (usually from POST),
 // and only return those that are accessible
 
foreach($data as $key => $value)
 
{
  
if (in_array($key$this->attributes))
  
{
   $ret[$key] 
= (is_array($value)) ? $value[0] $data[$key];
  
}
 }
 
 
return $ret;
 
Posted: 11 November 2011 11:06 AM   [ Ignore ]   [ # 31 ]   [ Rating: 0 ]
Avatar
Joined: 2010-12-20
1586 posts

@JonoB,

Your models can have several callbacks to interact with internal Gas lifecycle. Look at hooks point callbacks, available points are :

_before_check() : run before validate your input (if you set your fields rule in _init function, and you passing TRUE within save() method).

_after_check() : run after validate your input.

_before_save() : run before INSERT or UPDATE operation.

_after_save() : run after above operation.

_before_delete() : run before DELETE operation.

_after_delete() : run after DELETE operation.

In above case, you may want to use _before_check or _before_save callback, depend on how you want to doing it.

So, suppose you have some post data like this one :

$_POST = array(
    
'name' => 'Mr. Foo',
    
'email' => 'foo@bar.com',
    
'username' => 'foo',
    
'somefields' => 'Lorem',
    
'otherfields' => 'Ipsum',
);

// and you try to save it
$new_user Gas::factory('user')->fill($_POST);

$new_user->save(TRUE); 

Then in your Gas model, if you want to only save name, email and username, you can executed your own filtering method on _before_save() callback, mean you want to validate all the data, but only several of it will be saved/updated.

// within your Gas model, suppose you set your own properties
public $attributes = array('name''email''username');

//...
//...

function _before_save()
{
    
// Here you can see all populated data, dont try to check $_POST here.
    // Gas has its own mechanism, so overide/change/modify $_POST will not affect anything
    
$old_data $this->filled_fields();
    
    
// Here you can implement your own filtering method
    
$new_data $this->filter_attributes($old_data);

    
// to overide all set data, use set_fields() method
    
$this->set_fields($new_data);

    return 
$this;

This mean you can also modified some (or all) of it, or adding some common fields (like timestamps) before save/update data.

 Signature 

“In Code We Trust.”


CI Library : Gas ORM | Proxy

 
Posted: 12 November 2011 10:33 AM   [ Ignore ]   [ # 32 ]   [ Rating: 0 ]
Joined: 2009-01-21
48 posts

Great job toopay, rly thx!!!!

I have one question: have any technique matter if i load one model in another? i do this, and i want to know if i have some problem because this in case i migrate to Gas.

Nice job man, thx.

 
Posted: 12 November 2011 01:03 PM   [ Ignore ]   [ # 33 ]   [ Rating: 0 ]
Avatar
Joined: 2010-12-20
1586 posts
ClaudioX - 12 November 2011 10:33 AM

I have one question: have any technique matter if i load one model in another?

You mean load a model files manually (more related with autoload models configuration), or use another model instance within a model (if yes, is the foreign model defined in relationship or not)? And what “matter” you mean above, raw performance or what?

The best practice when you using Gas, is defined your table/model relation, and works in thats constraint. Off course you can work with another model, which might not related at all, with factory method (eg, work with sale model in role model, which is not related each other), but that will make your model harder to maintain. Also with clear constraint, you can maximize your queries performance, since with relations properties defined, you can eager load your related model/table.

 Signature 

“In Code We Trust.”


CI Library : Gas ORM | Proxy

 
Posted: 12 November 2011 05:41 PM   [ Ignore ]   [ # 34 ]   [ Rating: 0 ]
Joined: 2009-01-21
48 posts

First, thx for the reply,

I mean:

use another model instance within a model

what “matter” you mean?

Conflicts, key mix, that staffs.

thinking in what you say, the most part of time when i use this, is probaly for what the library is propose to do! tongue rolleye

Let me show:

$this->load->model("page_class");        //table page
$this->load->model("page_data_model");   //table page_data
$this->load->model("page_adress_model"); //table page_adress

$PG     = new Page_class();
$PGData = new Page_data_model();
$PGAdr  = new Page_adress_model();

$PG->setAll();
$PGData->setAll();
$PGAdr->setAll();
$PG->setData($PG->Data);
$PG->setAdr($PG->Adr);
$PG->save(); 
 
Posted: 13 November 2011 02:11 AM   [ Ignore ]   [ # 35 ]   [ Rating: 0 ]
Avatar
Joined: 2010-12-20
1586 posts
ClaudioX - 12 November 2011 05:41 PM

Conflicts, key mix, that staffs.

No, it wouldnt be a matter.

ClaudioX - 12 November 2011 05:41 PM

thinking in what you say, the most part of time when i use this, is probaly for what the library is propose to do! tongue rolleye

I already stated that you can do anything, even if you plan to work with several models within one model, which not related at all (based by your schema) : it will still works.

But with clear constraint, you can gain maximum performance(mean extra benefits).wink

 Signature 

“In Code We Trust.”


CI Library : Gas ORM | Proxy

 
2 of 18
2