/**
  * Removes the relation to the specified term taxonomy, and maintains the
  * data integrity of the term taxonomy provided
  * @param EE_Term_Taxonomy $term_taxonomy
  * @return EE_Base_Class the relation was removed from
  */
 function remove_relation_to_term_taxonomy($term_taxonomy)
 {
     if (!$term_taxonomy) {
         EE_Error::add_error(sprintf(__("No Term_Taxonomy provided which to remove from model object of type %s and id %d", "event_espresso"), get_class($this), $this->ID()), __FILE__, __FUNCTION__, __LINE__);
         return NULL;
     }
     $term_taxonomy->set_count($term_taxonomy->count() - 1);
     $term_taxonomy->save();
     return $this->_remove_relation_to($term_taxonomy, 'Term_Taxonomy');
 }
 public function test_insert_and_update_and_delete()
 {
     $term1 = EE_Term::new_instance(array('name' => 'monkey1', 'slug' => 'monkey1'));
     $term2 = EE_Term::new_instance(array('name' => 'monkey2', 'slug' => 'monkey2'));
     $term1->save();
     $term2->save();
     $tt_1 = EE_Term_Taxonomy::new_instance(array('taxonomy' => 'whatever', 'term_id' => $term1->ID()));
     $tt_1->save();
     $tt_2 = EE_Term_Taxonomy::new_instance(array('taxonomy' => 'whatever', 'term_id' => $term2->ID()));
     $tt_2->save();
     $e = EE_Event::new_instance(array('EVT_name' => 'for_term_1'));
     $e->save();
     //ok done setup
     //test INSERT
     $this->assertEquals(0, $tt_1->count());
     $new_tr_id = EEM_Term_Relationship::instance()->insert(array('term_taxonomy_id' => $tt_1->ID(), 'object_id' => $e->ID()));
     $this->assertNotNull($new_tr_id);
     //refresh out term_taxonomy objects, as the database has changed
     $tt_1 = EEM_Term_Taxonomy::reset()->get_one_by_ID($tt_1->ID());
     $tt_2 = EEM_Term_Taxonomy::instance()->get_one_by_ID($tt_2->ID());
     $this->assertEquals(1, $tt_1->count());
     $this->assertEquals(0, $tt_2->count());
     //test UPDATE... except we can't update term_relationship because there's no Primary Key
     //on it. This should be fixed at some point
     //@todo: fix this test
     //		$updated = EEM_Term_Relationship::instance()->update_by_ID(array('term_taxonomy_id'=>$tt_2->ID() ), $new_tr_id );
     //		//refresh out term_taxonomy objects, as the database has changed
     //		$tt_1 = EEM_Term_Taxonomy::reset()->get_one_by_ID( $tt_1->ID() );
     //		$tt_2 = EEM_Term_Taxonomy::instance()->get_one_by_ID($tt_2->ID() );
     //		$this->assertEquals( 0, $tt_1->count() );
     //		$this->assertEquals(1,$tt_2->count() );
     //test DELETE
     //@todo: fix this test too. see above
     //		$count_deleted = EEM_Term_Relationship::instance()->delete_by_ID($new_tr_id);
     //		$this->assertNotEmpty( $count_deleted );
     //		//refresh out term_taxonomy objects, as the database has changed
     //		$tt_1 = EEM_Term_Taxonomy::reset()->get_one_by_ID( $tt_1->ID() );
     //		$tt_2 = EEM_Term_Taxonomy::instance()->get_one_by_ID($tt_2->ID() );
     //		$this->assertEquals( 0, $tt_1->count() );
     //		$this->assertEquals(0,$tt_2->count() );
 }
 /**
  * Adds an event category with the specified name and description to the specified
  * $cpt_model_object. Intelligently adds a term if necessary, and adds a term_taxonomy if necessary,
  * and adds an entry in the term_relationship if necessary.
  * @param EE_CPT_Base $cpt_model_object
  * @param string $category_name (used to derive the term slug too)
  * @param string $category_description
  * @param int $parent_term_taxonomy_id
  * @return EE_Term_Taxonomy
  */
 function add_event_category(EE_CPT_Base $cpt_model_object, $category_name, $category_description = '', $parent_term_taxonomy_id = null)
 {
     //create term
     require_once EE_MODELS . 'EEM_Term.model.php';
     //first, check for a term by the same name or slug
     $category_slug = sanitize_title($category_name);
     $term = EEM_Term::instance()->get_one(array(array('OR' => array('name' => $category_name, 'slug' => $category_slug))));
     if (!$term) {
         $term = EE_Term::new_instance(array('name' => $category_name, 'slug' => $category_slug));
         $term->save();
     }
     //make sure there's a term-taxonomy entry too
     require_once EE_MODELS . 'EEM_Term_Taxonomy.model.php';
     $term_taxonomy = EEM_Term_Taxonomy::instance()->get_one(array(array('term_id' => $term->ID(), 'taxonomy' => EE_Event_Category_Taxonomy)));
     /** @var $term_taxonomy EE_Term_Taxonomy */
     if (!$term_taxonomy) {
         $term_taxonomy = EE_Term_Taxonomy::new_instance(array('term_id' => $term->ID(), 'taxonomy' => EE_Event_Category_Taxonomy, 'description' => $category_description, 'count' => 1, 'parent' => $parent_term_taxonomy_id));
         $term_taxonomy->save();
     } else {
         $term_taxonomy->set_count($term_taxonomy->count() + 1);
         $term_taxonomy->save();
     }
     return $this->add_relationship_to($cpt_model_object, $term_taxonomy, 'Term_Taxonomy');
 }