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]
  • #196 / Nov 19, 2012 12:57pm

    Kljofa

    4 posts

    Here is a crude example of my problem.

    I have 3 tables

    TABLE 1
    id
    year

    TABLE 2
    id
    table1_id

    TABLE 3
    id
    table2_id

    I want to grab all results from table 3 that matches year from table 1.

    I tried doing something like this:

    $table3 = Model\Table3::join('table2', 'table2.id = table3.table2_id')->join('table1', 'table1.id = table2.table1_id')->find_by_year(2012);

    but i only get one join and that’s the last one i entered. Relationships are properly set.

  • #197 / Nov 19, 2012 2:13pm

    snowfall

    7 posts

    @toopay

    Found the problem.  In the child relation, if capital letters are used for Model and Class, it fails.  So this causes the problem:

    public $foreign_key = array('\\Model\\Tbl_sample' => 'sample_refid');

    But if lower case letters are used, it passes.  This solves the problem:

    public $foreign_key = array('\\model\\tbl_sample' => 'sample_refid');

    Raised before, http://ellislab.com/forums/viewthread/213348/P120/#1010854, but I was using the code from Github, so thought it would be fine. D’oh.  Thanks for looking, sorry to disturb ^^

  • #198 / Nov 19, 2012 9:07pm

    toopay

    1583 posts

    @Kijofa

    If everything has been setup correctly, you should only need :

    $table1 = Model\Table1::with('table3')->find_by_year(2012); 
    $table3 = $table1->table3();

    You do not need to set up a manual JOIN.

  • #199 / Nov 20, 2012 3:23am

    Kljofa

    4 posts

    Tried that but didn’t work. This is what i have setup in my models.

    TABLE 1 model:

    self::$relationships = array('table2' => ORM::has_many('\\Model\\Table2'));

    TABLE 2 model:

    self::$relationships = array('table1' => ORM::belongs_to('\\Model\\Table1'), 'table3' => ORM::has_many('\\Model\\Table3'));

    TABLE 3 model:

    self::$relationships = array('table2' => ORM::belongs_to('\\Model\\Table2'));

    With this relationship i tried calling the code you mentioned but got error that the method doesn’t exists in the object.

  • #200 / Nov 20, 2012 11:23am

    toopay

    1583 posts

    @Kijofa

    With your current setup, this should do the job :

    $table1 = Model\Table1::with('table2')->find_by_year(2012);
    
    foreach ($table1->table2() as $table2)
    {
      var_dump($table2->table3());
    }

    Actually, it is easier to set up :

    // In table1
    self::$relationships = array('table3' => ORM::has_many('\\Model\\Table2 <= \\Model\\Table3'));

    so that you could use the previous code i mention.

  • #201 / Dec 10, 2012 10:07am

    necken

    6 posts

    Hi.

    I have a pivot table set up. I want to fetch data from a table connected to the pivot table using the result extension but i want to check if the id is valid against the pivot table.

  • #202 / Dec 14, 2012 7:25am

    indCI

    11 posts

    After upgrading from GORM 2.0.0 to 2.1.1 the following code no longer works for me:

    Model\User::with('userdata')->all();

    It used to do a subquery:

    SELECT * FROM `userdata` WHERE `userdata`.`user_id` IN ( 1,2,3,4,5,6 )

    But now instead it does

    SELECT * FROM `userdata` WHERE `userdata`.`user_id` IN ( 6 )

    and every other $user->userdata() will return bool False.

    I.e. it only queries the userdata table for the last row matched in the user table.

    I can still loop through the list of users and print their IDs and check by hand that they match the ones in the database, so that should not be a problem.

    What am I doing wrong here?

  • #203 / Jan 31, 2013 5:14am

    necken

    6 posts

    I have three tables. Jobs, Categories and Location. I want to find jobs with a sertain category id and location id. Jobs has many categories and categories has many jobs. Therefore i need to use a pivot table. I checked out laravels ORM and in there you used:

    $jobs = Jobs::with(array('locations' => function($query)
    {
        $query->where('id', 'like', 1);
    
    }))->get();

    Are there any ways to do something similar with GAS?

  • #204 / Feb 13, 2013 11:30am

    toopay

    1583 posts

    Patch v.2.1.2

    Changes :
    1. Fixes several known issues on relationship entities.
    2. Minor refactor on Core class.
    3. Additional files on test-suite to cover core files.

    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.2 gas

    I added a section on README about test-suite, so make sure to read that section before tweeting me any issues regarding this open-source library. 😊

  • #205 / Feb 23, 2013 2:03pm

    Arugin

    1 posts

    Hello! How can I get a result, as I’m make a query:
    SELECT * from USER WHERE user_email = ‘Arugin’ AND user_password=“123”

    I’m trying to write something like this:

    $data = User::where(“user_email =”,$user_email)->first()->where(“user_password =”,$user_password)->first();

    but it’s ignore the first “where”.

    What I’m missed?

    Thank you.

  • #206 / Feb 25, 2013 6:20am

    radd

    3 posts

    Hi toopay,

    I have some problems using relationships into GAS.
    I basically have two tables:

    chef_secteur {id[int], nom_cs[var_char], email_cs[int]}
    1 | John Doe | 1
    2 | Ana Alba | 3

    users {id[int], identity[varchar], pass[varchar], email[varchar]}
    1 | jd | xxx | jdoe at tt.co
    2 | ue| xxx | ultra at tt.co
    3 | aa | xxx | aalba at tt.co

    So the `chef_secteur`.`email_cs ` belongs to the `users`.`email`

    These are my GAS models : http://codepad.org/kQynbVeo
    I get a server NetworkError: 500 Internal Server Error hangout when I add fields in relationships array. If I comment them, GAS 2.1.2 works fine, I’m using it as result()->find_by_field() and I’m happy with it.

    Thanks, Radu

  • #207 / Feb 27, 2013 7:00am

    jax2904

    1 posts

    Hi toopay, great library you wrote! 
    But i have a problem with relationships.

    I’ve a class User and a Class Privatemsg

    each Privatemsg belongs to user twice: from_id, to_id.
    and each User has many Privatemsg twice: msgs_sent, msgs_received.

    How can I create this relationship?

    +--------+      +--------------+
    | user   |      | privatemsg   |
    +--------+      +--------------+
    | id     |<--|  | id           |
    | name   |   |->| from_id      |
    |        |   |->| to_id        |
    |        |      | message      |    
    +--------+      +--------------+
  • #208 / Mar 12, 2013 11:32am

    dorobica

    1 posts

    Hello Toopay and thanks for this awesome ORM.

    I have one problem though. I have one_to_many relationship between order and order_promo_code and I have troubles saving correct into pivot table.

    Here is a small test to explain:

    $order = Model\Order::find(13);
    
      $test = Model\Order_Promo_Code::make(array(
       'discount' => 10,
       'uses' => 100
       ));
    
      $test->related->set('entities.order.pivot', array(
        'order_id' => $order->id
       ));

    Now doing $test->save() will save my promo code and save into pivot table. The thing is that the order_promo_code_id field isn’t save, only the order_id field.

    I can save the promo code before entering it to pivot but that just feels wrong 😛

    $order = Model\Order::find(13);
    
      $test = Model\Order_Promo_Code::make(array(
       'discount' => 10,
       'uses' => 100
       ))->save();
    
      $test = Model\Order_Promo_Code::last();
    
      $test->related->set('entities.order.pivot', array(
        'order_promo_code_id' => $test->id
        'order_id' => $order->id
       ));
    
      $test->save();

    Can anyone help me?
    Thanks!

  • #209 / Mar 24, 2013 5:37am

    toopay

    1583 posts

    @radd, could you post the error-log from your web-server?

    @jax2904, normalize your database schema first. Add a pivot table is one option.

    @dorobica, did you set the foreign key correctly? If you look at this block, the foreign key value should be passed automatically.

  • #210 / Mar 26, 2013 7:32am

    jejanim

    2 posts

    Hey folks,

    I’m running into the pivot table problem again. I already read the response about accessing additional pivot fields by creating a has_many relationship definition in one model. But the problem that raises now is that I get an array of objects if I then try to access the method.

    My tables are set up like this:

    +--------+      +--------------+        +--------------+
    | user   |      | user_job     |        | job          |
    +--------+      +--------------+        +--------------+
    | id     |<---->| user_id      |    |-->| id           |
    | name   |      | job_id       |<---|   | title        |
    |        |      | entry_date   |        | salary       |
    |        |      | some_attr    |        | company      |
    +--------+      +--------------+        +--------------+

    In table job I did something like this:

    $relati
         'user'        => ORM::has_many('\\Model\\User\\Job => \\Model\\User'),
         'user_job' => ORM::has_many('\\Model\\User\\Job'),
    );

    Let’s say I want to have the date a user got a job, I’d do the following:

    $users = Model\User::with('job')->all();
    
    foreach($users as $user){
         echo $user->name;
         echo $user->job()->title;
         echo $user->job()->user_job()->entry_date;
    }

    The problem: user_job() returns an array of objects.
    Is there any way to do this w/o having to do an additional request?

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

ExpressionEngine News!

#eecms, #events, #releases