1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

What's the purpose of constants with model files

Discussion in 'XenForo Development Discussions' started by simbolo, Mar 5, 2012.

  1. simbolo

    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
    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;
    FETCH_FORUM 0x02;
    FETCH_AVATAR 0x08;
    Then later on before joining on that table there's a check to make sure the const is defined.
    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?
  2. bambua

    bambua Well-Known Member

    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.

    ragtek and simbolo like this.
  3. simbolo

    simbolo Well-Known Member

    Ahh, glossed over the single &. Makes perfect sense. Thanks for the prompt response!
  4. silence

    silence Well-Known Member

    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?
  5. digitalpoint

    digitalpoint Well-Known Member

    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.
    silence likes this.
  6. silence

    silence Well-Known Member

    I gotcha. I need to read up on this more.

Share This Page