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]
  • #106 / Apr 27, 2012 3:38am

    toopay

    1583 posts

    @khavayero,

    You shouldn’t alter the empty property that way, empty property should automatically set by the Core class based by your model state.

    For update a record, you should use finder before use save method.

    $user = Model\User::find($id); // Or Model\User::find_by_otherfield($field, FALSE)
    $user->name = "New name";
    
    $user->save();
    
    // Or if you want to run the validation
    // if ($user->save(TRUE))
    // {
    //    Success
    // }
    // else
    // {
    //    Fails, you can get the error from $user->errors
    // }
  • #107 / May 02, 2012 12:37am

    Lyken

    5 posts

    Hello,

    Thanks for a cool module. I’m just getting into ORM and HMVC and I’m using your gas orm 2 and the MX 5.4 hmvc implementation.

    I can’t seem to work out how to load the ‘many’ side of the relationship in regards to form building.

    Case:

    I have a part that has a category. Categories can have many parts. I have defined the has_many and belongs_to relationships in the models.

    Here is the part model _init().

    self::$relationships = array (
       'category' => ORM::belongs_to('\\Model\\Category')
      );
      
      self::$fields = array(
       'id' => ORM::field('auto[11]'),
       'name' => ORM::field('char[255]', array('required','max_length[255]')),
       'description' => ORM::field('string', array('required')),
       'category_id' => ORM::field('int[11]'),
       'partnumber' => ORM::field('char[255]'),
       'manufacturer_partnumber' => ORM::field('char[255]'),
       'modified_at' => ORM::field('datetime'),
       'created_at' => ORM::field('datetime'),
      );

    here is the category model _init().

    self::$relationships = array (
       'part' => ORM::has_many('\\Model\\Part')
      );
      
      self::$fields = array(
       'id' => ORM::field('auto[11]'),
       'name' => ORM::field('char[255]', array('required','max_length[255]')),
       'prefix' => ORM::field('char[255]', array('required','max_length[255]') ),
       'related_to' => ORM::field('char[255]'),
       'modified_at' => ORM::field('datetime'),
       'created_at' => ORM::field('datetime'),
      );
      
      $this->ts_fields = array('modified_at','[created_at]');

    Now when creating a part I would like to load all the categories that are available and populate a dropdown with these categories. I assumed that I would be able to just pass the following call in my part controller to my part form view and then manipulate the variable in the view:

    $data['categories'] = Model\Category\Category::all(); 
    $this->load->view('includes/template', $data);

    But when I load the form I get a code igniter error that it can’t find the categories variable. If i print_r the $data[‘categories’] variable before parsing it to the view then I get a complete variable dump with all the data in there. Am I missing something?

    All the examples using Gas ORM 2 that I can find seem to be fairly simple and don’t really show any real world examples of anything.

    Thanks in advance.

  • #108 / May 02, 2012 12:54am

    Lyken

    5 posts

    Hello,

    Thanks for a cool module. I’m just getting into ORM and HMVC and I’m using your gas orm 2 and the MX 5.4 hmvc implementation.

    I can’t seem to work out how to load the ‘many’ side of the relationship in regards to form building.

    Case:

    I have a part that has a category. Categories can have many parts. I have defined the has_many and belongs_to relationships in the models.

    Here is the part model _init().

    self::$relationships = array (
       'category' => ORM::belongs_to('\\Model\\Category')
      );
      
      self::$fields = array(
       'id' => ORM::field('auto[11]'),
       'name' => ORM::field('char[255]', array('required','max_length[255]')),
       'description' => ORM::field('string', array('required')),
       'category_id' => ORM::field('int[11]'),
       'partnumber' => ORM::field('char[255]'),
       'manufacturer_partnumber' => ORM::field('char[255]'),
       'modified_at' => ORM::field('datetime'),
       'created_at' => ORM::field('datetime'),
      );

    here is the category model _init().

    self::$relationships = array (
       'part' => ORM::has_many('\\Model\\Part')
      );
      
      self::$fields = array(
       'id' => ORM::field('auto[11]'),
       'name' => ORM::field('char[255]', array('required','max_length[255]')),
       'prefix' => ORM::field('char[255]', array('required','max_length[255]') ),
       'related_to' => ORM::field('char[255]'),
       'modified_at' => ORM::field('datetime'),
       'created_at' => ORM::field('datetime'),
      );
      
      $this->ts_fields = array('modified_at','[created_at]');

    Now when creating a part I would like to load all the categories that are available and populate a dropdown with these categories. I assumed that I would be able to just pass the following call in my part controller to my part form view and then manipulate the variable in the view:

    $data['categories'] = Model\Category\Category::all(); 
    $this->load->view('includes/template', $data);

    But when I load the form I get a code igniter error that it can’t find the categories variable. If i print_r the $data[‘categories’] variable before parsing it to the view then I get a complete variable dump with all the data in there. Am I missing something?

    All the examples using Gas ORM 2 that I can find seem to be fairly simple and don’t really show any real world examples of anything.

    Thanks in advance.

    Well now I feel slightly stupid 😊 Moving the categories definition to the top of the function with nothing else above it has it working fine, why I have no idea, but now its working.

    $data['categories'] = Model\Category\Category::all();


    For anyone that wants to know how to grab all the categories/countries/what have you for use in populating a drop down for use in a form you’d do the following:

    Parse all categories to the view from your controller:

    function create() {
           $data['categories'] = Model\Category\Category::all(); 
                  // Other stuff to load into the form be it existing data or not. 
                 // Load the view
                 $this->load->view('your_view', $data);
            }

    Populate your dropdown in the view:

    <?php 
    $options = array();     
      
    foreach ($categories as $cat) :   
         $options[$cat->id] = $cat->name;
    endforeach; 
       
    echo form_dropdown('category', $options );
    ?>

    Your drop down should now be populated with the information from the related table.

  • #109 / May 02, 2012 12:41pm

    toopay

    1583 posts

    Moving the categories definition to the top of the function with nothing else above it has it working fine, why I have no idea, but now its working.

    Since i did not see the full code in corresponding function, i can’t comment on that. Maybe you overide the array? Or you put that in a control flow (if/else statement, or else) which didnt fired up?

    The example, given by mecharius in example section indeed fairly general. But it did, give anyone a throughout (and practical) example to get started with this package. I believe he and someone else still improve those example in their spare time.

    And when you works with form, you may want to look at extension section (html extension). You also could easily extend all extensions, to fit with your needs.

  • #110 / May 02, 2012 9:44pm

    Lyken

    5 posts

    The example, given by mecharius in example section indeed fairly general. But it did, give anyone a throughout (and practical) example to get started with this package. I believe he and someone else still improve those example in their spare time.

    Thanks for your response. I do believe I was doing something do the $data array.

    I’m hoping to write up an more detailed example that uses Gas ORM and HMVC as a tutorial so people can get a better understanding as to how to use the more complex parts of the system with examples, once I understand the system better myself.

    thanks!

  • #111 / May 03, 2012 9:57pm

    khavayero

    14 posts

    Hello Toopay, How are you?

    I need validate two or more data with GAS ORM. I need do a “reset” for Form_validation, look here…

    http://fczaja.blogspot.com.es/2011/07/codeigniter-resetting-form-validation.html

    How I can do it in GAS ORM?

    I created temporarily a class:

    class MY_Form_validation extends CI_Form_validation {
        public function __construct()
        {
            parent::__construct();
        }
        
        public function _reset_validation()
        {
            // Store current rules
            $this->_error_array = array();
            $this->_field_data = array();
        }
    }

    It’s work, but….

    Thanks!!

     

  • #112 / May 03, 2012 10:00pm

    khavayero

    14 posts

    Sorry…

    In core.php I have added:

    // Perform CI validation
      if ($CI->form_validation->run() == FALSE)
      {
       // Set an error boundary
       $boundary = '<ERROR>';
                        
       // Get each error 
       foreach ($entries as $field => $entry)
       {
        if (($error = $CI->form_validation->error($field, $boundary, $boundary)) and $error != '')
        {
         // Parse the error and put it into appropriate field
         $error               = str_replace($boundary, '', $error);
         $gas->errors[$field] = $error;
        }
       }
                
       $valid = FALSE;
    
       Reset Form_validation--------------> $CI->form_validation->_reset_validation();
      }
  • #113 / May 04, 2012 2:52am

    toopay

    1583 posts

    @khavayero, i’m not sure what you are trying to do there. But from the changes you did above, i suggest you try doing stuff like that in hook point (_after_check perhaps) in your model.

  • #114 / May 09, 2012 8:42am

    Eternal

    2 posts

    Hello

    I added the migration library 1.0 to my application and I enabled it;
    I created my database tables, and set the database properties in the CI config file.

    When I try to load the gas library, the following error message appears:
    “Unable to load the requested class: migration”

    Should I create a migration class? If so, with which content?
    Otherwise, what did I forget?

  • #115 / May 09, 2012 11:44am

    toopay

    1583 posts

    @Eternal, you dont have to add any library. Migration class was shipped alongside with the framework for CodeIgniter v.2.1.0. Prior to that version, Migration class is not available, and therefore those auto-generate functionalities will not be supported. You will need to upgrade, if this is the case.

  • #116 / May 09, 2012 11:53am

    Eternal

    2 posts

    I’m in CI 2.0.3 indeed, I didn’t check before starting my new dev which version it was.
    Thanks for your help!

  • #117 / May 10, 2012 10:45pm

    Lyken

    5 posts

    Hello Again,

    I’ve managed to get some information into my pivot tables and I can access the objects defined in the pivot relationship however i have extra information stored in these pivot tables and I’m not entirely sure how to retrieve the information from my pivot table.

    Here are the table definitions:

    CREATE TABLE `site` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(255) NOT NULL,
      `modified_at` datetime NOT NULL,
      `created_at` datetime NOT NULL,
      PRIMARY KEY (`id`)
    )
    
    CREATE TABLE `part` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(255) NOT NULL,
      `description` text NOT NULL,
      `modified_at` datetime NOT NULL,
      `created_at` datetime NOT NULL,
      PRIMARY KEY (`id`)
    )
    
    CREATE TABLE `site_part` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `site_id` int(11) NOT NULL,
      `part_id` int(11) NOT NULL,
      `min_order` int(11) NOT NULL,
      `max_order` int(11) NOT NULL,
      `qty` int(11) NOT NULL,
      PRIMARY KEY (`id`)
    )

    I’ve defined a many to many relationship and have that working as I can insert information into that pivot table using the related method as defined in the docs.

    I’m able to find all the parts connected to a site using the standard.

    $site->part();

    But how do I access the $min_order, $max_order & $qty from that pivot table?

     

  • #118 / May 11, 2012 3:16am

    lexilya

    2 posts

    Loving Gas ORM 2 so far, but I have a question regarding relationships.

    In PHP ActiveRecord, I would declare it as following:

    class Person extends ActiveRecord\Model {
      static $primary_key = 'ID'; // Just added for verbosity
      static $has_one = array(
        array(
          'salary',
          'class_name' => 'Salary',
          'primary_key' => 'ssn',
          'foreign_key' => 'PNr'
        )
      );
    }

    i.e. individual primary and foreign keys per relationship. Am I able to do this in Gas ORM 2 as well?

  • #119 / May 11, 2012 8:01am

    toopay

    1583 posts

    @Lyken,

    You will need to set up a one-to-many relationship within site model to refer site_part entity.

    @lexilya, look at convention section and relationship section.

  • #120 / May 14, 2012 2:23am

    Lyken

    5 posts

    @Lyken,
    You will need to set up a one-to-many relationship within site model to refer site_part entity.


    Hello,

    I have the following relationships setup, I’m using modules hence the paths.

    self::$relationships = array (
       'part' => ORM::has_many('\\Model\\Site\\Site\\Part => \\Model\\Part\\Part'),
       'supplier' => ORM::has_many('\\Model\\Supplier\\Supplier'),
       'machine' => ORM::has_many('\\Model\\Machine\\Machine')
      );

    I’m able to store data in that site_part table, I just can’t seem to figure out how to get the data out for display.

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

ExpressionEngine News!

#eecms, #events, #releases