How to access the auto-generated ID from a Phinx migration insert?












1















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);
}
}









share|improve this question





























    1















    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);
    }
    }









    share|improve this question



























      1












      1








      1








      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);
      }
      }









      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 2:57









      miken32

      24.1k95073




      24.1k95073










      asked Nov 21 '18 at 2:40









      JonathanJonathan

      2,70322644




      2,70322644
























          1 Answer
          1






          active

          oldest

          votes


















          1














          This should return the equivalent of SELECT LAST_INSERT_ID():



          $this->getAdapter()->getConnection()->lastInsertId();


          Reference: https://github.com/cakephp/phinx/issues/819






          share|improve this answer
























          • Exactly what what I needed, thanks. I'd be curious how it behaves with multiple inserts before a save? Also does it have to be called after save()?

            – Jonathan
            Nov 21 '18 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 '18 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 '18 at 2:56











          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          1














          This should return the equivalent of SELECT LAST_INSERT_ID():



          $this->getAdapter()->getConnection()->lastInsertId();


          Reference: https://github.com/cakephp/phinx/issues/819






          share|improve this answer
























          • Exactly what what I needed, thanks. I'd be curious how it behaves with multiple inserts before a save? Also does it have to be called after save()?

            – Jonathan
            Nov 21 '18 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 '18 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 '18 at 2:56
















          1














          This should return the equivalent of SELECT LAST_INSERT_ID():



          $this->getAdapter()->getConnection()->lastInsertId();


          Reference: https://github.com/cakephp/phinx/issues/819






          share|improve this answer
























          • Exactly what what I needed, thanks. I'd be curious how it behaves with multiple inserts before a save? Also does it have to be called after save()?

            – Jonathan
            Nov 21 '18 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 '18 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 '18 at 2:56














          1












          1








          1







          This should return the equivalent of SELECT LAST_INSERT_ID():



          $this->getAdapter()->getConnection()->lastInsertId();


          Reference: https://github.com/cakephp/phinx/issues/819






          share|improve this answer













          This should return the equivalent of SELECT LAST_INSERT_ID():



          $this->getAdapter()->getConnection()->lastInsertId();


          Reference: https://github.com/cakephp/phinx/issues/819







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 2:50









          miken32miken32

          24.1k95073




          24.1k95073













          • Exactly what what I needed, thanks. I'd be curious how it behaves with multiple inserts before a save? Also does it have to be called after save()?

            – Jonathan
            Nov 21 '18 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 '18 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 '18 at 2:56



















          • Exactly what what I needed, thanks. I'd be curious how it behaves with multiple inserts before a save? Also does it have to be called after save()?

            – Jonathan
            Nov 21 '18 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 '18 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 '18 at 2:56

















          Exactly what what I needed, thanks. I'd be curious how it behaves with multiple inserts before a save? Also does it have to be called after save()?

          – Jonathan
          Nov 21 '18 at 2:52





          Exactly what what I needed, thanks. I'd be curious how it behaves with multiple inserts before a save? Also does it have to be called after save()?

          – Jonathan
          Nov 21 '18 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 '18 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 '18 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 '18 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 '18 at 2:56




















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Guess what letter conforming each word

          Port of Spain

          Run scheduled task as local user group (not BUILTIN)