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]
  • #211 / Mar 26, 2013 8:18am

    toopay

    1583 posts

    But…

    // If each job could have many users
    // and each user could have several jobs
    // how you could call it by 
    echo $user->job()->title; // ?

    When you fetching jobs entity, you should get an array of object too, no?

    If you adding same relationship setting (that refer to user_job) at user entity, then you’ll get a paired info when fetching both related entities.

    foreach($users as $user){
        echo $user->name;
        $jobs = $user->job() and $jobs_info = $user->job_user();
         
        for ($i = 0, $max = count($jobs); $i < $max; $i++) {
            $job = $jobs[$i] and $job_info = $jobs_info[$i];
    
            echo 'Started as '.$job->title.' at '.$job_info->entry_date;
        }
    }

    If you use above code alot, then to avoid same iteration block over and over-again in your application, wrapping those functionality on your own extension is one option. Refer to Extension Section furthermore.

  • #212 / Mar 26, 2013 10:45am

    jejanim

    2 posts

    Thanks toopay,

    you finally enlightened me. Yes, while trying to create a good example I mixed up things about the many_to_many
    relation. But never the less, it works quite well now and I didn’t even have to use the for loop.
    Just changed the foreach:

    foreach ($users->jobs() as $key => $value){
        $pivot = $value->user_job();
        $info   = $pivot[$key];
    }

    Thanks a lot!

  • #213 / May 24, 2013 7:50am

    fdidron

    2 posts

    Hi Toopay,

    First, thanks for the neat piece of software, It s from far the easiest and most elegant ORM I’ve used.

    I was wondering what was the best way to update a Model given a simple associative array as input while filtering out the array’s key that don t match field of the model ?

    Kind regards,

    Florian

  • #214 / May 24, 2013 10:39am

    toopay

    1583 posts

    You could have :

    function _before_save()
    {
     $allowed_fields = array_keys($this->meta->get('fields'));
     $allowed_entries = array_intersect_key($this->record->get('data'), array_flip($allowed_fields));
    
     // Update data with filtered entries
     $this->record->set('data', $allowed_entries);
    
     return $this;
    }

    Here you could see the corresponding assertion, as an example attempts to insert data with some invalid fields, which would be filtered by above “before save” callback.

  • #215 / May 26, 2013 7:50pm

    fdidron

    2 posts

    Hi Toopay,

    Thanks exactly what I was looking for !

  • #216 / Jul 29, 2013 3:26am

    necken

    6 posts

    Is it possible to wrap custom query result in an GAS object?

  • #217 / Aug 05, 2013 1:48pm

    albertleao

    30 posts

    Hello!

    Gas Orm looks great and I’m super excited to start using it. Chose it over Doctrine and Datamapper.

    I am having one issue though.

    I have a user_accounts table and a user_credits table with a one-to-one relationship. I believe I set the models up correctly but I’m unable to pull the relationships together in the controller. Can anyone give me a hand?

    user.php:

    <?php namespace Model;
    
    use \Gas\Core;
    use \Gas\ORM;
    
    class User extends ORM {
    
            public $table = 'user_accounts';
            public $primary_key = 'userid';
    
       function _init()
       {
            self::$relationships = array (
                    'credit'        =>      ORM::has_one('\\Model\\Credit'),
            );
    
            self::$fields = array(
                    'userid'              =>              ORM::field('auto[10]'),
                    'firstname'             =>              ORM::field('char[50]'),
                    'lastname'      =>      ORM::field('char[50]'),
                    'email'         =>      ORM::field('char[100]'),
                    'password'      =>      ORM::field('char[150]'),
                    'gender'        =>      ORM::field('char[11]'),
                    'month'         =>      ORM::field('char[16]'),
                    'day'           =>      ORM::field('char[10]'),
                    'year'          =>      ORM::field('int[255]'),
                    'regdate'       =>      ORM::field('char[16]'),
                    'regtime'       =>      ORM::field('char[16]'),
                    'phonenumber'   =>      ORM::field('char[15]'),
                    'zipcode'       =>      ORM::field('int[11]'),
                    'type'          =>      ORM::field('char[16]'),
                    'verified'      =>      ORM::field('int[1]'),
                    'allow_email'   =>      ORM::field('int[1]'),
                    'is_advertiser' =>      ORM::field('int[4]')
            );
        }
    }

    credit.php:

    <?php namespace Model;
    
    use \Gas\Core;
    use \Gas\ORM;
    
    class Credit extends ORM {
    
        public $table = 'user_credits';
        public $primary_key = 'userid';
    
        function _init()
        {
            self::$relationships = array (
                'user' => ORM::belongs_to('\\Model\\User'),
            );
            self::$fields = array(
                'userid'          =>          ORM::field('auto[10]'),
                'credits'           =>          ORM::field('int[10]'),
                'campaigns_viewed'  =>          ORM::field('int[int]')
            );
        }
    }

    In my controller, I’m trying to look at how many credits a user has using the following function:

    public function orm_test() {
                    $this->output->enable_profiler("true");
                    error_reporting(E_ALL);
                    ini_set('display_errors', '1');
                    $a = Model\User::find(1)->credit();
                    echo "<pre>";
                    print_r($a);
                    echo "</pre><p>“;<br />
            }
    </pre>

    Running the above code returns the following:

    “Fatal error: Call to a member function result_array() on a non-object in /var/www/dev/application/third_party/gas/classes/core.php on line 1591”

  • #218 / Dec 03, 2013 2:10am

    kamaroly

    2 posts

    I am getting below error while trying to use another host which is linux, but on my local machine(Windows 7) everything works fine.

    Fatal error: Class ‘Model\Currency_model’ not found in /home/patient/public_html/hellorwanda.com/KPos2/application/controllers/config.php on line 207

    Model is

    <?php 
    Namespace Model;
    
    if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    /*
     * Currency MOdle
    *
    * @author   Kamaro  Lambert
    * @access public
    
    */
    Use \Gas\Core;
    Use \Gas\ORM;
    
    Class Currency_model extends ORM
    {
     public $table       ="currencies";
     public $primary_key ="curr_id";
     
     function _init()
     {
         self::$fields = array(
            'curr_id'           =>  ORM::field('auto[30]'),
            'Name'              =>  ORM::field('string',array('required')),
            'Exchange_Rate'     =>  ORM::field('numeric', array('required')),
            'Symbol'            =>  ORM::field('char[6]'),
            'Symbol_Suffix'     =>  ORM::field('char[5]'),
            'Thousand_Separator'=>  ORM::field('char[1]'),
            'Decimal_Separator' =>  ORM::field('char[1]'),
            'Status'            =>  ORM::field('numeric'),
            'Default'           =>  ORM::field('numeric'),
           ); 
         $this->ts_fields = array('Date');
         
     }
    }

    and Controller is

    ###++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++###
     // CURRENCY
     ###++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++###
     
     /**
      * @author Kamaro Lambert
      * @name   currency()
      * Displays a manageable list of all currencies. Each row can be updated or deleted.
      */
     function currency($offset=0)
     {
      $currencies= Model\Currency_model::all();
      
      $config['base_url'] = site_url('/config/currency');
      $config['total_rows'] =  count($currencies);
      $config['per_page'] = '10';
      $config['uri_segment'] = 3;
      $this->pagination->initialize($config);
      
      $data['currencies']= Model\Currency_model::limit($config['per_page'],$offset)->all();
      
      
      //Setting the tamplate for the table
      $template = array( 'table_open' => '<table border="1" class="table table-bordered table-striped">');
      
      $data['page_title']='Currencies';
      
      
      
      $data['body']=$this->load->view('config/global_config/currency_table',$data,true);
     
      $this->load->view('config/page',$data);
      
      
     }

    Path to the model is
    application/models/Currency_model.php
    anyone who can help me please?

  • #219 / Jan 13, 2014 3:39am

    omegasteffy

    2 posts

    @DerLola & Khrome83,

    An Error Was Encountered
    Gas ORM cannot create a model(s) file at: ../application/models\fields\of\study.php

    I’m guessing it has something to do with the ‘fields’ part of my table name which is fields_of_study. Is there a way to fix this? And does it have to go into a subdirectory? I don’t really need that.

    Well, it assume all of your entities is in a single word. user to hold user entity, book for books entity and so on. And it assume all table with _ (underscore) as a pivot table, which associate entity, eg : job_user to associate user with job and so on. I encourage you to use this convention, for your convenience.

    If for somehow reason(s), you cant follow those convention, you always still could create your model based by your own convention, manually 😊

    I got the same error. I seems to be a great feature, and i would just point out that it is desired by more users.
    Could it at least be stated a bit more clear in the documentation

  • #220 / Jan 13, 2014 6:36am

    omegasteffy

    2 posts

    Hello again.
    I tried using the relation of the Gas ORM2 get link related table ORMs

    I have tried link as described in the documentation.


    Error Number: 1054
    Unknown column ‘vdrs.vessels_id’ in ‘where clause’
    SELECT * FROM `vdrs` WHERE `vdrs`.`vessels_id` IN (4)
    Filename: /srv/framework_tests/code_igniter/third_party/gas/classes/core.php
    Line Number: 850

    The query i need is something like
    “SELECT * FROM `vdrs` WHERE `vdrs`.`id` ==”.$vessel_model->fk_vdr_current

    class Vessels extends ORM {
        public $primary_key = 'id';
        public $table = 'vessels';
        public $foreign_key = array('\\model\\Vdrs' => 'fk_vdr_current');
        
     public function _init() {
         
      self::$relationships = array(
          
       'vdr_current' => ORM::has_one('\\model\\Vdrs')
                    );
         
        self::$fields  = array(
           'id' => ORM::field('auto'),
           'mmsi_no' =>ORM::field('numeric'),
           'name'=> ORM::field('char[100]'),
           'imo' =>ORM::field('numeric'),
           'fk_active_type' =>ORM::field('numeric'),
           'fk_vdr_current' =>ORM::field('numeric'),
          );
     
        }
    }
    ?>

    So far the solution is quite simple

    public function vdr_current(){
          return Vdrs::find($this->fk_vdr_current);
    }

    However the purpose of the Gas ORM is defeated.

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

ExpressionEngine News!

#eecms, #events, #releases