The default (and only) locale for my site is en, however, when trying to query the ElementService from a console command without specifying the locale in the criteria, the locale used in the query is en_us. As such, the query returns no results. For example:

namespace Craft;

class ShoppingCommand extends BaseCommand {

    public function actionListOrders() {
        $c = craft()->elements->getCriteria(ElementType::Entry);
        $c->section = 'orders';
        $c->limit = 5;
        foreach($c as $o) {
            print $o->id . ": " . $o->title . "\n";

prints nothing, even though there are entries present for the default locale en.

Printing out the query object print_r(craft()->elements->buildElementsQuery($c)); shows the :locale parameter being passed to the sql query as en_us. But this locale doesn't even exist for the site (select * from craft_locales shows only en).

The only way I could find to work around this is by adding the site's default locale to the criteria manually with:

$c->locale = craft()->i18n->getPrimarySiteLocale();

However, since the query works find without this when run in a controller, I'm skeptical that it should be needed. Is this is a bug, or is there some extra configuration i'm missing? Having some parameter to specify the locale to use for commands would make sense in mutli-locale environments, but I would still expect the default to be the site's default rather than en_us.


Turns out this is actually a bug, too.

It's going to end up being more involved to fix than a build release probably warrants, though, so it'll have to wait for the upcoming 2.3 release to get a proper fix.

In the meantime, you'll be able to get by with your workaround of manually setting it.

$c->locale = craft()->i18n->getPrimarySiteLocale();

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.