Welcome Guest, Not a member yet? Register   Sign In
Has-many I need guidance
#1

[eluser]toadies[/eluser]
Hello, I have been lurking on here for awhile and tried to read as much as I can. But I'm not familiar with Datamapper ORM.

I believe i'm trying to use Has-Many Relationships and wondering if someone could help me setup my relationships...

Here are my tables

customers:
id
first_name
last_name

equipment_groups:
id
name

equipments:
id
equipment_group_id
name

manufacturers:
id
equipment_id
name

So I assume I would create a 5th table called customers_equipments with the following fields
id
equipment_id
customer_id
manufacturer_id
model_name

In my model I put
Customer Class - var $has_many = array('equipment');
Equipment Class - var $has_many = array('customer');

I'm not worried about getting manufacture name or group name yet...

In the controller I would try to access my 1 entry in customers_equipments table, but I would get an entire list of all equipment names from equipment table, not what 1 entry in my table.

$e = new Equipment();
$e->get();
foreach($e as $equip){
echo $equip->name
echo $equip->manufacturer_id;
}

Please help!

Thanks,
Chris
#2

[eluser]InsiteFX[/eluser]
You should ask this in the DataMapper forum topic.
#3

[eluser]WanWizard[/eluser]
@loadies,

You can't use a relationship table to relate 3 tables at the moment, DataMapper will not be able to maintain the relations.

And $e->get() will get all equipment. If you want the equipment related to a customer, you need to run the query from the customer object:
Code:
$c = new Customer();
$c->get_by_id(1);

$c->equipment->get();
#4

[eluser]toadies[/eluser]
Ok so what if I want the remaing items in the customers_equipments table?

for example
$c = new Customer();
$c->get_by_id(1);
$c->equipment->get();

echo $c->equipment->manufacturer_id;
echo $c->equipment->model_name;

gives me nothing. I notice in your bugs model you have something like this but I can't figure out where you access it.




Theme © iAndrew 2016 - Forum software by © MyBB