1

Can anyone tell me why this is returning all entries instead of entries with a creditsUsed field > 0? creditsUsed is a number field that I created for this entry type.

$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->authorId = craft()->userSession->getUser()->id;
$criteria->section = 'news';
$criteria->creditsUsed > 0;
$criteria->status = null;
$entries = $criteria->total();
3

Here's what's actually happening with this line of code...

$criteria->creditsUsed > 0;

You are actually generating the boolean value of "is creditsUsed more than zero". Granted, you aren't setting it to an actual variable, but nonetheless you are generating a boolean value. For example:

$moreThanZeroCreditsUsed = ($criteria->creditsUsed > 0);

Even though you're not assigning that new boolean value to a variable, it's still valid PHP (and therefore no error is thrown).

What it looks like you are trying to do instead is to actually set the value of creditsUsed, which you'd need to use an equals sign to do:

$criteria->creditsUsed = "whatever";

If my memory serves me correctly, the way to denote a comparison on an ElementCriteriaModel is to simply pass the comparison as a string:

$criteria->creditsUsed = "> 0";

See the last example in this subsection... Granted, it's being applied via Twig syntax, but it's still modifying the ElementCriteriaModel in the same way:

craft.entries.width('>= 100')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.