Example #1
0
 public function importCategories($categories)
 {
     Ak::import('category');
     $CategoryInstance = new Category(array('init' => false));
     if ($this->db) {
         $CategoryInstance->setConnection($this->db);
     }
     $CategoryInstance->init();
     foreach ($categories as $category_name => $related) {
         if (!($Category = $CategoryInstance->findFirstBy('name', $category_name))) {
             $Category = new Category(array('init' => false));
             if ($this->db) {
                 $Category->setConnection($this->db);
             }
             $Category->init();
             $Category->setAttributes(array('name' => $category_name));
             if ($Category->save()) {
                 $this->log('Created new category: ' . $category_name);
             }
         }
         if (!empty($related['relations'])) {
             foreach ($related['relations'] as $related_category) {
                 if (!($RelatedCategory = $CategoryInstance->findFirstBy('name', $related_category))) {
                     $RelatedCategory = new Category(array('init' => false));
                     if ($this->db) {
                         $Category->setConnection($this->db);
                     }
                     $Category->init();
                     $Category->setAttributes(array('name' => $related_category));
                     $RelatedCategory->save();
                 }
                 $this->log('Relating category ' . $related_category . ' with ' . $category_name);
                 $Category->related_category->add($RelatedCategory);
             }
         }
     }
 }