ElggMetadata::save PHP Method

save() public method

Save metadata object
public save ( ) : integer | boolean
return integer | boolean the metadata object id or true if updated
    public function save()
    {
        if ($this->id > 0) {
            return update_metadata($this->id, $this->name, $this->value, $this->value_type, $this->owner_guid, $this->access_id);
        } else {
            // using create_metadata() for deprecation notices in 2.x
            $this->id = create_metadata($this->entity_guid, $this->name, $this->value, $this->value_type, $this->owner_guid, $this->access_id);
            if (!$this->id) {
                throw new \IOException("Unable to save new " . get_class());
            }
            return $this->id;
        }
    }

Usage Example

コード例 #1
0
ファイル: ElggMetadataTest.php プロジェクト: elgg/elgg
 public function testCanSaveMetadata()
 {
     $owner = $this->mocks()->getUser();
     _elgg_services()->session->setLoggedInUser($owner);
     $object = $this->mocks()->getObject(['owner_guid' => $owner->guid]);
     $metadata = new ElggMetadata();
     $metadata->entity_guid = $object->guid;
     $metadata->name = 'foo';
     $metadata->value = 'bar';
     $metadata->time_created = _elgg_services()->metadataTable->getCurrentTime()->getTimestamp();
     $id = _elgg_services()->metadataTable->iterator + 1;
     // Insert
     $dbprefix = elgg_get_config('dbprefix');
     $sql = "INSERT INTO {$dbprefix}metadata\n\t\t\t\t(entity_guid, name, value, value_type, owner_guid, time_created, access_id)\n\t\t\t\tVALUES (:entity_guid, :name, :value, :value_type, :owner_guid, :time_created, :access_id)";
     _elgg_services()->db->addQuerySpec(['sql' => $sql, 'params' => [':entity_guid' => $metadata->entity_guid, ':name' => 'foo', ':value' => 'bar', ':value_type' => 'text', ':owner_guid' => $metadata->owner_guid, ':time_created' => $metadata->time_created, ':access_id' => $metadata->access_id], 'insert_id' => $id]);
     $this->assertEquals($id, $metadata->save());
     _elgg_services()->session->removeLoggedInUser();
 }