Hasone - cant return single column

public function getbrand() {
        $model = $this->hasOne(Brand::className(), ['id' => 'brand_id'])->select('brand_name');
}

public function view($id) {
        $model = LinkConfiguration::find()
                ->joinWith('brand')
                ->asArray()
                ->one();
        echo "<pre>";
        print_r($model);
        exit;
    }

It gives the following output:

Array
(
    [id] => 1
    [brand_id] => 2
    [client_id] => 5551
    [client_secret] => fsdfsdfdsf
    [created_on] => 2021-03-15 18:01:27
    [updated_on] => 
    [status] => 0
    [brand] => Array
        (
            [brand_name] => samples
        )
)

Expected output:

Array
(
    [id] => 1
    [brand_id] => 2
    [client_id] => 5551
    [client_secret] => fsdfsdfdsf
    [created_on] => 2021-03-15 18:01:27
    [updated_on] => 
    [status] => 0
  [brand_name] => samples
)

Pls help me to solve this

Hi @Preethi_vu,

HasOne relation is not meant to be used to fetch a single column. It returns a query to fetch a related model as a whole. It’s by design.

public function getBrand()
{
    return $this->hasOne(Brand::className(), ['id' => 'brand_id']);
}
public function view($id)
    $model = LinkConfiguration::findOne($id);
    echo "<pre>";
    print_r($model);
    exit;
1 Like

Thank you for the reply…