1

it is possible to do like this:

$users = Entry::find()
  ->userLevel('>3')
  ->or group('admin')

So in database level,the sql will go like this:

select * from users
  where users.userLevel > 3 or users.group='admin'

Many many thanks!!

1 Answer 1

1

Yes but not like you tried it. You have to work on Yii2 level and not with Crafts Query

// prefix for content column tables
// if you want to select fields in the field layout do
// $prefix . 'fieldHandle'
$prefix = Craft::$app->getContent()->contentTable . '.' . Craft::$app->getFields()->oldFieldColumnPrefix; 

$user = User::find()
    ->andWhere(
        new OrCondition(
            ['>','users.userLevel', 3],
            ['=', 'users.group', 'admin'] 
        )
    )
    ->all();

However: keep in mind this SQL will likely produce errors because there is neither a users.group column nor a users.userLevel column in the database

Just as an example for more complex search:

$prefix = Craft::$app->getContent()->contentTable . '.' . Craft::$app->getFields()->oldFieldColumnPrefix;
$criteria = [
    'where'     => new OrCondition(
        [
            [
                'between',
                $prefix . 'startDate',
                Db::prepareDateForDb($startDateDb),
                Db::prepareDateForDb($endDate)
            ],
            new AndCondition(
                [

                    ['=', $prefix . 'recurring', 1],
                    ['<=', $prefix . 'startDate', $startDateDb->format('Y-m-d H:i:s')]
                ]
            )
        ]
    ),
    'orderBy'   => ['startDate' => SORT_ASC],
    'sectionId' => 10,
];

$query = Entry::find();
\Craft::configure($query, $criteria);
2
  • Thank you. But I know nothing about Yii, so in this way, how can I use relatedTo function?
    – yu xia
    Commented May 3, 2018 at 22:04
  • You have to join the relation table and check if a value exists with your criteria. You can perform a simple query to see what Craft does. Entry::find()->relatedTo(1)->getRawSql() Commented May 3, 2018 at 23:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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