ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Gas ORM 2

March 16, 2012 11:36pm

Subscribe [39]
  • #46 / Mar 29, 2012 5:38am

    Thibaut L.

    9 posts

    Hi !

    Thanks for this awesome ORM ! i love it !

    But i have a problem with relationships.

    I have a Class Foo and an Class Bar
    Foo has Many Bar
    Bar belongs to Foo.

    When I use Foo->bar(), i want to have my Bar datas order by Date.

    I try

    self::$relationships = array(
                'bar' => ORM::has_many('\\Model\\Bar', array('order_by:date[asc]')),
            );

    But nothing change if I try with ASC/DESC in the profile I don’t see OrderBy ...
    What am i doing wrong? :(

    Thanks 😊

  • #47 / Mar 29, 2012 6:01pm

    toopay

    1583 posts

    So how can I make my code universal for reading 1 or many objects?

    For example, if i have the same page to display 1 as I do many (the view) but the model either gets all or a limited number, even a specific entry, then I have to test to see if it’s a array before presenting, or should I do a conversion to make it a array with a single entry?

    A quick cheatsheat :

    // Finder method :
    // all                => will return an array of object if records exists, or NULL
    // find_by_column     => will return an array of object if records exists, or NULL
    // find               => will return a Gas instance (object), or NULL
    // avg, max, min, sum => will return a Gas instance (object), or NULL
    // first, last        => will return a Gas instance (object), or NULL

    You may also want to look up Result extension.

  • #48 / Mar 29, 2012 6:04pm

    toopay

    1583 posts

    But nothing change if I try with ASC/DESC in the profile I don’t see OrderBy ...
    What am i doing wrong? :(

    No, your report is a legitimate bug. For version 2.0, only select work in additional query setting within relationship. I include this fixed within 2.1

  • #49 / Mar 29, 2012 6:08pm

    toopay

    1583 posts

    Updated to v.2.1.0

    Changes :
    1. Support related entities at write operation, refer to updated documentation for usage. You may need to delete your browser cache to see the updated documentation.
    2. Result Extension included.
    3. Fix several minor issue on relationships methods.

  • #50 / Mar 30, 2012 2:41am

    indCI

    11 posts

    A quick cheatsheat :

    // Finder method :
    // all                => will return an array of object if records exists, or NULL
    // find_by_column     => will return an array of object if records exists, or NULL
    // find               => will return a Gas instance (object), or NULL
    // avg, max, min, sum => will return a Gas instance (object), or NULL
    // first, last        => will return a Gas instance (object), or NULL

    As far as I have noticed, “all”, “find_by_column” and “find” return an array of objects if there are multiple results and a single object (not in an array) if there was only one result.

    On the other hand, getting records through relations ( like $user->messages() ) will always return an array of objects even if there was only one result.

    This seems to cause some inconsistencies. Sometimes it’s good that it works that way but sometimes you have to change the type between an array and single object.


    Some examples:
    user table has one-to-one relationship with userdata table. You call $user->userdata() and maybe you want their address so $user->userdata()->address. But wait, that causes an error, because userdata() returned an array of single object.

    In another place you want to list all users so you call Model\User::all(). Now, let’s loop through them with foreach. Oh, but there is only one user and all() returns an object instead of an array with a single object, so the foreach causes an error.

  • #51 / Mar 30, 2012 3:30am

    Thibaut L.

    9 posts

    Updated to v.2.1.0

    Changes :
    1. Support related entities at write operation, refer to updated documentation for usage. You may need to delete your browser cache to see the updated documentation.
    2. Result Extension included.
    3. Fix several minor issue on relationships methods.

    Hi 😊

    I’ve just downloaded 2.1.0 version and I have some issues :

    Message: array_merge() [function.array-merge]: Argument #1 is not an array
    Filename: libraries/gas.php
    Line Number: 88

    L88 :

    $config = array_merge($CI->config->item('gas'), array('migration' => $CI->config->item('migration')));

    If I try to change to :

    $config = array_merge(array($CI->config->item('gas')), array('migration' => $CI->config->item('migration')));

    New error :

    Message: Undefined index: auto_create_models
    Message: Undefined index: auto_create_tables

    And CI write :

    Invalid runtime configuration.

    For the moment I use old libraries/gas.php and everything is ok and order_by works fine 😊

     

  • #52 / Mar 30, 2012 6:06am

    mecharius

    21 posts

    Hi Toopay,
    First of all great job again with version 2… I’ve spent a couple of hours working on an application using v2 and its great!

    However one niggle is the return types of find_by_** mentioned by indCI.  I think for consistency it would be better to always return null or an array of objects…  as you say in your docs, if you are only after one record you can always apply a limit of 1!

    As far as I have noticed, “all”, “find_by_column” and “find” return an array of objects if there are multiple results and a single object (not in an array) if there was only one result.

    ...

    In another place you want to list all users so you call Model\User::all(). Now, let’s loop through them with foreach. Oh, but there is only one user and all() returns an object instead of an array with a single object, so the foreach causes an error.


    Thanks 😊

  • #53 / Mar 30, 2012 6:06am

    toopay

    1583 posts

    @Thibaut L, you donwload from Spark or the zipped files?

  • #54 / Mar 30, 2012 6:12am

    toopay

    1583 posts

    @mecharius, did you upgrade into Gas ORM 2.1? find_by_collumn will alyways return an array of object if records exists, and NULL if there is no records. Also, there is result extension included, you can use those extension as :

    $result = Model\ORM\User::result()->all();
    
    // Easy way to debug over your result
    echo $result;
    
    // Convert the instances into an array of instance
    $users = $result->as_array();
    
    foreach ($users as $user)
    {
      // $user will be a typical Gas Instance
    }
    
    // Convert all instance's record into various format
    $records_array = $result->to_array(); // assoc array
    $records_json = $result->to_json();   // JSON 
    $records_xml = $result->to_xml();     // XML
    
    // Convert all instance's record into Gas\Data object
    $records_data = $result->to_data();
    
    $first_user = $records_data->get('data.0'); // Return the first index
    $first_user_name = $records_data->get('data.0.name'); // Return the first index name
    $some_user_name = $records_data->get('data.100.name', 'Default Name');
    // And so on…
  • #55 / Mar 30, 2012 6:29am

    Thibaut L.

    9 posts

    @Thibaut L, you donwload from Spark or the zipped files?

    Zipped file from link in DOC v2.1

  • #56 / Mar 30, 2012 7:02am

    toopay

    1583 posts

    As far as I have noticed, “all”, “find_by_column” and “find” return an array of objects if there are multiple results and a single object (not in an array) if there was only one result.


    In another place you want to list all users so you call Model\User::all(). Now, let’s loop through them with foreach. Oh, but there is only one user and all() returns an object instead of an array with a single object, so the foreach causes an error.

    One thing that consistent, since this ORM follow Active Record pattern, is that every Gas Instance is always hold a record not two or three or more. The returning values of finder methods, in v2.0.0 is depend on the fetched records.

    Update to 2.1.0, the quick cheat sheat is for this version. You may also looks for result extension, as more conveniency way to working with finder. You always can easily extend it and adding some functionality which fit for your application.

  • #57 / Mar 30, 2012 7:16am

    toopay

    1583 posts

    On the other hand, getting records through relations ( like $user->messages() ) will always return an array of objects even if there was only one result.

    Since version 1.X.X, relationship method always works this way :
    has_one => returning an object
    belongs_to => returning an object
    has_many => returning an array of object

  • #58 / Mar 30, 2012 9:31am

    toopay

    1583 posts

    Patch for v.2.1.0

    Changes :
    1. Finder behaviour. As default, all finder now have legacy behaviour as you know since version 1.X, but for all and find_by_collumn can accept a parameter that will force Gas to behave as indCI and mecharius pointed above. You can refer to below file, or for detailed usage example, see the updated documentation. (you may need to delete your browser cache).
    2. Adding one phpunit file to ensure above changes.

    How to update :
    If you previously download the zipped files, just re-download and replace the old one. If you already installed via Spark, update using this command from your comman line :

    $ php tools/spark reinstall -v2.1.0 gas
  • #59 / Mar 31, 2012 10:43am

    Khrome83

    22 posts

    toopay thanks for implementing the relationship insert.

    =)

    Just to clarify, if I have a pivot table, I use -

    $job->related->set('entities.user.pivot', array(

    Otherwise I would use -

    $job->related->set('entities.user.child', array(

    The other question i had was, if I have a one-to-many relationship,

    lets say I have a Roles table which defines permissions, and a Users table. It is a One-To-Many with Users belonging to Roles, and Roles having many Users. So Users table has a ‘roles_id’.

    If I was inserting a new User, since I am not making a new role, I would first need to lookup the ID of the role, or have it record to insert with the user? In most cases I assume I would know the ID of the role from the form and such. This is just a curiosity, and could be helpful?

    Would it make sense to have entities.user.parent and/or a option to find. Is .parent even needed in general, or does child work both ways depending on where your inserting from?

    $user = Model\Users::make(array(
            'name' => 'bob',
            'email' => '[email protected]',
    ));
    
    $user->related->set('entities.roles.parent', array(
            'roles_id' => function() {
                    $role = Model\User::limit(1)->find_by_name('admin', FALSE);
                    return $role->id;
            },
    ));
    
    $user->save();

    Additionally, in the existing example of the documentation, if the ID isn’t known can I do this -

    $job->related->set('entities.user.pivot', array(
            'user_id' => function() {
                    $user = Model\User::limit(1)->find_by_name('Bob', FALSE);
                    return $user->id;
            },
    ));

    I can see how the second PARAM is useful by passing FALSE we get a single object.

  • #60 / Mar 31, 2012 1:06pm

    Khrome83

    22 posts

    A quick question about the best way to use Gas ORM in this situation.

    I have the following tables -

    Users
    Profiles
    Articles
    Comments

    This is the relationships I am considering -

    Users
    Has One - Profile
    Has Many - Articles, Comments

    Profiles
    Belongs To - Users (or would Has One be Better)

    Articles
    Belongs To - Users (or would Has One be Better)
    Has Many - Comments

    Comments -
    Belongs To - Articles
    Belongs To - User (or would Has One be Better)


    So with finder, when I do All, would this cause any problems. For example if I want to get all Articles by a User as well as the comments and the comments users names…

    Everything I did before was many - to - many. I am concerned about the circular relationships this might cause. Whats the best way to handle this?

    I am assuming if I found User(1),

    I would not end up with something like this to get the comments user name. (not actual code)

    User()->Articles->Comments->User()->Name

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases