user759235
user759235

Reputation: 2207

Add a prefix to values when using a yii\db\Query

Is there a way to add a prefix to values when using a DB query to get the data from a database? I have used a var as example to show you how I want it to be.

$query = (new \yii\db\Query())->select(['name' , 'product_image."$prefix"' ])->from('products');

Lets say that I want to add after every image path a prefix like _250x250, so the final output will be pathToImage_250x250 or uploads/pathToImage_250x250.

Upvotes: 0

Views: 306

Answers (1)

rob006
rob006

Reputation: 22174

You may use yii\db\Expression to create more advanced selects. For example for MySQL you may use CONCAT() function for this:

$query = (new \yii\db\Query())
    ->select([
        'name',
        'product_image' => \yii\db\Expression('CONCAT(product_image, :suffix)', [
            ':suffix' => '_250x250',
        ]),
    ])
    ->from('products');

Upvotes: 2

Related Questions