Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to read record with composite primary key #7088

Closed
brandher opened this issue Jul 22, 2015 · 5 comments
Closed

how to read record with composite primary key #7088

brandher opened this issue Jul 22, 2015 · 5 comments

Comments

@brandher
Copy link

Hi there, really wondering how this works:

I have a table with a combined primary key. Its not necessary to have relational tablestructure in this case, because its only a temporary table to hold the products which a user puts in a basket. I just want to ensure that at the moment of ordering the basket, the products will be still the same as when put in the basket, in case somebody is changing product details in meanwhile

structure is:

basket_id
user_id
id (of product)
name (of product)
description (of product)
partnumber (of product)
price (of product)
quantity (of product to be ordered)
EAN (of product)
weight (of product)

the combined key ist: basket_id + user_id + id

at the moment I do:
$basketProducts = $this->Bproducts->find()
->where(['basket_id' => $basketId, 'user_id' => $userId, 'id' => $productId])
->toArray();

I wonder how to specify with get() since from my understaning I can only pass one value as key to get()

@lorenzo
Copy link
Member

lorenzo commented Jul 22, 2015

After you had specified what the primary key is (in database or in the initialize method) :

$this->primaryKey(['basket_id', 'user_id', 'id']);

You can now use the order in which the keys are defined to pass it to the get() function:

$table->get([$basketID, $userID, $id]);

@lorenzo lorenzo closed this as completed Jul 22, 2015
@lorenzo lorenzo added this to the 3.0.11 milestone Jul 22, 2015
@brandher
Copy link
Author

honestly that easy?
primary key is already defined in table.
did not think about specifying like that in get().
i could not find some hint in documentation, about that yesterday night. thanks so far.

@brandher
Copy link
Author

Sorry, this is not working in this way.
I receive following error:
Record not found in table "bproducts" with primary key [38, 9, 2]

this is my table definition:
public function initialize(array $config)
{
$this->table('bproducts');
$this->displayField('name');
$this->primaryKey('basket_id', 'user_id', 'id');
}

this is the record in database:
image

@AD7six
Copy link
Member

AD7six commented Jul 22, 2015

$this->primaryKey('basket_id', 'user_id', 'id');

That's 3 arguments, not one (an array).

@brandher
Copy link
Author

thx, embarresing for me :-(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants