Habari\Vocabulary::set_object_terms PHP Метод

set_object_terms() публичный Метод

Sets the Term objects associated to that type of object with that id in this vocabulary
public set_object_terms ( String $object_type, integer $id, Array $terms = [] ) : boolean.
$object_type String the name of the object type
$id integer The id of the object for which you want the terms
$terms Array The names of the terms to associate
Результат boolean.
    public function set_object_terms($object_type, $id, $terms = array())
    {
        // Make sure we have an array
        $terms = new Terms($terms);
        $new_terms = array();
        // Make sure we have terms and they're in the database.
        // Key the terms to their id while we're at it.
        foreach ($terms as $term) {
            $new_term = $this->get_term($term);
            if (!$new_term instanceof Term) {
                $new_term = $this->add_term($term);
            }
            if ($new_term != false && !array_key_exists($new_term->id, $new_terms)) {
                $new_terms[$new_term->id] = $new_term;
            }
        }
        // Get the current terms
        $old_terms = $this->get_object_terms($object_type, $id);
        $keys = array_keys($new_terms);
        foreach ($old_terms as $term) {
            // If the old term isn't in the new terms, dissociate it from the object
            if (!in_array($term->id, $keys)) {
                $term->dissociate($object_type, $id);
            }
        }
        // Associate the new terms
        foreach ($new_terms as $term) {
            $term->associate($object_type, $id);
        }
        return true;
    }