In esoTalk, models do not follow the convention of being a direct representation of an object in the database. Rather, models simply provide methods to conveniently perform operations on data in the database.
All models extend the ETModel class which provides basic functions common to all types of data. Model classes specific to different types of data (such as ETConversationModel and ETMemberModel) provide additional functions to manipulate that data.
When extending ETModel, the parent::__construct
method should be called with the name of the table that the model operates on and optionally the table's primary key (defaults to [table]Id
.)
Defining A Model
class ConversationModel extends ETModel {
public function __construct()
{
parent::__construct("conversation", "conversationId");
}
}
There are models for most types of data included in esoTalk's core which provide a plethora of functionality. All of these can easily be instantiated using special static methods of the ET
class.
List Of Core Models
Class | Instance accessible via |
---|---|
ETActivityModel | ET::activityModel() |
ETChannelModel | ET::channelModel() |
ETConversationModel | ET::conversationModel() |
ETGroupModel | ET::groupModel() |
ETMemberModel | ET::memberModel() |
ETPostModel | ET::postModel() |
ETSearchModel | ET::searchModel() |
ETUpgradeModel | ET::upgradeModel() |
Below are a few examples of how these models may be used in esoTalk. This is by no means an exhaustive list of the functionality provided by the core models; more information can be found in the API Reference, and in specific examples in the Plugins documentation.
Updating A Member's Details
ET::memberModel()->update(array(
"username" => "Simon",
"email" => "simon@esotalk.org"
), array("memberId" => 1));
Getting A Conversation, Adding A Reply, And Checking For Errors
$model = ET::conversationModel();
$conversation = $model->getById(123);
$model->addReply($conversation, "This is the reply content");
if ($model->errorCount()) {
$errors = $model->errors();
} else {
echo "Success!";
}
Determine Whether Or Not The User Can Edit A Specific Post
$sql = ET::SQL()
->where("conversationId", 34)
->where("memberId", 1)
->limit(1);
$posts = ET::postModel()->getWithSQL($sql);
$post = $posts[0];
$conversation = ET::conversationModel()->getByPostId($post);
$canEdit = ET::postModel()->canEditPost($post, $conversation);