abstract \Groundhogg\Base_Object

📅 Published on

📝 Last updated

Documentation

»

Classes

»

abstract \Groundhogg\Base_Object

implements SerializableArrayAccessJsonSerializable

The abstract foundation class for every Groundhogg data object. All objects — contacts, emails, funnels, tags, events, and more — extend this class. It handles creating, reading, updating, and deleting database records, as well as relationships between objects, notes, and serialisation.

Methods

MethodReturnsDescription
get_id()intReturns the object’s primary key ID.
exists()boolReturns true if the record exists in the database.
create( $data = [] )static|falseInserts a new record. Returns the object on success or false on failure.
update( $data = [] )boolUpdates the database record with the given data.
delete()boolDeletes the record from the database.
duplicate( $overrides = [] )static|falseCreates a copy of the object with optional field overrides.
merge( $other )boolMerges another object of the same type into this one and deletes the other.
pull()boolRe-fetches the object’s data from the database, refreshing any cached values.
get_data()arrayReturns the raw database row as an associative array.
get_as_array()arrayReturns a serialisable representation of the object for use in REST responses or exports.
admin_link()stringReturns the URL to this object’s edit screen in the WordPress admin.
add_note( $note, $context = 'system', $user_id = false, $overrides = [] )Note|falseAdds a note to this object.
get_related_objects( $other_type = false, $is_parent = true )arrayReturns related objects of a given type from the object relationships table.
create_relationship( $other, $is_parent = true )boolCreates a relationship between this object and another.
delete_relationship( $other, $is_parent = true )boolRemoves a relationship between this object and another.

Usage

Here’s a pseudo code example for a hypothetical extension that works with any Groundhogg object.

<?php

// All Groundhogg objects share this interface
$contact = \Groundhogg\get_contactdata( 'jane@example.com' );
$email   = new \Groundhogg\Email( 42 );
$funnel  = new \Groundhogg\Funnel( 7 );

// Common Base_Object methods work on all of them
if ( $contact->exists() ) {
    $contact->update( [ 'first_name' => 'Jane' ] );
    $contact->add_note( 'Updated via my plugin.' );
}

// Create a relationship between a contact and a campaign
$campaign = new \Groundhogg\Campaign( 3 );
$contact->create_relationship( $campaign );

// Duplicate an email template
$copy = $email->duplicate( [ 'title' => 'Copy of ' . $email->get_title() ] );

Was this helpful?

Let us know if this document answered your question. That’s the only way we can improve.