Oh yes and per your original question .. the Criteria:BINARY_AND is a way to
tell your database to do a bitwise comparison. In SQL that would be & (not
&&) just like in PHP.

On Thu, Apr 30, 2009 at 7:45 AM, Gareth McCumskey <[email protected]>wrote:

> It uses an integer type, or tiny int if you will not be using many types.
> The bitwise operations don't need any specific type. If you want more info
> there is a nice little tutorial at
> http://www.litfuel.net/tutorials/bitwise.htm . That should explain better.
> Perhaps another small example here would help. This is directly from our
> code, from a batch script that does not use symfony to transfer
> classifications from the old "column per classification" schema of the old
> system mentioned in my blog post to the new schema using binary:
>
> $classification_value = 0;
>
> $classification_value = $get_batch_result->fields['isspam'] == 1 ?
> $classification_value + 1 : $classification_value;
>
> $classification_value = $get_batch_result->fields['ishighspam'] == 1 ?
> $classification_value + 2 : $classification_value;
>
> $classification_value = $get_batch_result->fields['issaspam'] == 1 ?
> $classification_value + 4 : $classification_value;
>
> $classification_value = $get_batch_result->fields['isrblspam'] == 1 ?
> $classification_value + 8 : $classification_value;
>
> $classification_value = $get_batch_result->fields['isfp'] == 1 ?
> $classification_value + 16 : $classification_value;
>
> $classification_value = $get_batch_result->fields['spamwhitelisted'] == 1 ?
> $classification_value + 32 : $classification_value;
>
> $classification_value = $get_batch_result->fields['spamblacklisted'] == 1 ?
> $classification_value + 64 : $classification_value;
>
>  $classification_value = $get_batch_result->fields['virusinfected'] == 1 ?
> $classification_value + 128 : $classification_value;
>
> $classification_value = $get_batch_result->fields['nameinfected'] == 1 ?
> $classification_value + 256 : $classification_value;
>
> $classification_value = $get_batch_result->fields['otherinfected'] == 1 ?
> $classification_value + 512 : $classification_value;
>
> $classification_value = $get_batch_result->fields['quarantined'] == 1 ?
> $classification_value + 1024 : $classification_value;
>
> The reason it works is because in PHP you can take a value from the
> classification and do a bitwise if:
>
> if ($classification_value & 512)
>
> So if the classification value had 512 (otherinfected) added onto it the
> result would be true. Another example is if I had a classification value of
> 768:
>
> if ($classification_value & 512)
>
> would still return true as well as:
>
> if ($classification_value & 256)
>
> and nothing else would because 512 + 256 = 768.
>
> Hope that helps a bit more
>
>
> On Thu, Apr 30, 2009 at 1:47 AM, Richtermeister <[email protected]> wrote:
>
>>
>> Hi Gareth,
>>
>> thanks for the explanation. The part about the 1, 2, 4, 8 step I get..
>> but to me those would still be integers.. albeit not continuous.
>> I guess I am just not familiar with the bit datatype and how it is
>> used.
>>
>> If you don't mind me asking, what database storage type does this use?
>>
>> Thanks,
>> Daniel
>>
>>
>> On Apr 29, 2:38 pm, Gareth McCumskey <[email protected]> wrote:
>> > If you used just integers then you wouldn't be able to work backwards
>> from
>> > the totalled number. For example:
>> >
>> > - is_low_socring = 1
>> > - is_high_scoring = 2
>> > - is_virus = 3
>> > - is_badcontent = 4
>> >
>> > If I had something that was just bad content it would be a 4. But If I
>> had
>> > something that was low_scoring and a virus it would also be 4... hmmm.
>> But
>> > if I use binary...
>> >
>> > - is_low_socring = 1
>> > - is_high_scoring = 2
>> > - is_virus = 4
>> > - is_badcontent = 8
>> >
>> > ONLY bad_content can be an 8, only low_scoring AND virus can be a 5 ..
>> etc
>> > etc.
>> >
>> > Hope that helps
>> >
>> >
>> >
>> > On Wed, Apr 29, 2009 at 6:01 PM, Richtermeister <[email protected]>
>> wrote:
>> >
>> > > Hi Gareth,
>> >
>> > > I just read your blog entry on that subject, and I have to admit that
>> > > it never occurred to me to use bits to store multiple flags in one
>> > > column. I like it! :) Thanks.
>> >
>> > > My question, if I may, is why the classification column is not just an
>> > > integer? What is the Criteria::BINARY_AND for?
>> >
>> > > Thanks for enlightening me :)
>> > > Have a great day.
>> > > Daniel
>> >
>> > > On Apr 29, 1:09 am, Gareth McCumskey <[email protected]> wrote:
>> > > > Hi all,
>> >
>> > > > I need the great minds that populate this liust to assist me with a
>> small
>> > > > dilema. We currently use an integer field that stores a range of
>> values
>> > > as a
>> > > > bitwise number depending on certain criteria of an item stored in
>> the db.
>> > > > What we want to do now is write a sql query like:
>> >
>> > > > SELECT [columns] FROM [tablename] WHERE classification & 1 AND
>> > > > classification & ~2
>> >
>> > > > I am struggling to find the negation (~) operator as part of the
>> criteria
>> > > > syntax. So far I have:
>> >
>> > > > $status_criterion =
>> $c->getNewCriterion(MailDetailsPeer::CLASSIFICATION,
>> > > 1,
>> > > > Criteria::BINARY_AND);
>> >
>> > >
>> $status_criterion->addAnd($c->getNewCriterion(MailDetailsPeer::CLASSIFICATION,
>> > > > 2 , Criteria::BINARY_AND));
>> >
>> > > > As you can see I have no negation on the second-line because I do
>> not
>> > > know
>> > > > how I should do that. There aren't any obvious options as part of
>> Propel.
>> >
>> > > > ANy help would be appreciated guys :)
>> >
>> > > > --
>> > > > Gareth McCumskeyhttp://garethmccumskey.blogspot.com
>> > > > twitter: @garethmcc
>> >
>> > --
>> > Gareth McCumskeyhttp://garethmccumskey.blogspot.com
>> > twitter: @garethmcc
>> >>
>>
>
>
> --
> Gareth McCumskey
>
> http://garethmccumskey.blogspot.com
> twitter: @garethmcc
>



-- 
Gareth McCumskey
http://garethmccumskey.blogspot.com
twitter: @garethmcc

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"symfony users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/symfony-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to