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.

DataMapper ORM v1.8.2

November 30, 2011 3:43pm

Subscribe [100]
  • #76 / Dec 30, 2011 9:36am

    Nguyen Huy

    3 posts

    @Nguyen Huy,

    do you autoload the database in your config, manually in your controller, or do you have Datamapper handle it?

    Which version of CI are you using? I’ll see if I can find the time to reproduce it.

    Hi,
    I’m using DataMapper 1.8.2 and CI 2.1.0.

    My config in autoload.php file is $autoload[‘libraries’] = array(‘database’, ‘datamapper’);
    My bug occurs when i try to load a function that does not exist in the file controllers/manage.php.
    localhost/mysite.com/manage/function_no_exist for example.
    But if i only load a function that does not exist in the file controllers/home.php, this bug does not occur. localhost/mysite.com/home/function_no_exist for example. The progress will bring me to localhost/mysite.com/home/index page.
    Config in route.php file is $route[‘404_override’] = ‘home’;
    Please help me :((((

  • #77 / Dec 30, 2011 10:10am

    WanWizard

    4475 posts

    Ok,found the issue.

    My sparks repo on github wasn’t in sync with the Bitbucket repo where the original code is hosted.

    Just updated it, now spark v1.8.2.1 is building with the post-release fixes for 1.8.2.

  • #78 / Dec 30, 2011 10:57am

    WanWizard

    4475 posts

    @Nguyen Huy,

    I rebuild exactly what you wrote, on a clean CI 2.1.0 and DM tip. And I can not reproduce it. In all cases $this->db is an instance of DM_DB_[platform]_Driver, as it should be.

    So I guess it is something in your code that triggers this…

    edit: you don’t use multiple databases do you? In that case upgrade to 1.8.2.1, there’s a bug in 1.8.2 relating to that.

  • #79 / Dec 30, 2011 1:18pm

    weblizzer

    15 posts

    Hi, i’m starting to test out the datamapper ORM seems it’s very interesting, so i’m giving a try to understand how it works….


    anyway just a quick question…

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Users extends CI_Controller{
    
    var $o_users ;
    
     public function __contstruct(){
      parent::__construct();
      $this->o_users = new User();
     }
     public function index(){
     
      
      $this->o_users = new User();
      
      $this->o_users->get();
      
      foreach($this->o_users as $user){
       echo "First Name: ".$user->u_fname;
       echo "
    Last Name: ".$user->u_lname;
       echo "
    Email: ".$user->u_email;
       echo "
    
    ";
      }
    
      $total_row = $this->o_users->count();
      
      echo "Total Records: ".$total_row;
      echo "
    
    ";
    
     
     }
     
     public function add(){
     
      $this->o_users = new User();
      $this->o_users->u_fname ="John Filip";
      $this->o_users->u_lname ="Collins";
      $this->o_users->u_email ="[email protected]";
      $this->o_users->u_status =1;
      
      if($this->o_users->save()){
      echo "You have successfully added!";
      }
     
     }
    }


    Can’t I just initialize the User Model on the contructor only so I don’t need to reinitialize the new model on every function? sorry for noob question 😊

  • #80 / Dec 30, 2011 1:20pm

    WanWizard

    4475 posts

    Just updated it, now spark v1.8.2.1 is building with the post-release fixes for 1.8.2.

    There are some issues with regenerating the spark, please wait until tomorrow…

  • #81 / Dec 30, 2011 1:23pm

    WanWizard

    4475 posts

    Can’t I just initialize the User Model on the contructor only so I don’t need to reinitialize the new model on every function? sorry for noob question 😊

    Yes, you can. No need to do it in every method…

  • #82 / Dec 31, 2011 4:00am

    weblizzer

    15 posts

    fixed.

  • #83 / Dec 31, 2011 4:38am

    WanWizard

    4475 posts

    Your constructor isn’t executed due to a typo:

    public function __contstruct(){
    
    // should be
    
    public function __construct(){

    so $o_user is never defined.

    And no, you can’t call DataMapper statically, you use it via model objects.

  • #84 / Dec 31, 2011 4:42am

    weblizzer

    15 posts

    Also encounter some issues

    class Category extends DataMapper{
    
     var $table="categories";
     var $has_many = array("product");
     
     public function __construct(){
      parent::__construct();
     }
     public function post_model_init($from_cache = FALSE){
     }
    }
    class Product extends DataMapper{
    
     var $has_one = array('category');
     
     public function __construct(){
      parent::__construct();
     }
    }

    ON CONTROLLER

    public function view(){
     
      $oCat = new Category();
      $oCat->where('id','1')->get();
      echo $oCat->product->get_iterated(); 
      foreach($oCat->product as $p){
      
       echo "
    ".$p->prod_name;
      }
      
      
     }

    but I’m getting this error:

    A Database Error Occurred

    Error Number: 1146

    Table ‘db_ci.categories_products’ doesn’t exist

    SELECT `products`.* FROM (`products`) LEFT OUTER JOIN `categories_products` categories_products ON `products`.`id` = `categories_products`.`product_id` WHERE `categories_products`.`category_id` = 1

    Filename: F:\htdocs\ci_new\system\database\DB_driver.php

    Line Number: 330


    Btw, I running on a Clean CI 2.1.0 with DataMapper 1.8.2

  • #85 / Dec 31, 2011 4:56am

    WanWizard

    4475 posts

    You have defined a many-to-one relation between category and product. This either requires a column ‘category_id’ in the products table, or a relationship table called ‘categories_products’ containing the foreign keys to both.

    If the column is not present, DataMapper will automatically assume you use a relationship table.

    So check your table definition.

  • #86 / Dec 31, 2011 5:12am

    weblizzer

    15 posts

    got fixed it now… i change cat_id to ‘category_id’ from product table. thanks a lot!

  • #87 / Dec 31, 2011 6:16am

    WanWizard

    4475 posts

    For future reference, DataMapper supports ‘advanced relationship’ configuration, in which you can define the names of the foreign keyfields (minus the ‘_id’ suffix).

    But as it is more complex to define, I would advice against using it unless you know what you’re doing…

  • #88 / Dec 31, 2011 6:16am

    weblizzer

    15 posts

    Just another quick question is it possible to do this:

    $prod->category->category_name;

    Can I call the master table field from the prod table?

  • #89 / Dec 31, 2011 6:52am

    WanWizard

    4475 posts

    Yes, as long as category is populated, either by setting ‘auto_populate’ to true, or by using include_related_category(NULL, FALSE, TRUE) in your get() query.

    ( assuming the column name is ‘category_name’ as in your question. If it is ‘name’, you will access it as $prod->category->name )

    Alternatively you can use include_related_category() in your get() query, and use $prod->category_name.

    (assuming the column name in the category table is ‘name’)

  • #90 / Jan 02, 2012 2:44pm

    Cord

    6 posts

    Happy New Year WanWizard and all DataMapper ORM users!

    I’m having a hard time representing the following in DataMapper and would appreciate a nudge in the right direction as I’m sure that I can’t be the only one whose had a similar problem. It seems a pretty standard thing for any website with security.

    I have 3 tables: Users, Sites, Groups. A User can belong to many Sites and for each Site, the User can belong to many Groups. I would have thought that a join table with the following fields would be the answer:

      id
      user_id
      site_id
      group_id

    But I don’t really see how to set this up in DataMapper, or see how else I could represent it.

    Thanks!

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

ExpressionEngine News!

#eecms, #events, #releases