Part of the EllisLab Network
   
3 of 25
3
Gas ORM
Posted: 08 November 2011 12:42 PM   [ Ignore ]   [ # 21 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1544
Joined  12-20-2010

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

Profile
 
 
Posted: 09 November 2011 05:54 AM   [ Ignore ]   [ # 22 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  162
Joined  03-19-2008

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

Profile
 
 
Posted: 09 November 2011 11:20 AM   [ Ignore ]   [ # 23 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1544
Joined  12-20-2010

@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

Profile
 
 
Posted: 09 November 2011 04:33 PM   [ Ignore ]   [ # 24 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  162
Joined  03-19-2008
toopay - 09 November 2011 11:20 AM

@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

Profile
 
 
Posted: 09 November 2011 09:12 PM   [ Ignore ]   [ # 25 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1544
Joined  12-20-2010

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

Profile
 
 
Posted: 10 November 2011 03:23 AM   [ Ignore ]   [ # 26 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  162
Joined  03-19-2008

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

Profile
 
 
Posted: 10 November 2011 08:07 PM   [ Ignore ]   [ # 27 ]  
Summer Student
Avatar
Total Posts:  6
Joined  03-17-2007

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

Profile
 
 
Posted: 10 November 2011 11:02 PM   [ Ignore ]   [ # 28 ]  
Summer Student
Total Posts:  18
Joined  10-20-2011

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

Profile
 
 
Posted: 11 November 2011 09:18 AM   [ Ignore ]   [ # 29 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1544
Joined  12-20-2010

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

Profile
 
 
Posted: 11 November 2011 09:30 AM   [ Ignore ]   [ # 30 ]  
Lab Assistant
RankRank
Total Posts:  159
Joined  08-15-2010

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;
Profile
 
 
   
3 of 25
3