CodeIgniter Forums
Gas ORM - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Gas ORM (/showthread.php?tid=46224)

Pages: 1 2 3 4 5 6 7 8 9 10 11


Gas ORM - El Forum - 10-23-2011

[eluser]toopay[/eluser]
A lighweight and easy-to-use ORM for CodeIgniter. Gas was built specifically for CodeIgniter app. It uses standard CI DB packages, also take anvantages of its validator class. Gas provide methods that will map your database table and its relation, into accesible object.
[h4]For download or recent update, look at GasORM @ GitHub. This library also available at Sparks. For guide and example go to Documentation for version 1.x.x.[/h4]

[h3]Requirements[/h3]
1. PHP v.5.2.x
2. CodeIgniter v.2.x.x

[h3]Features[/h3]
1. Supported databases : cubrid, mssql, mysql, oci8, odbc, postgre, sqlite, sqlsrv.
2. Support multiple database connection.
3. Support modular models directories and sub-models directories.
4. Multiple relationship (has_one, has_many, belongs_to, has_and_belongs_to) with custom relationship setting (through, foreign_key, foreign_table, self).
5. Auto-create models from database tables and vice versa, and auto-synchronize models-tables by creating migrations file.
6 Per-request caching.
7. Self-referential and adjacency column/data (hierarchical data).
8. Eager Loading.
9. Various finder method (can chained with most of CI AR syntax) and aggregates.
10. Validation and auto-mapping input collection, with minimal setup.
11. Hooks points, to control over your model.
12. Extensions, to share your common function/library/helper/plugin across your model instances.
13. Transaction and other CI AR goodness.
14. Command Line Interface.

[h3]Planned Features[/h3]
1. Support for tree traversal data.
More useful features.

NOTE : latest version is v.1.4.3 (also compatible with the latest CI 2.1.0), if you using < v.1.3.0, please update. Also note that Auto-create models from tables and vice versa require CI v.2.1.x


Gas ORM - El Forum - 01-18-2012

[eluser]sqwk[/eluser]
Just discovered Gas and figured out how to use it within a couple of minutes. Great job. One thing though? Is there a way to select what fields are being selected from the DB?

Code:
$buyers = Gas::factory('users')->with('emails')->all()

Can I tell Gas that I only want the 'id' and 'username' fields from the users table and the 'id' and 'email' field from the emails table? I found out that I can add ->select() to tell Gas what to get from the users table, but what about the email table?

Also, can I eager load multiple levels? For example if there is another m:m relationship after emails?


Gas ORM - El Forum - 01-18-2012

[eluser]toopay[/eluser]
[quote author="sqwk" date="1326916072"]
Can I tell Gas that I only want the 'id' and 'username' fields from the users table and the 'id' and 'email' field from the emails table? I found out that I can add ->select() to tell Gas what to get from the users table, but what about the email table?
[/quote]
This is what added on in the latest version. You could have something like :
Code:
function _init()
{
   // Define relationships
   self::$relationships = array(
      'wife' => ORM::has_one('\\Model\\Wife', array('select:id,name')),
      'job'  => ORM::has_many('\\Model\\Job\\User => \\Model\\Job', array('select:id,name')),
   );  
   // Define fields definition
   self::$fields = array(
      'username' => ORM::field('char[10]', array('required', 'callback_username_check')),
   );
}
In user table, so when you load your related entity, you could include some pre-process query you want (select, order_by and limit). Look around at the repo, in the develop branch within unit test section for furthermore usage.
[quote author="sqwk" date="1326916072"]
Also, can I eager load multiple levels? For example if there is another m:m relationship after emails?[/quote]
In latest version, sure. You could have something like :
Code:
// Relationship statement block....
'acl'  => ORM::has_many('\\Model\\Role\\User => \\Model\\Role <= \\Model\\Acl\\Role => \\Model\\Acl', array('select:id,name')),
for example, which actually connectiong 'user' table in several tier level of tuple(s) to reach the 'acl' table.


Gas ORM - El Forum - 01-19-2012

[eluser]sqwk[/eluser]
[quote author="toopay" date="1326949314"]
Code:
// Relationship statement block....
'acl'  => ORM::has_many('\\Model\\Role\\User => \\Model\\Role <= \\Model\\Acl\\Role => \\Model\\Acl', array('select:id,name')),
for example, which actually connectiong 'user' table in several tier level of tuple(s) to reach the 'acl' table.[/quote]

Not entirely sure about the syntax here. Could you explain what all the \\, => and <= are doing?


Gas ORM - El Forum - 01-19-2012

[eluser]toopay[/eluser]
'\\' is used, because all gas model should have namespace, and in string variable we cannot write '\'.
And '=>' or '<=' is to simplify our relationship paths. The relationship interpreter, will parsing and break down above path into paired tuples as follow :
Code:
//'=>' mean LEFT is belongs to RIGHT
//'<=' mean RIGHT is belongs to LEFT

// Level 1 :
// \Model\User <= \Model\Role\User

// Level 2 :
// \Model\Role\User => \Model\Role

// Level 3 :
// \Model\Role <= \Model\Acl\Role

// Level 4 :
// \Model\Acl\Role => \Model\Acl
To generate the most efficient SQL query to reach ACL from USER, if there is a request from method like :
Code:
$user = Model\User::find(1);

// Then to retrieve corresponding acl, its done in one SQL query
// Bellow will output corresponding acl for user with primary key = 1
var_dump($user->acl());
Otherwise, like in earlier version of this ORM, identical task with above will need with something like :
Code:
$user1 = Gas::factory('user')->find(1);
$roles = $user1->roles;
$acl   = array();

foreach ($roles as $role)
{
   $acl[] = $role->acl;
}

// Here, we could finally get all user'a acl(s),
// But it will execute so much SQL (which supposed to resolved by each entity definition)
var_dump($acl);
Which both inefficient in its executed SQL command(s) and code(s) usage.


Gas ORM - El Forum - 01-19-2012

[eluser]sqwk[/eluser]
Is it just me or is that a completely different syntax to what is listed in the wiki?


Gas ORM - El Forum - 01-19-2012

[eluser]toopay[/eluser]
Its for 2.0, and i did not update the wiki yet. Version 1.X.X syntax will remain, as is. There are several things left, to completely release this new version. I just answer your previous question, regarding possibility to achive those task, within this new version.


Gas ORM - El Forum - 01-23-2012

[eluser]brunobarros[/eluser]
@toopay I'm trying to implement Gas on my already born system. I do something similar to Wordpress. I have a table "content" with posts, categories etc.
===========
content
===========
id
title
category_id
===========

If the line is a category the category_id is '0', otherwise it is the ID of a category.
In this case, do I need one single Model? I tried both, mapping with one and two models, one to posts and another to categories... but I could not get the category title.

Ex: $content->title
Ex: $content->category->title

Is it possible?
Tnks


Gas ORM - El Forum - 01-23-2012

[eluser]toopay[/eluser]
@brunobarros,
You could use self-referential option if you go with one table.


Gas ORM - El Forum - 01-23-2012

[eluser]brunobarros[/eluser]
Yeah man, I did it and works. See...

Code:
Content_gas
public $relations = array(
        'belongs_to' => array('content' => array(
                'self' => TRUE,
                'foreign_key' => 'category_id')
            )
}

And I called the GAS:
Code:
$posts = Gas::factory('content')->where(array('category_id !=' => 0))->with('content')->all();

Inside de loop:
Code:
foreach ($posts as $post) {          
    echo $post->title . ' - category: ' . $post->content->title . '<br>';
}

Is there a way to do it better? If a could reference the category using: $post->category->title would be much better, don't you think?