From 58c0c37b33fc4521065ad06b4222fde3e71468e2 Mon Sep 17 00:00:00 2001 From: Silvio Date: Tue, 31 Aug 2010 18:22:58 -0300 Subject: Changes into relations api --- lib/sfIsisImporterRelations.class.php | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) (limited to 'lib/sfIsisImporterRelations.class.php') 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); } } } -- cgit v1.2.3