We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26503
    • 620 Posts
    Is there a method with xPDO to do a bulk insert? example:

    $genres = array('A Capella','Acid','Acoustic','African-American','Afro','Afro Beat','Alternative','Ambient','Americana',******several hundred genres****'Tropicalisimo','Turkish','Urban','None of the above');
    	
    	foreach ($genres as $genre) {
    		$addgenre = $modx->newObject('ArtistGenres');
    		$addgenre->fromArray(array(
    			'genre' => $genre,
    		));
    		$addgenre->save();
    	}
    
    


    A better way than just looping over the genre array?

    -thanks
    -sean

    An afterthought - how can I reset the primary key's index via xPDO? normally I would something like:
    alter table foo AUTO_INCREMENT = 1
    

    [ed. note: sean69 last edited this post 14 years, 6 months ago.]
      *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

      Sean Kimball CLP, CLS.
      Technical Director / Sr. Developer | BigBlock Studios
      ._______________________________________________.
      Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
      27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
      phone/fax: 905-426-5525
      • 22303 MODX Staff
      • 10,725 Posts
      There is currently no batch insert capability in xPDO, but would certainly love to explore adding that feature.

      As for the modifying the auto_increment value for a table represented by an xPDO class, I would just do this:
      <?php
      $modx->exec("ALTER TABLE {$modx->getTableName('ArtistGenres')} AUTO_INCREMENT = 1");
      


      OR just truncate the table which will delete all rows and reset the auto_increment:
      <?php
      $modx->exec("TRUNCATE TABLE {$modx->getTableName('ArtistGenres')}");
      

        • 26503
        • 620 Posts
        Hmmmm looking at your example [cool - thanks, BTW!] something like this should work?

        <?php
        $modx->exec("insert into {$modx->getTableName('ArtistGenres')} ('genre') values  ('A Capella'),('Acid'),('Acoustic'),('African-American').. etc... ");
        

        or:
        <?php
        $modx->exec("LOAD DATA INFILE '/tmp/data/genres.dat' into table {$modx->getTableName('ArtistGenres')}");
        


        ??

        -sean
          *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

          Sean Kimball CLP, CLS.
          Technical Director / Sr. Developer | BigBlock Studios
          ._______________________________________________.
          Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
          27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
          phone/fax: 905-426-5525
          • 22303 MODX Staff
          • 10,725 Posts
          They should as long as they are a single SQL statement, not multiple separated via a semi-colon.