Railsbin – The vulnerable pastebin service!

User:

Title: Test

Content:

<?php
/**
 * This trait adds utilities for a commented object.
 * 
 * To make an object commented, follow these steps:
 * 
 * 1. Add a new DBTable/GDO extending GWF_CommentsTable
 *    This table has to return the commented object table in gdoCommentObjectTable()
 *    
 * 2. Add this trait to your commented object.
 *    The commented object has to return your new DBTable in gdoCommentTable()
 *     
 * Your object is than able to easily add comments to the GWF_Comment table, joined via your new GWF_CommentsTable table.
 * All relations are foreign key safe, as which is the case in whole gwf5.
 *     
 * @author gizmore
 * @since 5.0
 * @see GWF_Comment
 * @see GWF_CommentTable 
 */
trait GWF_CommentedObject
{
    /**
     * Get the number of comments
     * @return number
     */
	public function gdoCommentCount()
	{
		return $this->queryCountComments();
	}
	
	/**
	 * Query the number of comments.
	 * @return int
	 */
	public function queryCountComments()
	{
		$commentTable = $this->gdoCommentTable();
		$commentTable instanceof GWF_CommentTable;
		return $commentTable->countWhere('comment_object='.$this->getID());
	}
	
	/**
	 * Build query for all comments.
	 * @return GDOQuery
	 */
	public function queryComments()
	{
		$comments = GWF_Comment::table();
		$commentTable = $this->gdoCommentTable();
		$commentTable instanceof GWF_CommentTable;
		return $commentTable->select('gwf_comment.*')->fetchTable(GWF_Comment::table())->joinObject('comment_id')->where("comment_object=".$this->getID());
	}
	
	/**
	 * @param GWF_User $user
	 * @return GDOQuery
	 */
	public function queryUserComment(GWF_User $user=null)
	{
	    $user = $user || GWF_User::current();
	    return $this->queryComments()->where("comment_user={$user->getID()}");
	}
	
}

Edit | Back