Currently I have about 40 models and whenever I have needed to do an insert its always just been on a single model, so I have been happy to just use my class model which extends Zend_Db_Table_Abstract
class Model extends Zend_Db_Table_Abstract
However, now one of my requirements is to read a CSV file and normalize it into my database. This means that I am doing an insert on about 7 tables.
How should I structure this? I obviously need to use a transaction. Should I create a class that extends nothing?
Like:
class Model {
public function __construct() {
// Then get my Zend_Db PDO connection which I have previously saved to registry?
$this->_db = Zend_Registry::get("db");
}
public function insertCsv($csv) {
data = array( .. )
$this->_db->insert( "table1", $data);
}
}
This appears incredibly hackeyed, and I don't like the way that looks. I'm getting my Zend_Db object from registry and I'm not extending anything for the class.
Should I instead create a new model for each table that I want to insert over? And in my controller start the transaction..
So
/application/default/controllers/ImportController.php
public function indexAction() {
// My CSV fetch... and create the $data arrays
$csv = $this->getCSV();
list($data1, $data2...) = $csv;
$db = Zend_Registry::get("db");
$db->beginTransaction();
// Create the Models
$model1 = new Model1();
$model2 = new Model2();
$model3 = new Model3();
$model4 = new Model4();
$model5 = new Model5();
$model6 = new Model6();
// Inserts
$model1->insert( $data1 );
$model2->insert( $data2 );
$model3->insert( $data3 );
$model4->insert( $data4 );
$model5->insert( $data5 );
$model6->insert( $data6 );
// Commit the transaction
$db->commit();
}
Now, I don't like doing that either, because then it feels like my Controllers are becoming Model heavy and I'm doing all the model stuff like transactions in my controller?
I feel like I can't win? How should I do this?