diff --git a/include/class-QueryUser.php b/include/class-QueryUser.php index c07de69..faaf8fd 100644 --- a/include/class-QueryUser.php +++ b/include/class-QueryUser.php @@ -1,32 +1,82 @@ . + /** - * Query an User + * Methods for a QueryUser class */ -class QueryUser extends Query { +trait QueryUserTrait { /** - * Constructor + * Where the User ID is... + * + * @param int $id User ID + * @return self */ - public function __construct() { - - // construct the Query parent class - parent::__construct(); + public function whereUserID( $id ) { + return $this->whereInt( $this->USER_ID, $id ); + } - // set SQL 'FROM' to desired table - $this->from( 'user' ); + /** + * Where the User is me + * + * @return self + */ + public function whereUserIsMe() { + return $this->whereUserID( get_user()->getUserID() ); + } - // set default class name for the results - $this->defaultClass( 'User' ); + /** + * Join a table with the User table + * + * @return + */ + public function joinUser( $type = 'INNER' ) { + return $this->joinOn( $type, 'user', 'user.user_ID', $this->USER_ID ); } +} + +/** + * Execute a Query against an User + */ +class QueryUser extends Query { + + use QueryUserTrait; + /** - * Where the User ID is... + * Univoque User ID column name * - * @param int User ID - * @return self + * @var string */ - public function whereUserID( $id ) { - return $this->whereInt( 'user_ID', $id ); + protected $USER_ID = 'user.user_ID'; + + /** + * Constructor + * + * @param object $db Database connection + * @param string $class_name Default class name for the results + */ + public function __construct( $db = null, $class_name = 'User' ) { + + // set the database connection and the default class name for the results + parent::__construct( $db, $class_name ); + + // set FROM table + $this->from( 'user' ); } }