Welcome Guest, Not a member yet? Register   Sign In
DataMapper ORM v1.8.2
#71

[eluser]WanWizard[/eluser]
You could just use the to_array() method in the extension, copy it to a new method, and replace the part where the id is stored in the array by a recursive call to to_array().

Then copy all_to_array() too, and have it use the new methodname you've chosen.
#72

[eluser]Maglok[/eluser]
I was thinking of something like that, thanks for the tip. Smile Useful.
#73

[eluser]PatrickGalbraith[/eluser]
Ok I am having another strange issue with this version of DataMapper any ideas?

Code:
Unexpected PHP error [Accessing static property Band::$config as non static] severity [2048] in [/application/libraries/datamapper.php line 6563]

The code still works fine but that error is being generated.

Edit: It only happens when using a DataMapper model outside of a controller. If I use it from a controller it works fine.
#74

[eluser]WanWizard[/eluser]
Please define "outside a controller"?
#75

[eluser]Maglok[/eluser]
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.
Code:
$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):

Code:
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. Smile
#76

[eluser]WanWizard[/eluser]
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).
#77

[eluser]PatrickGalbraith[/eluser]
[quote author="WanWizard" date="1330007907"]Please define "outside a controller"?[/quote]

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.

Code:
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
         ...
#78

[eluser]Maglok[/eluser]
[quote author="WanWizard" date="1330099200"]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?[/quote]
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:

Code:
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. Smile Thanks!

[quote author="WanWizard" date="1330099200"]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.[/quote]
Ah! So that's how... right, moving on, nothing to see here. I blame friday-tired brain.

[quote author="WanWizard" date="1330099200"]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).[/quote]
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.

Code:
foreach($object->has_many as $key => $value)
  {
   $fields[$key] = $key;

  }
#79

[eluser]Spir[/eluser]
[quote author="WanWizard" date="1327608743"]Better then the extra parameter.

In the end I did this:
Code:
strpos($orderby, '.') === FALSE AND $orderby = $this->add_table_name($orderby);
[/quote]it seems it load some bug.
Here is what I've made :
Code:
$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 :
Quote: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 Tongue
#80

[eluser]WanWizard[/eluser]
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...




Theme © iAndrew 2016 - Forum software by © MyBB