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]
  • #181 / Feb 23, 2012 9:38am

    WanWizard

    4475 posts

    Please define “outside a controller”?

  • #182 / Feb 24, 2012 10:02am

    Maglok

    402 posts

    I have been trying to do a few things, and my lack to see what the state of my classes are is limiting my ability to fix it.

    To quickly return to the all_to_array(). It does show IDs, but only of “has_one” and not “has_many”.
    Example: A character has 1 gender. I get the gender_id.
    Example: A character has 10 friends. I don’t see anything.

    I am just trying to understand some of the datamapper code itself and that has led me to try to find out why the constructor of a model class is called everytime I reference the instance.

    $ch = new Character(); // Calls the constructor
    $ch->get_by_id(1); // Calls the constructor
    $ch->friend->include_join_fields()->get(); // Calls the constructor

    Shouldn’t I be working with the same instance of the Character class all the time?

    Lastly I am also trying to populate a array based on some data from the “has_many” relationships. I have a function like this in the class Character (which extends Datamapper and is a model):

    private function _populate_stuff() {
      // get stuff
      foreach($this->friend as $friend) {
       $data['name'] = $friend->name;
       $data['age'] = $friend->age;
       $this->bonusses[] = $data;
       }
      }
      return true;
     }

    The Character class then has a class variable bonnusses and the constructor calls the _populate_stuff function. Yet The $this->friend seems to be empty, since I don’t get into the foreach. Which leads me back to wanting to know what I am referencing and the state of my variables (without the huge list of stuff if I just var_dump. 😊

  • #183 / Feb 24, 2012 11:00am

    WanWizard

    4475 posts

    To start with the all_to_array() question: if you check the array extension, you’ll see that the to_array() method checks both the has_one and has_many arrays to see if the property name is an related object. Perhaps you can debug that and see why it doesn’t work?

    A constructor should only be called when constructing the object, so you are right in that it should only be called once. However, internally an object is constructed for each record in the result, so it’s logical that you will see a call to the constructor in get() operations, as new objects of your model are instantiated.

    As for your last question, assuming that ‘friend’ is a related model, it will only exist if related objects have been fetched, which is never the case in a model constructor (as you haven’t even fetched the record itself at that point).

  • #184 / Feb 26, 2012 1:28am

    Please define “outside a controller”?

    It seems to be an error of level E_STRICT since it doesn’t show when error_reporting is set to E_ALL. So I am just ignoring it for now.

    It is a unit test (I am using simpletest). It looks like this and is included by the simpletest library.

    class BandsTest extends UnitTestCase
    {
        private $ci;
        
        function __construct() {
            parent::__construct('Bands tests');
            
            $this->ci =& get_instance();
        }
     
        function testCountBands()
        {
             $b = new Band(); //Band is a datamapper model
             ...
  • #185 / Feb 27, 2012 9:02am

    Maglok

    402 posts

    To start with the all_to_array() question: if you check the array extension, you’ll see that the to_array() method checks both the has_one and has_many arrays to see if the property name is an related object. Perhaps you can debug that and see why it doesn’t work?

    I have found out why it doesn’t work (for me).

    In to_array() (which is called by all_to_array()) the $object->fields are taken. Those do not include has_one or has_many. The array extension then does this:

    if(array_key_exists($f, $object->has_one) || array_key_exists($f, $object->has_many))

    Where $f is the current field. The thing is the $fields do not include has_many or has_one. It does take has_one, because $fields is the database fields and a has_one has a database field, whereas a has_many has a database join table.

    in_array() does have the option to give the fields as a parameter instead of defaulting to the fields of the Object you are calling in_array on. Thus I ‘fixed’ this by giving the names of the has_many objects as the parameter. Another way would be to overload in_array and get it to check fields and has_many arrays.

    The way the function works is probably intended, but I didn’t get it at first. 😊 Thanks!

    A constructor should only be called when constructing the object, so you are right in that it should only be called once. However, internally an object is constructed for each record in the result, so it’s logical that you will see a call to the constructor in get() operations, as new objects of your model are instantiated.

    Ah! So that’s how… right, moving on, nothing to see here. I blame friday-tired brain.

    As for your last question, assuming that ‘friend’ is a related model, it will only exist if related objects have been fetched, which is never the case in a model constructor (as you haven’t even fetched the record itself at that point).

    This makes sense and I was able to find this out myself now that I got in_array() working for myself. It makes understanding the Datamapper much easier. I will just have to call a method to populate the friends after the Character is instanced.

    EDIT: Here is a quick and dirty addition to to_array, after row 45.

    foreach($object->has_many as $key => $value)
      {
       $fields[$key] = $key;
    
      }
  • #186 / Feb 27, 2012 1:24pm

    Spir

    139 posts

    Better then the extra parameter.

    In the end I did this:

    strpos($orderby, '.') === FALSE AND $orderby = $this->add_table_name($orderby);

    it seems it load some bug.
    Here is what I’ve made :

    $item_a = new Item_A;
    $item_a->include_related('item_b','attribut_b1')
           ->where_related_item_b('attribut_b2',$value_attribut_b2)
           ->order_by('item_b.attribut_b1', 'asc')
           ->get();

    That makes a DM Error :

    Error Number: 1054

    Unknown column ‘item_b.attribut_b1’ in ‘order clause’

    SELECT * FROM (`item_a`) ORDER BY `item_b`.`attribut_b1` asc

    Filename: .../application/libraries/datamapper.php

    Line Number: 1298

    Why is that? the method include_related should make a join between the table. There something missing here 😛

  • #187 / Feb 27, 2012 5:22pm

    WanWizard

    4475 posts

    Don’t think those are related, include_related() should add the related table using a call to _add_related_table(), which is unconditional.

    Are you sure the relation is defined properly? If not, no related table will be found and no join will be added…

  • #188 / Feb 28, 2012 4:07am

    Spir

    139 posts

    Don’t think those are related, include_related() should add the related table using a call to _add_related_table(), which is unconditional.

    Are you sure the relation is defined properly? If not, no related table will be found and no join will be added…

    My config/relation are OK.
    It is correctly working whithout the hack we previously talk about :
    http://ellislab.com/forums/viewreply/973083/
    I’ll digg more this issue…

  • #189 / Feb 28, 2012 1:23pm

    Lunaman

    3 posts

    Hey,

    Is it possible to set validation rules at runtime?

    I have some fields that are not always required, it depends on wich form is submitted.

    Greetings

  • #190 / Feb 28, 2012 1:37pm

    Spir

    139 posts

    Hey,

    Is it possible to set validation rules at runtime?

    I have some fields that are not always required, it depends on wich form is submitted.

    Greetings

    You can propably do what you need using a custom function.

    public $validation = array(
     'your_field' => Array(
      'rules' => array('trim', '_check_value')
     )
    );
    
    function _check_value($field)
    {
     // check if field is required
     if (<field is require>)
     {
      return (trim($this->{$field}) == '') ? FALSE : TRUE;
     }
     return TRUE;
    }
  • #191 / Feb 28, 2012 1:48pm

    Lunaman

    3 posts

    I guess doing the following is acceptable too?

    $object->validation['field']['rules'] = array('trim', 'required', ...);
  • #192 / Feb 28, 2012 1:51pm

    Spir

    139 posts

    I guess doing the following is acceptable too?

    $object->validation['field']['rules'] = array('trim', 'required', ...);

    yes much better.

  • #193 / Feb 28, 2012 7:16pm

    toadies

    8 posts

    I have a question about delete().  I’m trying to figure out why datamapper marks my relationship child id null (or 0) when I delete the parent.

    I tried to search and I think this is a common problem.

    Parent is Customers table.
    Child is Equipments table.

    So my relationship should be…

    Class Customer
    var $has_one = array(‘equipment’);

    Class Equipment
    var $has_one = array(‘customer’);

    When I delete the @ the customer level it updates the equipment table customer_id to 0.

    I want datamapper to delete automatically, if not I can delete it manually, but according the the manual it should delete all relationships?

  • #194 / Feb 29, 2012 6:05am

    WanWizard

    4475 posts

    It does delete the relationship, it does not delete the related record. These are two different things.

    I have to dive into the code to see what the exact rules are for cascading deletes, I’ll try to find the time for that later today.

  • #195 / Feb 29, 2012 7:08am

    toadies

    8 posts

    It does delete the relationship, it does not delete the related record. These are two different things.

    I have to dive into the code to see what the exact rules are for cascading deletes, I’ll try to find the time for that later today.

    How do I make equipments the child (relationship) of customers?

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

ExpressionEngine News!

#eecms, #events, #releases