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

[eluser]coldscooter[/eluser]
Can you return a result set with a certain field as the key, rather than the incrementing numeric?

[eluser]Stolz[/eluser]
[quote author="coldscooter" date="1338314073"]Can you return a result set with a certain field as the key, rather than the incrementing numeric?[/quote]

If the field is the "id" column, then set all_array_uses_ids = TRUE in your DM config file and the "all" array will be populated usin the id field as key.

Also it may be helpfull the all_to_single_array($field) method form the array extension
http://datamapper.wanwizard.eu/pages/ext...array.html

[eluser]WanWizard[/eluser]
@coldscooter,

where_related() is the same as where(), but with an extra first parameter indicating the related object.

It is documented here: http://datamapper.wanwizard.eu/pages/getadvanced.html, see "$object->{query}_related_{model}($field, $value);" where "{query}" can be any of the defined query clauses (so you can use or_where_not_in_related() if you wish).

[eluser]WanWizard[/eluser]
[quote author="coldscooter" date="1338314073"]Can you return a result set with a certain field as the key, rather than the incrementing numeric?[/quote]
Not with a certain field, only with the id.

[eluser]scottzirkel[/eluser]
I've been reading through this thread, but I don't seem to see an answer, so I'll go ahead and ask, sorry if it's been covered before.

I have a form that inserts data into two tables. Business data goes into the businesses table and address data goes into the addresses table. The relationship is set up so that a business can have multiple addresses, and it pulls data out just fine. However, when I go to put it in, it fails to establish the relationship. In the examples, there is always a get on one of the tables to properly map an id, however as both datasets are being written, I do not have the id for the business yet. The tables are joined on a business_id field in the address table.

The example code writes the data correctly to each table, however the business_id is always set to 1 in the addresses table, despite what the businesses.id actually is.
Code:
$a = new Address();
$a->address = $this->input->post('address');
$a->address_2 = $this->input->post('address_2');
$a->city = $this->input->post('city');
$a->state = $this->input->post('state');
$a->zip = $this->input->post('zip');
$a->zip = $this->input->post('poc_first_name');
$a->zip = $this->input->post('poc_last_name');
$a->zip = $this->input->post('poc_email');
  
$b = new Business();
$b->name = $this->input->post('name');
$b->phone = $this->input->post('phone');
$b->email = $this->input->post('email');
$b->fax = $this->input->post('fax');
$b->sales_rep = $this->input->post('sales_rep');
$b->owner_first_name = $this->input->post('owner_first_name');
$b->owner_first_name = $this->input->post('owner_last_name');
$b->zip = $this->input->post('notes');

$b->save($a);

Should it know to set the business_id to businesses.id or is there a way for me to get that id and pass it along to addresses in a separate save?

[eluser]WanWizard[/eluser]
You can't do this.

You've got a catch-22 here. To be able to relate $b to $a, $b needs to be saved first, otherwise you don't have a valid id. But in your code, Datamapper has to save $a first, otherwise it can't relate it to $b (it will notice too late that $a actually has the FK).

Try swapping the objects, so $a->save($b).

If that gives issues too, there is a bug reported that sometimes creating the relation fails when both objects are new, which I haven't looked into. If this is the case, save $b first, before you save $a and create the relation.

[eluser]scottzirkel[/eluser]
Yeah, that's kind of what I figured. My first thought was to save $b before $a, but is there a way to get the inserted id of $b? I could return all and just use the last one, but that's assuming two people weren't filling out the form at the same time, which doesn't seem like a sure thing.

[eluser]WanWizard[/eluser]
The id of the object is generated using AUTO INCREMENT, so your RDBMS should make sure it is unique.

After saving a new object, the inserted id is assigned to the id property, so you can just use $b->id.
If it is indeed the save issue, $a->save($b->save()) should already do the trick.

[eluser]scottzirkel[/eluser]
Never could get the save to figure out the relations, but $b->id worked like a charm. That was my first thought, just didn't realize $b->id would be available. I might have missed that in the user guide.

Thanks!

[eluser]7amza[/eluser]
Hey guys !
im getting this error everytime i call models using ORM lib :
Code:
Could not find the language line "table_field"
and the model stop!!
any solution?




Theme © iAndrew 2016 - Forum software by © MyBB