aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSilvio <s1lv10@uol.com.br>2010-07-29 11:16:30 -0300
committerSilvio <s1lv10@uol.com.br>2010-07-29 11:16:30 -0300
commit278dae3cc11fbccc7a9bfe7b67157a1f1085e48c (patch)
treec23f15777d4ec846ec8c5326db51401c54ea93a9
parent418f11c99dda69b31ee2fed5b441c4cdb9df6bef (diff)
downloadsf_isis_importer_plugin-278dae3cc11fbccc7a9bfe7b67157a1f1085e48c.tar.gz
sf_isis_importer_plugin-278dae3cc11fbccc7a9bfe7b67157a1f1085e48c.tar.bz2
Check if content already exist if a primary key is specified
-rw-r--r--lib/sfIsisImporter.class.php42
1 files changed, 34 insertions, 8 deletions
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;
}
/**