How to access the auto-generated ID from a Phinx migration insert?
PHP Phinx is a Database Seeder / Migration library from CakePHP.
This will create a fake user, but how would I create a row that has a FOREIGN KEY
(or regular ID reference) to the id
of that new user, i.e. a sub-user?
<?php
use PhinxSeedAbstractSeed;
class UserSeeder extends AbstractSeed
{
public function run()
{
$faker = FakerFactory::create();
$data = ;
for ($i = 0; $i < 100; $i++) {
$data = [
'username' => $faker->userName,
'password' => sha1($faker->password),
'password_salt' => sha1('foo'),
'email' => $faker->email,
'first_name' => $faker->firstName,
'last_name' => $faker->lastName,
'created' => date('Y-m-d H:i:s'),
];
}
$this->insert('users', $data);
}
}
php cakephp phinx
add a comment |
PHP Phinx is a Database Seeder / Migration library from CakePHP.
This will create a fake user, but how would I create a row that has a FOREIGN KEY
(or regular ID reference) to the id
of that new user, i.e. a sub-user?
<?php
use PhinxSeedAbstractSeed;
class UserSeeder extends AbstractSeed
{
public function run()
{
$faker = FakerFactory::create();
$data = ;
for ($i = 0; $i < 100; $i++) {
$data = [
'username' => $faker->userName,
'password' => sha1($faker->password),
'password_salt' => sha1('foo'),
'email' => $faker->email,
'first_name' => $faker->firstName,
'last_name' => $faker->lastName,
'created' => date('Y-m-d H:i:s'),
];
}
$this->insert('users', $data);
}
}
php cakephp phinx
add a comment |
PHP Phinx is a Database Seeder / Migration library from CakePHP.
This will create a fake user, but how would I create a row that has a FOREIGN KEY
(or regular ID reference) to the id
of that new user, i.e. a sub-user?
<?php
use PhinxSeedAbstractSeed;
class UserSeeder extends AbstractSeed
{
public function run()
{
$faker = FakerFactory::create();
$data = ;
for ($i = 0; $i < 100; $i++) {
$data = [
'username' => $faker->userName,
'password' => sha1($faker->password),
'password_salt' => sha1('foo'),
'email' => $faker->email,
'first_name' => $faker->firstName,
'last_name' => $faker->lastName,
'created' => date('Y-m-d H:i:s'),
];
}
$this->insert('users', $data);
}
}
php cakephp phinx
PHP Phinx is a Database Seeder / Migration library from CakePHP.
This will create a fake user, but how would I create a row that has a FOREIGN KEY
(or regular ID reference) to the id
of that new user, i.e. a sub-user?
<?php
use PhinxSeedAbstractSeed;
class UserSeeder extends AbstractSeed
{
public function run()
{
$faker = FakerFactory::create();
$data = ;
for ($i = 0; $i < 100; $i++) {
$data = [
'username' => $faker->userName,
'password' => sha1($faker->password),
'password_salt' => sha1('foo'),
'email' => $faker->email,
'first_name' => $faker->firstName,
'last_name' => $faker->lastName,
'created' => date('Y-m-d H:i:s'),
];
}
$this->insert('users', $data);
}
}
php cakephp phinx
php cakephp phinx
edited Nov 21 at 2:57
miken32
23.4k84672
23.4k84672
asked Nov 21 at 2:40
Jonathan
2,46412437
2,46412437
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
This should return the equivalent of SELECT LAST_INSERT_ID()
:
$this->getAdapter()->getConnection()->lastInsertId();
Reference: https://github.com/cakephp/phinx/issues/819
Exactly what what I needed, thanks. I'd be curious how it behaves with multipleinsert
s before asave
? Also does it have to be called aftersave()
?
– Jonathan
Nov 21 at 2:52
That value would be updated with everyINSERT
operation on the database table, so if you do multiple inserts in a single transaction you'd only see the last one. Note that this isn't table specific either, it will return the last ID from any table that had the most recent insert.
– miken32
Nov 21 at 2:54
I'm not familiar with Phinx, so not sure if inserts are queued up and executed withsave()
or what.
– miken32
Nov 21 at 2:56
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404570%2fhow-to-access-the-auto-generated-id-from-a-phinx-migration-insert%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This should return the equivalent of SELECT LAST_INSERT_ID()
:
$this->getAdapter()->getConnection()->lastInsertId();
Reference: https://github.com/cakephp/phinx/issues/819
Exactly what what I needed, thanks. I'd be curious how it behaves with multipleinsert
s before asave
? Also does it have to be called aftersave()
?
– Jonathan
Nov 21 at 2:52
That value would be updated with everyINSERT
operation on the database table, so if you do multiple inserts in a single transaction you'd only see the last one. Note that this isn't table specific either, it will return the last ID from any table that had the most recent insert.
– miken32
Nov 21 at 2:54
I'm not familiar with Phinx, so not sure if inserts are queued up and executed withsave()
or what.
– miken32
Nov 21 at 2:56
add a comment |
This should return the equivalent of SELECT LAST_INSERT_ID()
:
$this->getAdapter()->getConnection()->lastInsertId();
Reference: https://github.com/cakephp/phinx/issues/819
Exactly what what I needed, thanks. I'd be curious how it behaves with multipleinsert
s before asave
? Also does it have to be called aftersave()
?
– Jonathan
Nov 21 at 2:52
That value would be updated with everyINSERT
operation on the database table, so if you do multiple inserts in a single transaction you'd only see the last one. Note that this isn't table specific either, it will return the last ID from any table that had the most recent insert.
– miken32
Nov 21 at 2:54
I'm not familiar with Phinx, so not sure if inserts are queued up and executed withsave()
or what.
– miken32
Nov 21 at 2:56
add a comment |
This should return the equivalent of SELECT LAST_INSERT_ID()
:
$this->getAdapter()->getConnection()->lastInsertId();
Reference: https://github.com/cakephp/phinx/issues/819
This should return the equivalent of SELECT LAST_INSERT_ID()
:
$this->getAdapter()->getConnection()->lastInsertId();
Reference: https://github.com/cakephp/phinx/issues/819
answered Nov 21 at 2:50
miken32
23.4k84672
23.4k84672
Exactly what what I needed, thanks. I'd be curious how it behaves with multipleinsert
s before asave
? Also does it have to be called aftersave()
?
– Jonathan
Nov 21 at 2:52
That value would be updated with everyINSERT
operation on the database table, so if you do multiple inserts in a single transaction you'd only see the last one. Note that this isn't table specific either, it will return the last ID from any table that had the most recent insert.
– miken32
Nov 21 at 2:54
I'm not familiar with Phinx, so not sure if inserts are queued up and executed withsave()
or what.
– miken32
Nov 21 at 2:56
add a comment |
Exactly what what I needed, thanks. I'd be curious how it behaves with multipleinsert
s before asave
? Also does it have to be called aftersave()
?
– Jonathan
Nov 21 at 2:52
That value would be updated with everyINSERT
operation on the database table, so if you do multiple inserts in a single transaction you'd only see the last one. Note that this isn't table specific either, it will return the last ID from any table that had the most recent insert.
– miken32
Nov 21 at 2:54
I'm not familiar with Phinx, so not sure if inserts are queued up and executed withsave()
or what.
– miken32
Nov 21 at 2:56
Exactly what what I needed, thanks. I'd be curious how it behaves with multiple
insert
s before a save
? Also does it have to be called after save()
?– Jonathan
Nov 21 at 2:52
Exactly what what I needed, thanks. I'd be curious how it behaves with multiple
insert
s before a save
? Also does it have to be called after save()
?– Jonathan
Nov 21 at 2:52
That value would be updated with every
INSERT
operation on the database table, so if you do multiple inserts in a single transaction you'd only see the last one. Note that this isn't table specific either, it will return the last ID from any table that had the most recent insert.– miken32
Nov 21 at 2:54
That value would be updated with every
INSERT
operation on the database table, so if you do multiple inserts in a single transaction you'd only see the last one. Note that this isn't table specific either, it will return the last ID from any table that had the most recent insert.– miken32
Nov 21 at 2:54
I'm not familiar with Phinx, so not sure if inserts are queued up and executed with
save()
or what.– miken32
Nov 21 at 2:56
I'm not familiar with Phinx, so not sure if inserts are queued up and executed with
save()
or what.– miken32
Nov 21 at 2:56
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404570%2fhow-to-access-the-auto-generated-id-from-a-phinx-migration-insert%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown