Welcome Guest, Not a member yet? Register   Sign In
IgnitedRecord 1.0 pre-release

[eluser]talmdal[/eluser]
Thanks for the quick response. That was sort of the answer I expected.

[eluser]Heli[/eluser]
Hello again, I've got one more problem.
Code:
$modules = &IgnitedRecord;::factory('modules',array('has_many'=>array('table'=>'modules_avalible_methods','name'=>'mam','fk'=>'module_id') ));
$modules = &$modules->find_all();
// this was taken from view

foreach ($modules as $module):

    $methods = &$module->related('mam')->get();

    if ($methods):
        foreach ($methods as $method):
            $method->has_one('mmi',array('table'=>'modules_method_items','fk'=>'method_id'));// after this it drop some errors
            
            $setings = &$method->related('mmi')->get();
            $items = &IgnitedRecord;::factory($setings->from_tbl)->select($setings->select_by)->find_all();
            foreach ($items as $item):
                $real_url = $module->name."/".$method->name."/".$item->{$setings->select_by};
echo $real_url."<br>";

            endforeach;
        endforeach;
    endif;
endforeach;
Errors:
Code:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: IR_record::$__instance
Filename: ignitedrecord/record.php
Line Number: 440

A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: ignitedrecord/record.php
Line Number: 440

A PHP Error was encountered
Severity: Warning
Message: array_keys() [function.array-keys]: The first argument should be an array
Filename: ignitedrecord/record.php
Line Number: 440

A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: ignitedrecord/record.php
Line Number: 440


An Error Was Encountered
IR_record: Method has_one() is not found.
Called on line 71 in C:\xampp\htdocs\cms\system\application\modules\menu\controllers\menu.php.
Maybe doing somthing wrong, but I need thous relations.

[eluser]m4rw3r[/eluser]
The problem is that you have to have a model connected to a table to be able to relate from records which are mapped to the same table.
The DataMapper pattern (which IgnitedRecord follows) says that all database interaction should lie in the model, and it does.

Hence you have to create a model for the methods, and define the relation in that model:
Code:
$this->methods =& IgnitedRecord::factory('modules_avalible_methods', array('has_many' => 'mmi')); // don't know what parameters you have there

And that Has One relation (named mmi), you should use a JOIN there:
Code:
$this->modules = &IgnitedRecord;::factory('modules',array('has_many'=>array('table'=>'modules_avalible_methods','name'=>'mam','fk'=>'module_id') ));

$modules = &$this->modules->find_all();
// this was taken from view

foreach ($modules as $module):

    // join mmi
    $methods = &$module->related('mam')->join_relatd('mmi')->get();

    if ($methods):
        foreach ($methods as $method):
            
            $items = &IgnitedRecord;::factory($method->mmi_from_tbl)->select($method->mmi_select_by)->find_all();
            foreach ($items as $item):
                $real_url = $module->name."/".$method->name."/".$item->{$setings->select_by};
echo $real_url."<br>";

            endforeach;
        endforeach;
    endif;
endforeach;

[eluser]Heli[/eluser]
Now she does not accept the name of the table and trying to calculate its own.
Code:
$this->methods = IgnitedRecord::factory('modules_avalible_methods',array('has_one'=>array('table'=>'modules_method_items','name'=>'mmi','fk'=>'method_id')));

foreach ($modules as $module):

    $methods = &$module->related('mam')->join_related('mmi')->get();
    print_r($methods);
    if ($methods):
        foreach ($methods as $method):
            $items = &IgnitedRecord;::factory($method->mmi_from_tbl)->select($method->mmi_select_by)->find_all();
            foreach ($items as $item):
                $real_url = $module->name."/".$method->name."/".$item->{$setings->select_by};
...
And extradite SQL error
Code:
A Database Error Occurred

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS `mmi_`, `modules_avalible_methods`.* FROM `modules_avalible_methods` LEFT JOI' at line 1

SELECT `mmis`. AS `mmi_`, `modules_avalible_methods`.* FROM `modules_avalible_methods` LEFT JOIN `mmis` ON modules_avalible_methods.id = mmis_method_id WHERE `modules_avalible_methods`.`module_id` = '1'

[eluser]m4rw3r[/eluser]
I fail to see why IgnitedRecord would create another tablename for you.

Can you please submit a dump of the IgnitedRecord object (the methods model), and only the has_one property, please?

[eluser]Heli[/eluser]
I tried everything reworked into separate models.
Module
Code:
class Module extends IgnitedRecord {
        var $has_many = array('table'=>'modules_avalible_methods','fk'=>'module_id','model'=>'mam');
    }
M_a_m
Code:
class M_a_m extends IgnitedRecord {
        var $belongs_to = array('table'=>'modules','fk'=>'module_id','model'=>'mam');
        var $has_one = array('table'=>'modules_method_items','fk'=>'method_id','model'=>'mmi');
    }
M_m_i
Code:
class M_m_i extends IgnitedRecord {
        var $belongs_to = array('table'=>'modules_avalible_methods','fk'=>'method_id','model'=>'mmi');
    }

And doing a similar code
Code:
$this->load->model('module');
$this->load->model('m_a_m');
$this->load->model('m_m_i');

$modules = $this->module->find_all();

foreach ($modules as $module):
    $methods = &$module->related('mam')->get();//return false
    if ($methods):
        foreach ($methods as $method):
            $items = &IgnitedRecord;::factory($method->mmi_from_tbl)->select($method->mmi_select_by)->find_all();
            foreach ($items as $item):
                $real_url = $module->name."/".$method->name."/".$item->{$setings->select_by};
...
But now I can not even connect related objects. Most likely, I have done something wrong in my relationship.
Another question: how to use the "cascade_on_delete".

[eluser]m4rw3r[/eluser]
The model names are wrong, if you declare a model class as "m_m_i" you should reference it with "m_m_i", not "mmi".

If you still have problems with the tablename in the relation
(declare it as:
Code:
var $table = 'whatevertableyouchose';
in the class).
Please submit a print_r() of the faulty object (but strip away everything except for has_many or whatever relation type you use, or if it's the 0.2.1 SVN, submit a print_r($model->relations))

The cascade on delete is still under development (need to do some revisions to it, but that will be in a later version), but currently it only supports one level.
It works almost as a table constraint (the ON DELETE CASCADE option), but currently only one level (if the related records have cascade on delete, it won't cascade a second time).

[eluser]steelaz[/eluser]
Shouldn't "modules_avalible_methods" be "modules_available_methods"?

[eluser]Heli[/eluser]
I simply changed in the settings 'model' to 'name', but I had trouble with names tables.
Once I identified in each model table name, IR immediately began trying to define himself and not take the table name from the settings.
Here is a print_r() of my models:
Code:
Module Object
(
    [table] => modules
    [id_col] => id
    [columns] =>
    [child_class] => IR_record
    [act_as] => Array
        (
        )
    [belongs_to] => Array
        (
        )
    [has_one] => Array
        (
        )
    [habtm] => Array
        (
        )
    [has_and_belongs_to_many] => Array
        (
        )
    [relations] => Array
        (
            [mam] => IR_RelProperty_has Object
                (
                    [parent_model] => Module Object
*RECURSION*
                    [plural] => 1
                    [table] => modules_available_methods
                    [fk] => module_id
                    [cascade_on_delete] =>
                )
        )
...

Code:
M_a_m Object
(
    [table] => modules_available_methods
    [id_col] => id
    [columns] =>
    [child_class] => IR_record
    [act_as] => Array
        (
        )
    [has_many] => Array
        (
        )
    [habtm] => Array
        (
        )
    [has_and_belongs_to_many] => Array
        (
        )
    [relations] => Array
        (
            [mmi] => IR_RelProperty_has Object
                (
                    [parent_model] => M_a_m Object
*RECURSION*
                    [plural] =>
                    [table] => mmis // do not take table name from setings
                    [fk] => method_id
                    [cascade_on_delete] =>
                )
            [mam] => IR_RelProperty Object
                (
                    [table] => mams // do not take table name from setings
                    [fk] => module_id
                    [cascade_on_delete] =>
                )
        )
...

Code:
M_m_i Object
(
    [table] => modules_method_items
    [id_col] => id
    [columns] =>
    [child_class] => IR_record
    [act_as] => Array
        (
        )
    [has_many] => Array
        (
        )
    [has_one] => Array
        (
        )
    [habtm] => Array
        (
        )
    [has_and_belongs_to_many] => Array
        (
        )
    [relations] => Array
        (
            [mmi] => IR_RelProperty Object
                (
                    [table] => mmis // do not take table name from setings
                    [fk] => method_id
                    [cascade_on_delete] =>
                )
        )
...

steelaz
Yes you are right: to be the case. But mistake is not here.

[eluser]m4rw3r[/eluser]
Thanks for the dumps!
They enabled me to reconstruct your scenario and debug it, and I found two bugs:

* join_related() bugs when using default columns, because empty(array(false)) == false when it should be empty(false) == true.
* The singular() method of IR_RelProperty was overwriting the tablename even when it was set earlier

They are now fixed in latest SVN (rev149).




Theme © iAndrew 2016 - Forum software by © MyBB