Welcome Guest, Not a member yet? Register   Sign In
Need help understanding DataMapper and Relationships
#1

[eluser]xzela[/eluser]
Hey All,

I'm a bit confused on how DataMapper handles existing data relationships. In other words, if you have a Map table that maps the relationships between two other data objects, how to do you describe that relationship to DataMapper? I've been searching the forums, stackoverflow, and google for quite a while now but have been unable to get a solid answer or example. So, here I am...

Below is a simplified database schema and model descriptions:

The Performer table describes any given performer, in this case, just their name
Code:
-- Performer table
[PerformerId] [Name]
1              Billy
2              Sally
3              John

The Show table describes any given show, in this case, just a show title
Code:
-- Show table
[ShowId] [ShowTitle]
1         Show 1
2         Show 2
3         Show 3

The PerformerShowMap table describes which performers are performing in which shows.
Code:
-- PerformerShowMap table
[Id] [ShowId] [PerformerId]
1     1        1
2     1        2
3     1        3
4     2        1
5     3        1
6     3        3


The Performer Model
Code:
class Performer extends DataMapper {
  var $table = "Performer";
  // relationship descriptions here
}

The Show Model
Code:
class Show extends DataMapper {
  var $table = "Show";
  // relationship descriptions here  
}

As you can see from the above example, Show 1 has three performers applied to it (based on the information supplied in PerformerShowMap). When I access Show 1, I should be able to also get the three performers as well.

So, my ultimate question is: How do I describe relationships between Show and Performer while using the PerformerShowMap table to DataMapper?

Code:
// in some random controller
$show = new Show();
$show->get_by_id(3);
$show->performers; //should contain all of the performers for the show with an id of "3"

Any help on this subject would be great.

Thanks
#2

[eluser]WanWizard[/eluser]
Check out the docs (http://datamapper.wanwizard.eu/pages/relationtypes.html) on relationships.

Unfortunately you didn't do this before you started working (you might want to check the rest of the manual too), because now you've chosen primary keys and foreign keys that are not compatible with Datamapper (it requires "id" as PK for every table, and FK's like "show_id" and "performer_id", not "ShowId" and "PerformerId").

If you standardize your design, then it's simply defining a has_many in both models, pointing to the other model, and you're off.


#3

[eluser]xzela[/eluser]
Hey WanWizard,

Thanks for replying.

Unfortunately the database that i'm using was created a long time ago and many (many) tables do not follow the standard ORM setup. Very few tables actually have a primary key labeled id. In fact, the Performers table actually has emp_id as the primary key (because every Performer is actually an Employee, not sure who decided to call the table Performers!?). Other tools happen to use this same database, thus renaming columns would cause a lot of problems.

So, perhaps I should be asking a different question: Is there any way to get DataMapper to handle these improper column names and structures? Such as, telling DataMapper to use emp_id as the primary key instead of the standard id.

(and just to emphasize the database weirdness a bit more, the Show table primary key is shootid, note that lack of underscore and totally different word in this example. I guess that's what happens when you have 3 to 4 people independently working on a codebase over the course of 7 years.)

Let me know what you think.

Thanks.
#4

[eluser]WanWizard[/eluser]
Short answer: no, there isn't. The 'id' bit (and the "_id" suffix on FK's) is hardcoded.
#5

[eluser]Unknown[/eluser]
hi

I want to ask about how DataMapper relate the models..

I have model like this:

Code:
class Model_Pelanggan extends DataMapper
{
public $table = 'pelanggan';

var $has_many = array
(
  'barang' => array
  (
   'class' => 'model_barang',
   'other_field' => 'pelanggan',
   'join_table' => 'barang_pelanggan'
  ),
  
  'transaksi' => array
  (
   'class' => 'model_transaksi',
   'other_field' => 'pelanggan'
  )
);
}

Table Pelanggan:
id
nama
telepon
created
updated

and

Code:
class Model_Transaksi extends DataMapper
{
public $table = 'transaksi';

var $has_one = array
(
  'pelanggan' => array
  (
   'class' => 'model_pelanggan',
   'other_field' => 'transaksi'
  )
);

var $has_many = array
(
  'transaksi_detail' => array
  (
   'class' => 'model_transaksi_detail',
   'other_field' => 'transaksi'
  )
);
}

Table Transaksi:
id
pelanggan_id
total
created
updated

I alr tried code like in here too, but I don't know why it search for joining table as described in error message:

Code:
A Database Error Occurred

Error Number: 1146

Table 'asd.pelanggan_transaksi' doesn't exist

DELETE FROM `pelanggan_transaksi` WHERE `pelanggan_id` = 1

Filename: C:\xampp\htdocs\asd\system\database\DB_driver.php

Line Number: 330
#6

[eluser]yoelrodguez[/eluser]
Hello to correct the error models provides as follows:


class Model_Pelanggan extends DataMapper
{
public $table = 'pelanggan';

var $has_many = array
(
'barang' => array
(
'class' => 'model_barang',
'other_field' => 'pelanggan',
'join_table' => 'barang_pelanggan'
),

'transaksi' => array
(
'class' => 'model_transaksi',
'other_field' => 'pelanggan_id'
)
);
}

class Model_Transaksi extends DataMapper
{
public $table = 'transaksi';

var $has_one = array
(
'pelanggan' => array
(
'class' => 'model_pelanggan',
'other_field' => 'transaksi'
)
);

var $has_many = array
(
'transaksi_detail' => array
(
'class' => 'model_transaksi_detail',
'other_field' => 'id'
)
);
}

If you want to leave it as you just have to create the table that asks you which is the ratio of which corresponds to the ratio of one-to-many you have in the model







Theme © iAndrew 2016 - Forum software by © MyBB