[eluser]toadies[/eluser]
[quote author="WanWizard" date="1328169209"]This works, but fires an awful lot of queries.
If you need related information and you need to loop over a resultset, it's usually best to add the required fields to the query using include_related().
[/quote]
I am not sure where to put include_related()? I would think I still have to select for each loop results.
[quote author="WanWizard" date="1328169209"]
Also note that Datamapper does not support threeway relations. Functionally it works fine, as you have noticed, but the relational logic can not deal with it.
Which means that if you delete a parent record, Datamapper will delete the relationship record that links the parent to the other tables. Regardless of the fact that the other two tables still need that record to define the relationship between them.
You have to be aware of this fact.
If you want to do this the safe way, include the relationship table as a normal table. Make a model for it, and define the relationship between the three models and the relationship model as one-to-many.[/quote]
My 3 models (customer, equipment, manufacturer) has $has_many = array('customers_equipments'); and customers_equipments model has $has_one = array('customer','equipment','manufacturer'); Is this what you mean to prevent the delete relationships?
Can you or someone please share with me the best way to use datamapper to recreated this query? I don't necessary need customer model just need to a where clause for customer_id
Code:
SELECT c.id, e.id, e.equipment_type, c_e.manufacturer_id, c_e.model_name FROM (customers c, equipments e) LEFT OUTER JOIN customers_equipments c_e ON c.id = c_e.customer_id AND e.id = c_e.equipment_id WHERE c.id=1;
results
Code:
+----+----+------------------------+-----------------+--------------+
| id | id | equipment_type | manufacturer_id | model_name |
+----+----+------------------------+-----------------+--------------+
| 1 | 1 | Pump | 1 | Turbo Jet |
| 1 | 2 | Booster Pump | 2 | Filter 10000 |
| 1 | 3 | Filter | NULL | NULL |
| 1 | 4 | Heater | NULL | NULL |
| 1 | 5 | Chlorinator | NULL | NULL |
| 1 | 6 | Salt System | NULL | NULL |
| 1 | 7 | Cleaner | NULL | NULL |
| 1 | 8 | Blower Motor | NULL | NULL |
| 1 | 9 | Control System | NULL | NULL |
| 1 | 10 | Skimmer | NULL | NULL |
| 1 | 11 | Light | NULL | NULL |
| 1 | 12 | Aux Pump | NULL | NULL |
| 1 | 13 | Clean Filter Pressurer | NULL | NULL |
| 1 | 14 | Last Filter Clean | NULL | NULL |
| 1 | 15 | Pool Capacity | NULL | NULL |
+----+----+------------------------+-----------------+--------------+