EllisLab text mark
Advanced Search
     
(Datamapper) One-to-Many Self Relationships [SOLVED]
Posted: 08 August 2011 09:37 PM
Avatar
Joined: 2010-07-22
3 posts

Hi…

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

How to describe a model for the table:

`idint(5unsigned NOT NULL AUTO_INCREMENT,
`
parent_idint(5unsigned DEFAULT NULL,
`
namevarchar(255NOT NULL 

parent_id is nullable

I tried with the 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

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.

 
Posted: 09 August 2011 07:08 AM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4489 posts

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.

 Signature 

Me: WanWizard.eu | My company: Exite | Datamapper: DataMapper ORM <= LOOKING FOR A NEW MAINTAINER!

 
Posted: 09 August 2011 08:27 AM   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2010-07-22
3 posts

That was my mistake, does not read the manual carefully.

Now the databases structure to be like this,

`idint(5unsigned NOT NULL AUTO_INCREMENT,
`
holding_idint(5unsigned DEFAULT NULL,
`
namevarchar(255NOT NULL 

and the model & controller to be like this,

/* 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…

 
Posted: 10 August 2011 08:28 AM   [ # 3 ]   [ Rating: 0 ]
Joined: 2010-05-10
21 posts

Hi, I have the same problem with different error.

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

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

 Signature 

JComp

 
Posted: 10 August 2011 08:49 AM   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4489 posts

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.

 Signature 

Me: WanWizard.eu | My company: Exite | Datamapper: DataMapper ORM <= LOOKING FOR A NEW MAINTAINER!

 
Posted: 10 August 2011 08:55 AM   [ # 5 ]   [ Rating: 0 ]
Joined: 2010-05-10
21 posts

Sorry. Naziv is same as title. So insted of title I used naziv.

Controller

= 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

 Signature 

JComp

 
Posted: 10 August 2011 08:59 AM   [ # 6 ]   [ Rating: 0 ]
Avatar
Joined: 2010-07-22
3 posts

test `table` has no field `selfconnect`.
dont forget define ‘self_join_as’ key in some unusual self-relationships. CMIIW smile