user759235
user759235

Reputation: 2207

Yii2, add extra dummy fields to the DB query

So i'm trying to add some dummy key/values to an DB query, as these keys are not present in the table and i dont want to alter the array it self I was hoping that there is an easy way to do this right at the start so when im building the array from the DB.

The example belows shows that i'm trying to add the extra fields called 'type' & 'tax' with the values next to them.

The example below is sadly not working so is there a way to add extra fields with the same values to every row?

$query = (new \yii\db\Query())
->select(['id' , 'name' , 'price' , 'type' => 'car' , 'tax' => 'full' ])
->from('products' )

Upvotes: 4

Views: 1350

Answers (1)

Yerke
Yerke

Reputation: 2235

If you used ActiveRecord you could just add some variables with default values inside you class.

class User extends ActiveRecord
{
    public $example = 'value';
    ...

However if we are talking about taking some dummy key => value directly from SQL, then this way should be working:

use yii\db\Expression;
...

$query = (new \yii\db\Query())
    ->select(['id' , 'name' , 'price' , new Expression("'car' AS type") , new Expression("'full' AS tax")])
    ->from('products' );

Upvotes: 5

Related Questions