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]
  • #181 / Nov 06, 2012 4:45am

    toopay

    1583 posts

    @snowfall and @necken, there was a discussion about eager load with ‘all’ method issue previously. Try this patch class : https://gist.github.com/4023670 (replace your core.php under third_party/gas/classes/core.php with those patch). Let me know if you two, still have any issues with eager loading afterward.

    @jonahdahlquist, If you need self-referencing to some entity, you actually could do it without adding more complexity within your model definition. How? Just write something like :

    /** 
     * Method for fetching child categories 
     * 
     * @return array 
     */
    public function category()
    {
      return self::find_by_parent_category_id($this->id);
    }
    
    /** 
     * Method for fetching parent category
     * 
     * @return object
     */
    public function parent()
    {
      return self::find($this->parent_category_id);
    }

    As for your request regarding additional query for child entities, there is no support for ‘where’ or other clause than ‘select’, ‘limit’, and ‘order_by’ for version 2.x.x. Maybe for the next major version.

  • #182 / Nov 06, 2012 6:12am

    snowfall

    7 posts

    Hi Toopay,

    I had already made the change to core.php, I tried using the core.php you linked to in case there were other changes but it still doesn’t work. 

    With the code I posted you can see that the all() method works one way along the relationship with the patch, but not the other.  I put the behaviour that’s failing also at the bottom of the post, where with

    $token.$original_id

    $token points towards the correct values in $holder

    lkpsite:manager_refid

    However $original_id points towards the primary key value (a site_code value), and not the foreign key value (a manager_refid value) which would produce an array result.

    Thanks for the help 😊

  • #183 / Nov 06, 2012 1:42pm

    toopay

    1583 posts

    However $original_id points towards the primary key value (a site_code value), and not the foreign key value (a manager_refid value) which would produce an array result.

    Gotcha. I’ve just update the previous patch for core.php : https://gist.github.com/4023670

  • #184 / Nov 06, 2012 3:23pm

    jonahdahlquist

    6 posts

    @toopay

    Thanks for the reply.  The issue with the category model is that the relationships are in a separate table, so I can’t query the category table directly.  Each category can have multiple parents, and multiple children

  • #185 / Nov 06, 2012 3:26pm

    jonahdahlquist

    6 posts

    @toopay

    And if there’s no way to modify the query on the foreign table through the pivot table, how would you recommend I execute a more complex query on it?  I guess I’ll have to bypass the built-in relationship functionality.

    Thanks

  • #186 / Nov 06, 2012 4:54pm

    jonahdahlquist

    6 posts

    Is there a way to chain multiple where() clauses together?  I’ve tried it, but only the last clause is in the query.

    $categories = \Model\Categories::make()->where('priority >', 2)->where('author', 482)->all();
    // Will only run 'SELECT * FROM category WHERE author = 482'
  • #187 / Nov 07, 2012 2:51am

    necken

    6 posts

    When i updated the core, this error started showing:

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘“event_images” WHERE “event_images”.“events_id” IN (1, 2, 3, 4, 5, 6, 7, 8, 9)’ at line 1

    SELECT * FROM “event_images” WHERE “event_images”.“events_id” IN (1, 2, 3, 4, 5, 6, 7, 8, 9)

    My guess is that the double quotes carries the problem!

  • #188 / Nov 07, 2012 3:26am

    toopay

    1583 posts

    Updated patch : https://gist.github.com/4023670 (i’ve added backward compability within `generate_clause` method, as @necken issue emerged).

    Soon i have enough time i will wrap those patch with appropriate unit-test files, and merge that into the main repo. Report any issue with this patch, if any.

    @jonahdahlquist,

    And if there’s no way to modify the query on the foreign table through the pivot table, how would you recommend I execute a more complex query on it

    Only ‘select’, ‘limit’ and ‘order_by’ clauses which were supported, right now. That were more than enough for me 😊

    Regarding ‘where’ clause, ‘where’ method is accept three type of parameters. First you could passed a string :

    $categories = \Model\Categories::make()->where("priority > 2 AND author = 482")->all();

    Then you could also passed an array. So theres no need to chain ‘where’ with other ‘where’, since it will overwrite current recorder with the last one. But you COULD, chaining ‘where’ with ‘or_where’. For further detail, check this test case

  • #189 / Nov 09, 2012 8:43pm

    snowfall

    7 posts

    @Toopay,

    Thanks for the patch, it works perfectly 😊 <edit> I had a brief look but could not see in the source just now, when passing the $POST data to create a record does gasorm escape the data, or perform the XSS check?  If not, is there a point in the code you would recommend to add it, so it performs on all data supplied for insert/update and where fields. </edit>

  • #190 / Nov 19, 2012 9:14am

    snowfall

    7 posts

    Hi Toopay, another bug I think.

    After applying the patches I have this error:

    ERROR: column tbl_sample_related_context.tbl_sample_sample_refid does not exist LINE 1: SELECT * FROM "tbl_sample_related_context" WHERE "tbl_sample… ^
    
    SELECT * FROM "tbl_sample_related_context" WHERE "tbl_sample_related_context"."tbl_sample_sample_refid" IN (2)
    
    Filename: /var/ci/application/third_party/gas/classes/core.php
    
    Line Number: 839

    In the Where clause after the period it wrongly puts the table name connected to the primary key, it uses ‘tbl_sample_sample_refid’ when it should only be ‘sample_refid’.

    The relationships appear set up properly as are the primary/foreign key values in both models.


    Calling Line:

    $data['sample'] = Model\Tbl_sample::where("site_code = '".$site_code."' AND sample_number = ".$sample_number)->with('related')->all();

    Also same error with ->get(1) instead of ->all().

    Tbl_sample exert:

    class Tbl_sample extends ORM {
     
    
            public $primary_key = 'sample_refid';
    
     function _init()
     {
    
                    self::$relationships = array (
                            'monolith'          =>     ORM::has_one('\\Model\\Tbl_sample_monolith'),
                            'processed'          =>     ORM::has_one('\\Model\\Tbl_processing'),
                            'series'          =>     ORM::has_one('\\Model\\Tbl_sample_series'),
                            'children'          =>     ORM::has_many('\\Model\\Tbl_parent_sample'),
                            'location'          =>     ORM::has_many('\\Model\\Tbl_sample\\Tbl_sample_location'),
                            'purpose'          =>     ORM::has_many('\\Model\\Tbl_sample_secondary_purpose'),
                            'related'          =>     ORM::has_many('\\Model\\Tbl_sample_related_context'),
                    );

    Tbl_sample_related_context exert:

    class Tbl_sample_related_context extends ORM {
     
            public $primary_key = 'sample_context_refid';
            public $foreign_key = array('\\Model\\Tbl_sample' => 'sample_refid');
    
     function _init()
     {
    
    
                    self::$relationships = array(
                            'sample' => ORM::belongs_to('\\Model\\Tbl_sample'),
                    );
  • #191 / Nov 19, 2012 10:33am

    Kljofa

    4 posts

    Hi,

    started using Gas ORM today and am very pleased with it, but am having trouble with something. I don’t know how to do multiple join statements. Searched everywhere but was not able to find solution.

    Thanks

  • #192 / Nov 19, 2012 11:12am

    snowfall

    7 posts

    @Kljofa

    Hello,

    What do you mean when you say multiple joins?  Do you mean one parent entity with many child entities, or a parent entity with a child entity, which in turn is the parent of another and so on.  For the first type of relationship the documentation gives good examples. First you need to set up the relationships so that gasorm knows how tables relate to each other:

    http://gasorm-doc.taufanaditya.com/relationship.html

    Once you have done that, you can use the with() function.  Here is an example from:

    http://gasorm-doc.taufanaditya.com/quickstart.html

    There is an entity called user, and the relationships between the (has many)kid, (has one)wife and (has many)job entities have been created so that the command below creates the joins and retrieves the corresponding records for all users.

    $users = Model\User::with('wife', 'kid', 'job')->all();

    If there is a chain of parent/child entities, the above command could be used to get the initial parent/child relationship, then the command below can be used to fetch only the child entity details of further entities.

    Model\Parent_entity_name::find(1)->child_entity_name();

    Depending on the source of your download you may have to watch your capitalization as the link below discusses, as I vaguelly remember I had the problem with capitalization too first time round:

    http://ellislab.com/forums/viewthread/213348/P120/#1010854

     

  • #193 / Nov 19, 2012 11:35am

    toopay

    1583 posts

    @snowfall,

    Its looks like the ORM class seeing your primary key as invalid collumn name, in the constructor.

    Just to verify, ensure that ‘sample_context_refid’ is exists on below script output :

    var_dump(Model\Tbl_sample_related_context::make()->meta->get('collumns'));die;
  • #194 / Nov 19, 2012 11:47am

    snowfall

    7 posts

    @toopay

    Result is:

    array(3) { [0]=> string(20) "sample_context_refid" [1]=> string(12) "sample_refid" [2]=> string(14) "context_number" }

    The code nearly works, it is using the correct foreign key except it has the table name attached.  This is the malformed SQL:

    SELECT * FROM "tbl_sample_related_context" WHERE "tbl_sample_related_context"."tbl_sample_sample_refid" IN (2)

    This is what it *should* be when correct:

    SELECT * FROM "tbl_sample_related_context" WHERE "tbl_sample_related_context"."sample_refid" IN (2)
  • #195 / Nov 19, 2012 11:56am

    toopay

    1583 posts

    @snowfall.

    I can’t replicate this behaviour, since custom foreign key actually already well-covered within recent unit-test suite.

    I suspect, the ORM class going down to this line while investigating your entity meta-data.

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

ExpressionEngine News!

#eecms, #events, #releases