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.

DMZ 1.7.1 (DataMapper OverZealous Edition)

March 14, 2010 11:43pm

Subscribe [104]
  • #916 / Apr 24, 2013 12:31pm

    WanWizard

    4475 posts

    What do you mean by “saving cross database relations”?

    Depending on your configuration, Datamapper will use a shared connection for all models, or a unique connection for every model.

    So it’s no problem so save a parent and it’s children in one go, where parent and child are in different databases. This is done with several INSERT or UPDATE queries, per model, so they are always limited to a single database.

  • #917 / Jun 11, 2013 8:08am

    wietse

    3 posts

    As posted here:

    I use this to count all active users in a database, works like it should:

    $ouser = new User;
    $data['users_active'] = $ouser->where(array('active'=>1))->count();

    Now I also want to use the (same) object to count all inactive users, therefore I want to use this:

    $ouser = new User;
    $data['users_active'] = $ouser->where(array('active'=>1))->count();
    $data['users_inactive'] = $ouser->where(array('active'=>0))->count();

    But this doesn’t seem to work. Clearing the object first doesn’t work either:

    $ouser = new User;
    $data['users_active'] = $ouser->where(array('active'=>1))->count();
    $ouser->clear();
    $data['users_inactive'] = $ouser->where(array('active'=>0))->count();

    How can I resuse the same object, in this case for counting?

     

  • #918 / Jun 11, 2013 10:36am

    WanWizard

    4475 posts

    What is the definition of “not work”? Error messages? What is the result you do get? What does check_last_query() return?

  • #919 / Jun 11, 2013 11:04am

    wietse

    3 posts

    Thanks for your reply.

    I am trying to do this (little different, but same idea):

    $ouser = new User;
    $data['users_active'] = $ouser->get_where(array('active'=>1));
    $data['users_inactive'] = $ouser->get_where(array('active'=>0));
    $this->load->view('users', $data);

    I have 2 active users, and 1 inactive.

    Now when I loop them in my view, either the active or inactive give the same result (in this case only the inactive users, because this was the last line in the above code):

    foreach($users_active AS $user) {
        echo $user->name; // shows 1 user, the inactive one
    }
    foreach($users_inactive AS $user) {
        echo $user->name; // also shows 1 user, the same inactive one
    }

    As stated in the previous post, a $ouser->clear(); doesn’t solve this…
    Any ideas?

  • #920 / Jun 11, 2013 12:02pm

    WanWizard

    4475 posts

    You have to remember that you work with objects, and objects are (in recent PHP versions) passed around by reference.

    Datamapper methods return the current object (so you can chain methods off it), unless it has a specific return value, like count(), which returns a number (which is why I didn’t understand your question).

    So after this code, $data[‘users_active’], $data[‘users_inactive’] and $ouser all contain a reference to the same object. You can see that when you dump the variables, the object # will be the same for all three.

    This means you can’t just re-use a model object like this. Either create a new one, or clone the existing one.

  • #921 / Jun 11, 2013 4:00pm

    wietse

    3 posts

    Datamapper methods return the current object (so you can chain methods off it), unless it has a specific return value, like count(), which returns a number (which is why I didn’t understand your question).

    Well, that was probably my fault 😊

    But thanks again for your reply… it’s working now!

  • #922 / Mar 05, 2014 1:42pm

    elmystica

    5 posts

    * Bump

    I have three tables:
    - products with an id (PK), name, etc ...
    - brands with an id (PK), brand_label, etc ..
    - brands_products with an id (PK), product_id, brand_id

    My product -method:

    <?php
    
    class Product extends DataMapper {
    
     var $table  = 'products';
    
     var $has_one = array(
          'brand' => array(),
          'category' => array(), 
          'gender'  => array(), 
          'material' => array(), 
          'shape' => array(), 
          'structure' => array() 
          );
          
     var $has_many = array(
          
          'color' => array(),
          'list_item' => array()
          );
     
     function __construct($id = NULL)
     {
      parent::__construct($id);
     }
    }

    My Brand Method:

    <?php
    
    class Brand extends DataMapper {
    
     var $has_many = array('product');
     
     function __construct($id = NULL)
     {
      parent::__construct($id);
     }
    }


    In my controller, I want to select ALL products where “brand.brand_url” is a certain brand passed along in the URL.

    $product = new Product();
       $product->where_related_brand('brand_url', $this->uri->segment(3))->include_related('brand', array('id', 'brand_label', 'brand_url', 'brand_lc'), FALSE, TRUE)->get_iterated();
       
    
       echo $this->db->last_query() . "
    ";
       
    
       echo "=====================
    ";
       foreach($product as $item)
       {
        echo "
    " . $item->brand->brand_label . ": brilnr. " .  $item->name;
        
        
       }
       echo "=====================
    ";

    It gives me a query like this:

    SELECT products.* FROM products LEFT OUTER JOIN brands_products brands_products ON products.id = brands_products.product_id LEFT OUTER JOIN brands brands ON brands.id = brands_products.brand_id WHERE brands.brand_url = 'gucci'

    and it returns only one result ...

    As this is something I will do often, what is the right way to get the correct joins within Datamapper?

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

ExpressionEngine News!

#eecms, #events, #releases