What's the purpose of constants with model files

simbolo

Well-known member
Looking through the model files in a lot there is declaring of const for the table that will be joined on later.
E.G. in /library/XenForo/Model/Thread.php
PHP:
class XenForo_Model_Thread extends XenForo_Model
{
    /**
    * Constants to allow joins to extra tables in certain queries
    *
    * @var integer Join user table
    * @var integer Join node table
    * @var integer Join post table
    * @var integer Join user table to fetch avatar info of first poster
    * @var integer Join forum table to fetch forum options
    */
    const FETCH_USER = 0x01;
    const FETCH_FORUM = 0x02;
    const FETCH_FIRSTPOST = 0x04;
    const FETCH_AVATAR = 0x08;
    const FETCH_DELETION_LOG = 0x10;
    const FETCH_FORUM_OPTIONS = 0x20;
 
>>>>>
 
}

Then later on before joining on that table there's a check to make sure the const is defined.
PHP:
if (!empty($fetchOptions['join']))
        {
            if ($fetchOptions['join'] & self::FETCH_USER)
            {
                $selectFields .= ',
                    user.*, IF(user.username IS NULL, thread.username, user.username) AS username';
                $joinTables .= '
                    LEFT JOIN xf_user AS user ON
                        (user.user_id = thread.user_id)';
            }
My question is: what is it's purpose?
Better put,
Why does it allow joins on other tables?
Is there a pattern that should be followed for naming and the value assigned to them?
 
That line actually doesn't check if it's defined. The single & is a bitwise operator, what that is doing is checking to see if that bit is set in $fetchOptions['join'] If it is that evaluates as true.

You can do some great stuff with bits at the programatic level. Figure you have 128 pieces of yes or no information. You can store all of those in a single field 128bits long in a database rather than having 128 different fields. When you are talking millions of records that data storage can add up.

Looking through the model files in a lot there is declaring of const for the table that will be joined on later.
E.G. in /library/XenForo/Model/Thread.php
PHP:
class XenForo_Model_Thread extends XenForo_Model
{
    /**
    * Constants to allow joins to extra tables in certain queries
    *
    * @var integer Join user table
    * @var integer Join node table
    * @var integer Join post table
    * @var integer Join user table to fetch avatar info of first poster
    * @var integer Join forum table to fetch forum options
    */
    const FETCH_USER = 0x01;
    const FETCH_FORUM = 0x02;
    const FETCH_FIRSTPOST = 0x04;
    const FETCH_AVATAR = 0x08;
    const FETCH_DELETION_LOG = 0x10;
    const FETCH_FORUM_OPTIONS = 0x20;
 
>>>>>
 
}

Then later on before joining on that table there's a check to make sure the const is defined.
PHP:
if (!empty($fetchOptions['join']))
        {
            if ($fetchOptions['join'] & self::FETCH_USER)
            {
                $selectFields .= ',
                    user.*, IF(user.username IS NULL, thread.username, user.username) AS username';
                $joinTables .= '
                    LEFT JOIN xf_user AS user ON
                        (user.user_id = thread.user_id)';
            }
My question is: what is it's purpose?
 
Okay I'm still a little confused on this D: so, say XenForo_Model_Post::FETCH_USER is set as the join value and gets sent. What are those constant values for and is the value they are set to have some type of significance?
 
It's just an human-readable way to manage flags stored in bits. Like the option for XenForo_Model_Thread::FETCH_DELETION_LOG is in the bit that represents "16". Technically if you didn't want to use "XenForo_Model_Thread::FETCH_DELETION_LOG", you could instead just use "16". But that's not as human readable.
 
It's just an human-readable way to manage flags stored in bits. Like the option for XenForo_Model_Thread::FETCH_DELETION_LOG is in the bit that represents "16". Technically if you didn't want to use "XenForo_Model_Thread::FETCH_DELETION_LOG", you could instead just use "16". But that's not as human readable.
I gotcha. I need to read up on this more.
 
Top Bottom