Welcome Guest, Not a member yet? Register   Sign In
DataMapper ORM v1.8.2

[eluser]WanWizard[/eluser]
[quote author="darkylmnx" date="1360750896"]As to say, i'v already used the used TRUE as third parameter but that only works for one - to - one[/quote]

I've looked into the code, and I can't find a particular reason why that wouldn't run. It would create a mess probably. Reason for that is that for "many" relations, data of the parent is repeated in the result for all children (the normal join result), and the code doesn't seem to be able to deal with that. It will try to create new objects for every row.

So if a user has 10 groups, I assume the user object (with the same data) would exist 10 times). And the question will be what will happen with the group data. I haven't looked into that detail, but I think each of the 10 identical user records will get a single group.

[quote author="darkylmnx" date="1360750896"]
(and sorry for my english, i'm french)[/quote]

No worries, I'm not a native speaker either. Wink

[eluser]Maglok[/eluser]
I made a quick CI test with just datamapper, a Organisation class that has many Address.

I looked around in the code where you mentioned and turns out I never get there. At #5071 is a IF statement that makes the code jump to #5173. The reason it does not seem to work there is because $object->exists() seems to return FALSE. Checking exists() leads to the Address's $this->id is not set, thus there doesn't exist a record in the database. This seems likely, because the _save_relationship() should also be saving the object if it is new and then make a relationship between Organisation and Address. It then sets 'Unable to save the %s relationship: The related object was not saved.' as the error message.

Bottomline: It would appear that _save_relationship() checks if the Address is in the database, sees it isn't, and exits?

[eluser]WanWizard[/eluser]
I think $this must always exist, but $object may be a new one.

is it fixed when you add just before L#5071:
Code:
$object->exists() or $object->save();

[eluser]davidMC1982[/eluser]
When using include_related or similar on a has_many relationship, how can you limit the included result to just one.

Specifically:

Products - id, name, manufacturer_id
Items - id, product_id, ratetype_id, price
Ratetypes - id, name, is_sell
Manufacturer - id, name

Products have many Items and one Manufacturer
Items have one Product and one Ratetype

I want to list the products from a specific manufacturer, including the price from the item table, where the price is the lowest for that product.

I'd be happy with a plain old sql query but it must return a Product object as I am using the same view in many places throughout my site.

Thanks for any ideas.

[eluser]WanWizard[/eluser]
If things get complex, you can just add a method to the model that runs a standard CI DB query.

Feed the result on your custom query (a CI_DB_Result object) into $this->_process_query(), and it will convert the result back into the model instance you call your method on.

The only requirement is that 'id' of the models table MUST be included in the result.

[eluser]davidMC1982[/eluser]
Thanks. That's an interesting approach and would help to tidy up my controllers. I ended up doing this:

Code:
$sql_sell = "SELECT products.*, manufacturers.name AS manufacturer, MAX(price) AS list_price FROM items JOIN ratetypes ON items.ratetype_id=ratetypes.id JOIN products ON items.product_id=products.id JOIN manufacturers ON products.manufacturer_id=manufacturers.id WHERE is_sell=1 AND sell=1 AND manufacturer_id=? GROUP BY product_id ORDER BY sort_order DESC, id ASC;";
$sql_buy = "SELECT products.*, manufacturers.name AS manufacturer, MIN(price) AS list_price FROM items JOIN ratetypes ON items.ratetype_id=ratetypes.id JOIN products ON items.product_id=products.id JOIN manufacturers ON products.manufacturer_id=manufacturers.id WHERE is_sell=0 AND price>0 AND buy=1 AND manufacturer_id=? GROUP BY product_id ORDER BY sort_order DESC, id ASC;";
  
//Create the product object
$products = new Product();
  
//Get the sell mode
$sql = "sql_" . $this->session->userdata('mode');
  
//Execute the query
$data['products'] = $products->query($$sql, array($manufacturer_id));

[eluser]Maglok[/eluser]
[quote author="WanWizard" date="1360859617"]I think $this must always exist, but $object may be a new one.

is it fixed when you add just before L#5071:
Code:
$object->exists() or $object->save();
[/quote]
Yeah I've been sick, it is hitting the country by storm.

Yes that does fix it. Big Grin

[eluser]WanWizard[/eluser]
Yeah, a lot of people have been ill recently. Me included. Wink

Thanks for the feedback, I'll commit the fix.

[eluser]rzb13[/eluser]
Newbie question.

I'm using the same controller method to save or update an object.

What would be the best approach to achieve deletion of relations that were not included in $obj->save($related) ?

To be more specific, say I have a form which has a 'select multiple' to let the user chose his groups.

In the controller, I will have a $post['groups'] that I will use to populate $groups and save as user related. Say the user has removed some groups from the form Will I have to:

- check if user exists
- get his current groups
- compare to the $post['groups']
- delete different related groups
- save new related groups

Or am I missing some simpler syntax?

[eluser]WanWizard[/eluser]
Quickest way is probably to use delete() to delete the existing relation between the user object and groups, and then save the new one(s).




Theme © iAndrew 2016 - Forum software by © MyBB