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]
  • #151 / Sep 20, 2012 12:00pm

    Omegote

    6 posts

    @Omegote, try :

    $annotations = \Gas\Core::identify_annotation(self::$fields['annotations']);
    print_r($annotations);

    Nice, thanks. At the end the code I used was:

    // Name of my model
      $model_name = "Model\\" . "Book";
    
      // Init to populate the model fields
      $model_name::_init();
    
      // Get the type of each field
      foreach ($model_name::$fields as $field_name => $field_details)
      {
       $parsed_annotations = \Gas\Core::identify_annotation($field_details['annotations']);
       echo "The field {$field_name} has type {$parsed_annotations['type']}";
      }

    That “identify_annotation” function was very helpful, and there seems to be other similar functions in Core.php. Is there any documentation for these other than the inline doc?

  • #152 / Sep 22, 2012 4:14am

    pfreitas

    2 posts

    Hello!

    I’ve this database structure:

    CREATE TABLE IF NOT EXISTS `Costumer` (
      `costumerId` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
      `personId` smallint(5) unsigned NOT NULL,
      -- other costumer related fields
      PRIMARY KEY (`costumerId`),
      KEY `fk_Costumer_Person_idx` (`personId`)
    );
    
    CREATE TABLE IF NOT EXISTS `Person` (
      `personId` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
      -- other person related fields
      PRIMARY KEY (`personId`)
    );
    
    CREATE TABLE IF NOT EXISTS `Individual` (
      `personId` smallint(5) unsigned NOT NULL,
      -- other individual related fields
      PRIMARY KEY (`personId`)
    );
    
    CREATE TABLE IF NOT EXISTS `User` (
      `userId` smallint(6) NOT NULL AUTO_INCREMENT,
      `personId` smallint(5) unsigned NOT NULL,
      -- other user related fields
      PRIMARY KEY (`userId`),
      KEY `fk_User_Person_idx` (`personId`)
    );

    A Costumer is associated with a Person in a one-to-one relationship, and the Person have a Individual and a User associated, both 1:1 too.
    I’ve created my Costumer, Person, Individual and User models and I’ve successfully managed to get a Costumer row with all other tables data using Gas ORM relationships.
    Now I’m having trouble to insert a new costumer in the database. How I can do that? I’ve read the documentation, but I’m a bit lost in my specific case… I guess that I’ll have to use transactions here, but I can’t figure out what I’ve to do… Could anyone help me with this? Thank you in advance! 😊

    Cheers.

  • #153 / Sep 22, 2012 2:03pm

    toopay

    1583 posts

  • #154 / Sep 22, 2012 5:37pm

    pfreitas

    2 posts

    I think I still didn’t understand how to use $obj->related->set() in such case…

    Let me explain:

    $person = Model\Person::make($person_info);
    $person->trans_start();
    $person->related->set('entities.costumer.child', $costumer_info);
    $person->related->set('entities.individual.child', $individual_info);
    $person->related->set('entities.user.child', $user_info);
    $person->save();
    $person->trans_complete();

    I clearly know that it’s wrong, but I don’t know where I’m missing the point… Model\Costumer, Model\Individual and Model\User will need the insert_id() of Model\Person to relate at some point… no?

    Sorry for the confusion, I’m a bit lost!

  • #155 / Sep 23, 2012 9:23am

    toopay

    1583 posts

    @pfreitas,

    Cascading insert/update is also support closure, in case your creation of entity is related with other entity creation process. Read again this part

  • #156 / Sep 24, 2012 11:28pm

    Test32

    5 posts

    Fields

    Table tbl_wall

    public $primary_key = 'tbl_user_id';
    
    [color=red]self::$fields = array(
       'id' => ORM::field('auto[11]'),
       'tbl_user_id' => ORM::field('int[11]'),
       'post' => ORM::field('string'),
       'timestamp' => ORM::field('int[11]'),
      );


    Table tbl_user

    public $primary_key = 'id';
    
    self::$fields = array(
       'id' => ORM::field('auto[11]'),
       'email' => ORM::field('char[255]'),
       'fname' => ORM::field('char[255]'),
       'lname' => ORM::field('char[255]'),
       'bday' => ORM::field('int[11]'),
       'upass' => ORM::field('string'),
       'timestamp' => ORM::field('int[11]'),
      );

    Relationship

    Table tbl_wall

    self::$relationships = array (
       'user'          =>     ORM::has_one('\\Model\\user')
    );

    Table tbl_user

    self::$relationships = array (
       'wall'          =>     ORM::belongs_to('\\Model\\wall'),
    );


    Hi toopay,

    I need help im confuse on how to make this work in table tbl_wall i have field “tbl_user_id” relation is has_one and in table tbl_user in has a field “id” relation belongs_to.

    Now how can i point table tbl_wall “tbl_user_id” to table tbl_user “id”?

    When i try doing this it result:

    Unknown column 'tbl_user.tbl_wall_tbl_user_id' in 'where clause'
    SELECT * FROM `tbl_user` WHERE `tbl_user`.`tbl_wall_user_id` IN (1)

     

  • #157 / Sep 25, 2012 3:59am

    toopay

    1583 posts

    In your Wall model, you wouldn’t need to define primary key attribute. Instead, you should have something like :

    public $foreign_key = array('\\model\\user' => 'tbl_user_id');
  • #158 / Sep 25, 2012 5:00am

    Test32

    5 posts

    Its not working.
    If i remove the attribute <span style="color:red;">public $primary_key = ‘id’;</span> in tbl_wall still it get the “id”.

    Here’s the error message:

    Unknown column 'tbl_user.tbl_wall_id' in 'where clause'
    SELECT * FROM `tbl_user` WHERE `tbl_user`.`tbl_wall_id` IN (134, 136, 140, 138)

     

  • #159 / Sep 25, 2012 6:23am

    toopay

    1583 posts

    Now, did you specify table attributes on each models? Since your table name is different than your model name, each model should have table attribute.

  • #160 / Sep 25, 2012 7:11am

    Test32

    5 posts

    Yep i specify the table attribute like this:

    public $table = 'tbl_wall';
  • #161 / Sep 25, 2012 1:04pm

    toopay

    1583 posts

    @Test32, i just realize, that you define your entity relationship reversely. Swap that out too.

  • #162 / Sep 25, 2012 9:43pm

    Test32

    5 posts

    Cool its solve my problem but only for 1 table relationship.

    Is it posible to define a “primary_key/foreign_key” per table relationship like:

    self::$relationships = array (
      'table_1' => ORM::has_one('\\Model\\table_1',array('primary_key'=>'primary_key_1')),
      'table_2' => ORM::has_many('\\Model\\table_2',array('primary_key'=>'primary_key_2'))
      'table_3' => ORM::has_many('\\Model\\table_3',array('primary_key'=>'primary_key_3','foreign_key'=>'foreign_key_3'))
    );

    Thanks toopay, gas orm raks its super lightweight.

  • #163 / Sep 25, 2012 10:16pm

    toopay

    1583 posts

    @Test32, No, version 1.x.x support that kind of syntax. In version 2.x.x, you define foreign key in the “target” entity, just like what you did in your database schema.

  • #164 / Sep 25, 2012 11:31pm

    Test32

    5 posts

    But its nice you have a functionality like that maybe put like an optional.
    Is it the gas orm version 1.x.x compatible to CI 2.1?

    Im lazy to use the AR now since i started using gas orm 😊
    OK, Thank you

  • #165 / Sep 26, 2012 6:52pm

    Omegote

    6 posts

    I have defined a subclass of ORM for my base models:

    class Ego_Base extends ORM
    {
    ...
    }

    And I use it like

    class Catalog extends Ego_Base
    {
    ...
    }

    The problem is that I cannot load the child class, when I do for instance:

    $all_elements = Model\Catalog::all();

    I get:

    PHP Fatal error:  Class 'Model\\Catalog' not found

    What can I do? The base model is in the “models” folder as well.

     

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

ExpressionEngine News!

#eecms, #events, #releases