Tuesday, June 30, 2009

Re: Inserting record with id

On Tue, Jun 30, 2009 at 12:21 PM, Roman Brunnemann<mailinglists@gmx.de> wrote:
> Hi,
>
> i have a table products with the following columns ( simplified )
>
> id
> shop_id
> name
>
> id and shop_id are the primary key.

CakePHP does not work with multi-column PKs. o you mean that shop_id
is the Foreign Key?

> Now I have some kind of importer and want to insert data preparing my array
> like
>
> $i['Product']['id'] = $id;
> $i['Product']['shop_id'] = $shop_id;
> $i['Product']['name'] = $name;
>
> App::import('Model', 'Product' );
> $Product = new Product();
> $Product->save($i);
>
> But it does not save the id, only shop_id and name. I tried to add a
> fieldList to the save function, but it didn't work.

Normally, your PK would be an auto_increment column (for MySQL; serial
for Postgres, etc.). IOW, you wouldn't explicitly set the PK when
creating a record.

Also, why are you importing Product? Normally, when saving in a loop,
you'd do something along the lines of:

$this->Product->create($data);
$this->Product->save();

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: