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]
  • #136 / Jul 24, 2012 2:47am

    Tulimiekka

    2 posts

    @toopay
    Ah thank you a lot. Works perfectly. I kinda didn’t know what does that key in the array mean and just followed docs, but seems it can be named to anything and then just used as $someobject->the_array_key().

    EDIT: Hey was there a “through” relationship for 2.1 too? I remember it in the old version but it’s not documented in the new. Or how would I use a pivot tables other columns than the ID columns for relationships? It has own unique ID as primary key and everything. It is an EAV kinda system.

  • #137 / Jul 24, 2012 5:10am

    toopay

    1583 posts

    EDIT: Hey was there a “through” relationship for 2.1 too? I remember it in the old version but it’s not documented in the new. Or how would I use a pivot tables other columns than the ID columns for relationships? It has own unique ID as primary key and everything. It is an EAV kinda system.

    Learn from documentation or by an example.

  • #138 / Aug 26, 2012 1:22am

    rei

    67 posts

    Hi, I’m doing something like this:

    $all_users = Model\User::with('blog')->all();
    
                    foreach ($all_users as $some_user)
                    {
                            echo 'User ' .$some_user->username.' and he seems have several kids:';
    
                            foreach ($some_user->blog() as $blog)
                            {
                                echo '
    ';
                                echo '
    ';
                                echo '
    ';
                                echo '
    ';
                                echo $blog->title ;
                                echo '
    ';
                                echo '
    ';
                                echo '
    ';
                                echo '
    ';
                            }

    But why the output is:

    User kevin and he seems have several kids:User francie and he seems have several kids:
    
    
    francies first post
    
    
    francies second post
    
    
    francie 3rd


    It is just showing francie’s post, but not showing kevin’s post? Is this a bug?


    I enabled CI profiler and the created query is this:

    0.0004    SELECT *
    FROM (`user`) 
    0.0004    SELECT * FROM `blog` WHERE `blog`.`user_id` IN (2)

    Please kindly reply asap. Thanks in advance 😊

  • #139 / Aug 27, 2012 2:14pm

    toopay

    1583 posts

    @rei,

    Nice spotted. This bug seems only occured at plain all method without any conditional query. Here the quickfix :
    1. On Gas\Core class, on line 1578, replace

    $matched_id[$original_id][] = $child_instance;

    with:

    if (isset($fk_original_ids))
    {
     $matched_id[$fk_original_ids[$original_id]][] = $child_instance;
    }
    else
    {
     $matched_id[$original_id][] = $child_instance;
    }

    2. Still on Gas\Core class, on line 1335, replace

    if ( count($tuples) == 1)
    {
     // Easy one, this is one level path
     $ids = $original_ids = array($resource[$identifier]);
    }

    with :

    if (strpos($tuple, '<') === FALSE)
    {
     // Reset the ids matchers
     $fk_original_ids = array();
    
     // Revert for belongs to relationship
     foreach ($resources as $orig_index => $resource)
     {
      // Populate the ids
      $fk_original_ids[$original_ids[$orig_index]] = $resource[$identifier];
    
      // Generate new token and empty holder for each original identifier
      $token = $original_table.':'.$identifier.'.';
      $index = $resource[$identifier];
      $holder->set("$token$index", array($index));
     }
    
    $ids = $fk_original_ids;
    }

    i will include this fix on next patch release.

  • #140 / Aug 27, 2012 7:08pm

    rei

    67 posts

    Thank you sir I’ll try applying your fix this afternoon 😊 and nwei does gas orm support eager loading of associations of associations of association or deep relationships and so on and so on? What I mean is like chaining the ->with();  Like this:

    $all_users = Model\User::with('blog')->all()->with('post')->all()->with('comments')->all->with('author')->all();

    Does Gas ORM support that? Because I really like your Gas ORM because of its ease of use and it is fast and very light. I have tried DataMapper Orm and I like it also but it’s eager loading is different, it returns the result like a JOIN statement w/c repeats the parent for every child so you will do the filtering of the parent in the php side, so I have switched to Gas ORM but I found a bug when using with() method, so I switched to PHP ActiveRecord and I its doing the job well, the bloat is alright for me but the performance is so bad, it is very slow and eager loading is really very very slow, in fact their lazy loading is much much more faster than their eager loading w/c is very weird for me. But now you have a fix for GAS ORM so I will definitely switch back to your awesome ORM if it supports deep relationships. And if it supports that, how can I do that sir? Thank you very much in advance sir! 😊

     

     

  • #141 / Aug 28, 2012 2:10am

    toopay

    1583 posts

    It does support recursive entities, but not with that (*cough) syntax. For instance, if you need to retrieve ‘comments’ entities (that fetched through blog and post entities), you could have something similar with this with longer path. Please read documentation regarding relationship, especially “through” part.

  • #142 / Aug 28, 2012 2:57am

    rei

    67 posts

    uhm. this is what I really need:

    1. To fetch all the users
    2. To fetch all the posts posted by each user
    3. To fetch all the comments for each post

    So I can do something like this:

    foreach ($users as $user)
    {
        echo $user->name;
    
        foreach ($user->posts as $post)
        {
            echo $post->content;
    
            foreach ($post->comments as $comment)
            {
                 echo $comment->content;
            }
        }
    }

    I know its possible to do that using lazy loading, but I wanted to use eager loading to do that. Is that really possible sir? Can I ask a little sample from you on how to do that? And nwei okay sir I will read the docs 😊 Thanks! 😊


    EDIT:

    I have read the docs:

    As you see in has_and_belongs_to example, there are another option to set-up many-to-many relationship. This option exists, and could be your option whether your pivot table is not only used to linked two tables, but also have its own primary key and other columns. If you are in this situation, dont panic. You still can using the second option : through.

    So, instead having has_and_belongs_to values, now your user model would be something like :

    class User extends Gas {

    public $relations = array(

    'has_one' => array('wife' => array()),

    'has_many' => array('kids' => array()),

    'has_many' => array('job' => array(

    'through' => 'job_user',

    )),

    );

    }

    But I still can’t understand on how will I achieve what I wanted in using that :(

    So if u have time sir please kindly give me a sample/code snippet on how to achieve wanted. Thank you very much in advance.

     

     

     

  • #143 / Aug 28, 2012 1:13pm

    Odie

    3 posts

    Hi,

    I’ve been trying to use Gas ORM inside a library class, lets say Authenticate class.
    Its always throwing an error class does not exist.

    Anyone have any idea?
    Hope anyone can help out.

    TIA

  • #144 / Aug 28, 2012 2:09pm

    Odie

    3 posts

    Never mind, able to figure it out 😊

    Thanks

  • #145 / Aug 28, 2012 3:50pm

    Omegote

    6 posts

    Hi. I’m trying to build some custom forms and I need to read the fields defined in my GAS model. The problem is that it seems the “_init” function does not get called until I call some other function, like ::all().

    Should I manually call the _init method or there’s another way?

  • #146 / Aug 30, 2012 12:37pm

    toopay

    1583 posts

    @rei, its possible to directly eager load either post entity or comments entity from user entity as long you define them, but it is impossible (for now) to eager load them as an entity trees as you want. I’ll consider this idea in next release version.

    @Omegote, _init method always called everytime you instantiate your model. But for your case, you may want to take a look at extension.

  • #147 / Aug 30, 2012 7:09pm

    rei

    67 posts

    oh, thats what I’m trying to do. eager load them and make a tree, nwei thanks sir for your help sir, and I’m looking forward for your next awesome version w/c will support trees because that will be really very helpful 😉 Thanks again!

  • #148 / Sep 05, 2012 1:34pm

    Omegote

    6 posts

    How can I get the type of a field? While checking the contents of the self::$fields array, looks like the type is in the “annotations”, but the position within that array is inconsistent.

    Is there a method that returns the field type?

  • #149 / Sep 19, 2012 6:42pm

    Omegote

    6 posts

    How can I get the type of a field? While checking the contents of the self::$fields array, looks like the type is in the “annotations”, but the position within that array is inconsistent.

    Is there a method that returns the field type?

    Anyone about this?

  • #150 / Sep 20, 2012 12:44am

    toopay

    1583 posts

    @Omegote, try :

    $annotations = \Gas\Core::identify_annotation(self::$fields['annotations']);
    print_r($annotations);
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases