From 278dae3cc11fbccc7a9bfe7b67157a1f1085e48c Mon Sep 17 00:00:00 2001 From: Silvio Date: Thu, 29 Jul 2010 11:16:30 -0300 Subject: Check if content already exist if a primary key is specified --- lib/sfIsisImporter.class.php | 42 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-) (limited to 'lib/sfIsisImporter.class.php') diff --git a/lib/sfIsisImporter.class.php b/lib/sfIsisImporter.class.php index 84e4807..159fa8d 100644 --- a/lib/sfIsisImporter.class.php +++ b/lib/sfIsisImporter.class.php @@ -50,23 +50,49 @@ class sfIsisImporter extends sfIsisImporterBase * * @param string $base_model Model to use * @param int $entry Entry number - * @todo Check if content already exist if a primary key is specified */ public function addEntry($base_model, $entry) { // Get data and setup the model. - $this->log("Importing $base_model $entry..."); $this->read($entry); + $model = $this->newModel($base_model, $entry); + + if ($model) + { + $this->log("Importing $base_model $entry..."); + + // Dispatch to custom import procedures. + foreach (new IsisMethodIterator($this) as $method => $field) + { + $this->{$method}($model, $field); + } + + $model->save(); + } + else { + $this->log("Skipping existing entry $entry for $base_model."); + } + } + + /** + * Create a new model just if doesn't exist for a given entry. + * + * @param string $base_model Model to use + * @param int $entry Entry number + * @return mixed New model with assigned id or false + */ + public function newModel($base_model, $entry) + { $model = new $base_model(); - $this->setBaseModelId($model); + $id = $this->getBaseModelId($model); - // Dispatch to custom import procedures. - foreach (new IsisMethodIterator($this) as $method => $field) + if ($id && !call_user_func(array($base_model, 'getById'), $id)) { - $this->{$method}($model, $field); - } + $this->setBaseModelId($model); + return $model; + } - $model->save(); + return false; } /** -- cgit v1.2.3