Welcome Guest, Not a member yet? Register   Sign In
Datamapper, help with custom relationships
#1

[eluser]msteudel[/eluser]
I was hoping to get some help setting up some relationships. I have a couple of tables and foreign keys that were created by other libraries, and I need to setup the relationship manually

Code:
a3m_account
id | etc.

class User extends DataMapper {
var $table = 'a3m_account';
var $has_one = array( 'userdata' =>  array( 'other_field' => 'account_id' ) ); // I tried account too
}

a3m_account_details
id | account_id | etc.

class Userdata extends DataMapper {
var $table = 'a3m_account_details';
var $has_one = array( 'user' );
}

I get this error message:

Quote:An Error Was Encountered
DataMapper Error: 'account_id' is not a valid parent relationship for Userdata. Are your relationships configured correctly?

Anyway, any help would be appreciated.

TIA
#2

[eluser]WanWizard[/eluser]
Datamapper will automatically suffix the "_id", so don't include that in the definition.
#3

[eluser]msteudel[/eluser]
If I do just account in the definition I get

Quote:DataMapper Error: 'account' is not a valid parent relationship for Userdata. Are your relationships configured correctly?

And also am I retrieving the userdata correctly:

Code:
$u = new User();
$u->where( 'id', 3 )->get();

// this line ....
$u->userdata->get();
#4

[eluser]WanWizard[/eluser]
This is because 'other_field' has nothing to do with the column name.

The config values 'join_self_as' and 'join_other_as' define the FK's.
#5

[eluser]kuroyakedo[/eluser]
Hello, I'm new in the forum.
I have a problem like this, but I use de simple way to define relationchips.
Here is some code:

model Campus:
Code:
Class Campus extends Datamapper {

    var $table='campi';
    var $has_many=array('boleto');    

  function __construct()  {
    parent::__construct();  
  }
}

And boleto:
Code:
class Boleto extends Datamapper {

  var $table='boletos';
  var $has_one=array('campus');  

  function __construct(){
    parent::__construct();
  }
}

I have been working with these tables 5 months, even have the relation table:

| id | boleto_id | campus_id |

Everything was ok, but recently, every time I need to make something that includes that relation I got this error message:

Quote:DataMapper Error: 'boleto' is not a valid parent relationship for Campus. Are your relationships configured correctly?

Tables:
"boletos":
| id | folio |

"campi":
| id | nombre_campus |

"boletos_campi":
| id | boleto_id | campus_id |

I'm trying with this code:

Code:
$b = new Boleto();
$b->where('id',20114)->get();
$b->campus->get();
#6

[eluser]WanWizard[/eluser]
If it used to work, and no longer works, the first question is: what did you change?

p.s. for one-to-many's you don't need a relationship table, you can just add campus_id to the Boleto model.
#7

[eluser]kuroyakedo[/eluser]
That's the weird thing, it worked so I left it, I didn't change anything.

Now I added a new table, just testing, it's the same, just changed "campi" for "escuelas" and "campus" for "escuela", and it doesn't want to relate 'boleto' with 'escuela' .

#8

[eluser]WanWizard[/eluser]
Datamapper uses the Inflector helper to convert singular to plural and v.v., for example to automatically determine database names and relation table names from model names.

I can imagine that that utterly fails with non-english names. Perhaps you have to do some hardcoding?

You can do a var_dump() of the model to look at the properties, perhaps you can spot where it goes wrong.
#9

[eluser]kuroyakedo[/eluser]
I made a test with "school" and "schools" and it worked.

I'll try.

Thanks!!




Theme © iAndrew 2016 - Forum software by © MyBB