diff options
author | Silvio <s1lv10@uol.com.br> | 2010-08-31 18:22:58 -0300 |
---|---|---|
committer | Silvio <s1lv10@uol.com.br> | 2010-08-31 18:22:58 -0300 |
commit | 58c0c37b33fc4521065ad06b4222fde3e71468e2 (patch) | |
tree | aa8e3d090cbe0c19f641954e3d754fddc62ff4c1 | |
parent | 81cd263f4598115cc90817300f69a869fa5e444a (diff) | |
download | sf_isis_importer_plugin-58c0c37b33fc4521065ad06b4222fde3e71468e2.tar.gz sf_isis_importer_plugin-58c0c37b33fc4521065ad06b4222fde3e71468e2.tar.bz2 |
Changes into relations api
-rw-r--r-- | lib/sfIsisImporterEntities.class.php | 26 | ||||
-rw-r--r-- | lib/sfIsisImporterRelations.class.php | 33 |
2 files changed, 46 insertions, 13 deletions
diff --git a/lib/sfIsisImporterEntities.class.php b/lib/sfIsisImporterEntities.class.php index 56c2883..cd66afd 100644 --- a/lib/sfIsisImporterEntities.class.php +++ b/lib/sfIsisImporterEntities.class.php @@ -126,25 +126,41 @@ class sfIsisImporterEntities extends IsisConnector { * * @param string $entity Entity name * @param string $name Name value + * @param string $key Value key * @return object Entity data */ - public function addEntity($entity, $name) + public function addEntity($entity, $name, $key = 'name') { $name = $this->entityName($name); - $data = $this->getEntity($entity, $name); + $data = $this->getEntity($entity, $name, $key); if (!$data) { $this->log("Adding new $entity $name."); - $data = new $entity(); - $data->name = $name; - $data->save(); + $data = $this->newEntity($entity, $name, $key); } return $data; } /** + * Add a new entity into the database. + * + * @param string $entity Entity name + * @param string $name Name value + * @param string $key Value key + * @return object Entity data + */ + public function newEntity($entity, $name, $key = 'name') + { + $data = new $entity(); + $data->{$key} = $name; + $data->save(); + + return $data; + } + + /** * Add an element into the database if needed, returning * the resulting object. * diff --git a/lib/sfIsisImporterRelations.class.php b/lib/sfIsisImporterRelations.class.php index 8282845..6912305 100644 --- a/lib/sfIsisImporterRelations.class.php +++ b/lib/sfIsisImporterRelations.class.php @@ -61,15 +61,30 @@ class sfIsisImporterRelations extends sfIsisImporterEntities { } /** - * Import a single many to many data. + * Import a single value to a many to many data model. * * @param object $model Model * @param mixed $values Values to be added * @param string $relation Relation name + * @param string $key Value key */ - public function addManyToMany(&$model, $value, $relation) + public function addManyToMany(&$model, $value, $relation, $key = 'name') { - $method = 'add'. $relation; + return $this->addToSharedModel($model, $value, $relation, $key, 'add'); + } + + /** + * Import a single value to a shared data model. + * + * @param object $model Model + * @param mixed $values Values to be added + * @param string $relation Relation name + * @param string $key Value key + * @param string $strategy Strategy do add the content into the database + */ + public function addToSharedModel(&$model, $value, $relation, $key = 'name', $strategy = 'add') + { + $method = $strategy . $relation; // Populate related data. if (is_callable(array($this, $method))) @@ -78,7 +93,7 @@ class sfIsisImporterRelations extends sfIsisImporterEntities { } else { - $data = $this->addEntity($relation, $value); + $data = $this->{$strategy .'Entity'}($relation, $value, $key); } // Get model and relation names and id fields. @@ -101,12 +116,13 @@ class sfIsisImporterRelations extends sfIsisImporterEntities { * @param object $model Model * @param array $values Values to be added * @param string $relation Relation name + * @param string $key Value key */ - public function addManyToManyEntities(&$model, array $values, $relation) + public function addManyToManyEntities(&$model, array $values, $relation, $key = 'name') { foreach ($values as $value) { - $this->addManyToMany($model, $value, $relation); + $this->addManyToMany($model, $value, $relation, $key); } } @@ -116,12 +132,13 @@ class sfIsisImporterRelations extends sfIsisImporterEntities { * @param object $model Model * @param array $field Field data from ISIS database schema * @param string $relation Relation name + * @param string $key Value key */ - public function addManyToManyMain(&$model, array $field, $relation) + public function addManyToManyMain(&$model, array $field, $relation, $key = 'name') { foreach (new IsisMainItemIterator($this, $field) as $value) { - $this->addManyToManyEntities($model, $this->explodeBrackets($value), $relation); + $this->addManyToManyEntities($model, $this->explodeBrackets($value), $relation, $key); } } } |