model-mass-insertion This extension allows for easy mass insertion of models while retaining yii 1.1.14 implementation and model validation

  1. Summary
  2. Typical use case:
  3. Requirements
  4. Drawbacks
  5. Problems solved
  6. Links

Summary

This is meant to compliment the yii CDbCommandBuilder addition to allow many records to be inserted at once. This has a few inherent flaws which I will outline presently, but it also solves some painful issues as well.

Typical use case:

$insertMe = [];
foreach($models as $model)
{
     if( $model->validate() )
          $insertMe[] = $model->attributes;
     else
          throw new CException('Model validation failed, etc. etc.');
}

SomeClass::model()->insertMany($insertMe);

Requirements

  • this class, ActiveRecord, to be placed into somewhere meaningful (protected/components/, for example)
  • any models for which this functionality is desired should extend this class.

Drawbacks

  • code has to perform some magic with table schema in order to insert properly; don't know what happens if it fails and does not set back to original state.
  • could be further developed to loop over models and extract attributes as part of the insertMany() method; this could very easily be done but I always preferred to ensure greater flexibility rather than assume that all cases would be the same.
  • has not been tested against tables with multiple keys, or composite keys. It could prove problematic without some changes.

Problems solved

  • this makes it rather trivial to add many records at once while still using models for validation (you don't have to circumvent validation so that means you don't have to re-invent the wheel.
  • this is available to all classes which extend ActiveRecord, which is the parent class for all models if you follow the same methodology as we do.

Links

Project Page

0 0
2 followers
161 downloads
Yii Version: 1.1
License: MIT
Category: Database
Developed by: cottonaf
Created on: May 21, 2015
Last updated: 9 years ago

Downloads

show all

Related Extensions