Alternative to... Bitwise operators in XF...?

Robust

Well-known member
I'm no PHP expert so I'm not really someone to be criticising if others are getting along with this fine, but now I think it's usual to use query builders to access the DB. I think Laravel is a perfect example of good approaches to modern PHP approaches.

http://laravel.com/docs/5.1/eloquent

It's much easier to work with that, and I can do what takes a lot of lines in XF (to do properly, anyway).

So, is there any other way in XF 1.5 to do what I'm looking to do (will describe below). I'm assuming (and hoping) that XF 2.0 will have significant improvements to developer workflow, reducing unnecessary work, using modern approaches and reducing boilerplate and repetition of code, but until then...

My aim is to access the same table to get something. So, say for action 1 I need to get just general info from the table like the title and description. In action 2, I need to get title, description, more data like user IDs etc. In action 3, I need to get the info in action 2, plus left join about 4-5 other tables in to fetch specific data from the unique IDs stored in the table I got the title and description from. In action 4 I need to get the data in action 2, but fetch the user too.

So really, it's just the case of in some actions/controller functions I need to get more data, others I need more joins to fetch data associative to the IDs that already exist.

Right now, I'd probably be expected to compare bitwise operators but bits are beyond something I know and really I don't want to bother work with them if they're outdated technologies that aren't really used anymore. There's probably more modern and nicer approaches to it. I mean, there's always check if a string is set and if it is do an action, but something that doesn't involve that action at all, possibly.

Thanks :)
 
As @Mike mentioned in a different thread about what's coming up for XF 2.0, there will be some sort of query builder developers can use:
  • While you can still write SQL directly, most data access is done through a builder object. The builder can control what related data is fetched, what conditions are applied (including against related data) and the order of the results. This can be done in any order.
  • We are not explicitly building on top of a particular framework. However, we are bringing in libraries to help with common tasks. This might be a component from Symfony and another one from Zend Framework and another from an unrelated project. It's mostly down to what we feel fits our needs.


Right now, I'd probably be expected to compare bitwise operators but bits are beyond something I know and really I don't want to bother work with them if they're outdated technologies that aren't really used anymore.

It's never a bad thing to learn computer science fundamentals such as bitwise operators, especially if you're a programmer They're definitely not "outdated technologies that aren't really used anymore".
 
While you can still write SQL directly, most data access is done through a builder object. The builder can control what related data is fetched, what conditions are applied (including against related data) and the order of the results. This can be done in any order.
Praise the lord.

It's never a bad thing to learn computer science fundamentals such as bitwise operators, especially if you're a programmer They're definitely not "outdated technologies that aren't really used anymore".
Yeah... I dunno. Maybe not completely useless, but I have no use for knowing bits in depth and bitwise operators with comparisons of bits, to be honest.
 
Yeah... I dunno. Maybe not completely useless, but I have no use for knowing bits in depth and bitwise operators with comparisons of bits, to be honest.

Quick dirty (but very simple) bitwise operator tutorial -- pretty much sums up anything you'll need to know with XenForo's models at least:

Say you have 4 constants:
PHP:
const JOIN_A = 0x01; // 001
const JOIN_B = 0x02; // 010
const JOIN_C = 0x04; // 100
const JOIN_D = 0x03; // 011 -- You really shouldn't use this as it could potentially cause issues.

When setting the joins you want to use you use the Or (|) operator which sets the bits in both the first and second variable so if you do:
PHP:
$join = self::JOIN_A | self::JOIN_B; // 011
$anotherJoin = self::JOIN_C| self::JOIN_D | self::JOIN_A; // 111

Then when doing the check you'd use the And (&) operator in an if statement so:
PHP:
if ($join & self::JOIN_C) // This should be 'false' because the '4' bit isn't set in $join, just the 1 and 2 bit
if ($anotherJoin & self::JOIN_B) // would return true because the 2 bit is set in $anotherJoin

For what XenForo's doing by default that's all you really need to know as far as bitwise operators come. That and of course a very basic understanding of at least how binary works :)
 
Quick dirty (but very simple) bitwise operator tutorial -- pretty much sums up anything you'll need to know with XenForo's models at least:

Say you have 4 constants:
PHP:
const JOIN_A = 0x01; // 001
const JOIN_B = 0x02; // 010
const JOIN_C = 0x04; // 100
const JOIN_D = 0x03; // 011 -- You really shouldn't use this as it could potentially cause issues.

When setting the joins you want to use you use the Or (|) operator which sets the bits in both the first and second variable so if you do:
PHP:
$join = self::JOIN_A | self::JOIN_B; // 011
$anotherJoin = self::JOIN_C| self::JOIN_D | self::JOIN_A; // 111

Then when doing the check you'd use the And (&) operator in an if statement so:
PHP:
if ($join & self::JOIN_C) // This should be 'false' because the '4' bit isn't set in $join, just the 1 and 2 bit
if ($anotherJoin & self::JOIN_B) // would return true because the 2 bit is set in $anotherJoin

For what XenForo's doing by default that's all you really need to know as far as bitwise operators come. That and of course a very basic understanding of at least how binary works :)
Thanks! So what about making them unique? Like 0x03 would apply to both 0x01 and 0x02 (bits are present). So for unique ones do I just double? So after 0x04 it'd be 0x08, 0x16, 0x32, etc?
 
@Jake B. What about this, too:

const FETCH_ASSOC = 0x02;
const FETCH_ASSOC_EXTENDED = 0x04;

I want fetch_assoc fetches associated data, and fetch_assoc_extended fetches extended assoc data. How can I have fetch_assoc_extended also include fetch_assoc?
 
Thanks! So what about making them unique? Like 0x03 would apply to both 0x01 and 0x02 (bits are present). So for unique ones do I just double? So after 0x04 it'd be 0x08, 0x16, 0x32, etc?

If you did 0x03 you would have 11 in binary so it'd cover both 0x01 and 0x02. For unique you'd use any base 2 number (Anything 2^x -- 2, 4, 8, 16, 32, etc. -- any number that has it's own bit)
 
Top Bottom