CodeIgniter Forums
(Datamapper) One-to-Many Self Relationships [SOLVED] - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: (Datamapper) One-to-Many Self Relationships [SOLVED] (/showthread.php?tid=44240)



(Datamapper) One-to-Many Self Relationships [SOLVED] - El Forum - 08-08-2011

[eluser]Empu[/eluser]
Hi...

I have a problem with a one-to-many self relationships

How to describe a model for the table:
Code:
`id` int(5) unsigned NOT NULL AUTO_INCREMENT,
`parent_id` int(5) unsigned DEFAULT NULL,
`name` varchar(255) NOT NULL
parent_id is nullable

I tried with the code:
Code:
/* model */
class Organization extends DataMapper {

    var $table = 'organizations';
    var $has_one = array(
    'parent' => array(
        'class' => 'organization',
        'other_field' => 'organization',
        'join_self_as' => 'parent'
    ));
    var $has_many = array(
    'organization' => array(
        'other_field' => 'parent'
    )
    );

/* controller */
$org = new Organization();
$org->include_related('parent', array('name'));
$list = $org->get();
but it appears an error
Quote:Fatal error: Cannot use object of type Organization as array in Z:\htdocs\application\libraries\datamapper.php on line 2817

I tried looking in the manual, but could not find guides for it.

Thanks for helping Smile

Note: im using CI2, Datamapper1.8.1 and HMVC.


(Datamapper) One-to-Many Self Relationships [SOLVED] - El Forum - 08-09-2011

[eluser]WanWizard[/eluser]
Read this page carefully: http://datamapper.wanwizard.eu/pages/reservednames.html

The reason you get these errors is that you use the reserved word 'parent' here. By doing so, you'll overwrite the value of the objects parent relation. Replace 'parent' by 'holding' and it will work fine.

p.s. the 'join_self_as' on the has_one is not needed, it works fine without.


(Datamapper) One-to-Many Self Relationships [SOLVED] - El Forum - 08-09-2011

[eluser]Empu[/eluser]
That was my mistake, does not read the manual carefully.

Now the databases structure to be like this,

Code:
`id` int(5) unsigned NOT NULL AUTO_INCREMENT,
`holding_id` int(5) unsigned DEFAULT NULL,
`name` varchar(255) NOT NULL

and the model & controller to be like this,

Code:
/* model */
class Organization extends DataMapper {

    var $table = 'organizations';
    var $has_one = array(
    'holding' => array(
        'class' => 'organization',
        'other_field' => 'organization'
    ));
    var $has_many = array(
    'organization' => array(
        'other_field' => 'holding'
    )
    );
// ...


/* controller */
$org = new Organization();
$org->include_related('holding', array('name'));
$list = $org->get();

As you say, it's work fine.

Thanks so much...


(Datamapper) One-to-Many Self Relationships [SOLVED] - El Forum - 08-10-2011

[eluser]valuk[/eluser]
Hi, I have the same problem with different error.

Quote:Table 'najdiobrtnika.selfconnects_tests' doesn't exist

SELECT `test`.*, `selfconnect_test`.`naziv` AS selfconnect_naziv FROM (`test`) LEFT OUTER JOIN `selfconnects_tests` selfconnect_selfconnects_tests ON `test`.`id` = `selfconnect_selfconnects_tests`.`test_id` LEFT OUTER JOIN `test` selfconnect_test ON `selfconnect_test`.`id` = `selfconnect_selfconnects_tests`.`selfconnect_id`

Filename: D:\work\web site\najdiobrtnika.si\ci_system\database\DB_driver.php

Line Number: 330

Model
Code:
var $has_one = array
    (
        'selfconnect' => array
        (
            'class' => 'test',
            'other_field' => 'test',
        )
    );
    

    var $has_many = array
    (
        'test' => array
        (
            'other_field' => 'selfconnect',
        )
    );

Table Test

id
test_id
title

Any ideas? Thanks


(Datamapper) One-to-Many Self Relationships [SOLVED] - El Forum - 08-10-2011

[eluser]WanWizard[/eluser]
Difficult to comment as you don't provide all information.

What is 'naziv'? You select it, but it isn't part of your test table? What was the query you constructed?

You will get these kind of errors when Datamapper can't find the foreign key in the has_many table, and then assumes that you're using a relationship table instead.


(Datamapper) One-to-Many Self Relationships [SOLVED] - El Forum - 08-10-2011

[eluser]valuk[/eluser]
Sorry. Naziv is same as title. So insted of title I used naziv.

Controller

Code:
t = new Test();
$t->where('id', 2)->get();
$t->test->get();
echo $t->test->naziv;

Is it better?

I notice that this error occured if I used filed name test_id. If I replace it with self_id or holding it is OK.

Thanks


(Datamapper) One-to-Many Self Relationships [SOLVED] - El Forum - 08-10-2011

[eluser]Empu[/eluser]
test `table` has no field `selfconnect`.
dont forget define 'self_join_as' key in some unusual self-relationships. CMIIW Smile