Yii2 правое объединение, возвращающее только 2 столбца

Работа над API, и у меня есть этот запрос:

$query = CallerIdentity::findNonGeo()->join( 'RIGHT JOIN', 'ehadata', 'caller_ids.cidref = ehadata.cidref')
            ->select('ehadata.*')
            ->where(['caller_ids.cidref' => $id]);

        echo $query->getSQL();die;

Вывод функции ->getSQL():

SELECT `ehadata`.* FROM `caller_ids` RIGHT JOIN `ehadata` ON caller_ids.cidref = ehadata.cidref WHERE `caller_ids`.`cidref`='256'

и результат выполнения запроса через функцию в почтальоне возвращает:

{
"cidref": 256,
"status": 0
}

Однако, когда я запускаю запрос в adminer, результат:

Результат от администратора

У кого-нибудь есть идеи, почему это может быть? Я хорошо и действительно смущен. Любая помощь очень ценится.

Модель EhaData

class EHAData extends \yii\db\ActiveRecord
{
/**
 * @inheritdoc
 */
public static function tableName()
{
    return 'ehadata';
}

/**
 * @inheritdoc
 */
public function rules()
{
    return [
        [['cidref', 'status', 'name', 'premesis', 'thoroughfare', 'locality', 'postcode'], 'required'],
        [['cidref', 'status'], 'integer'],
        [['modified'], 'safe'],
        [['title', 'forename', 'postcode'], 'string', 'max' => 20],
        [['name', 'bussuffix'], 'string', 'max' => 50],
        [['premesis'], 'string', 'max' => 60],
        [['thoroughfare'], 'string', 'max' => 55],
        [['locality'], 'string', 'max' => 30],
        [['cidref'], 'exist', 'skipOnError' => true, 'targetClass' => CallerIdentity::className(), 'targetAttribute' => ['cidref' => 'cidref']],
    ];
}

/**
 * @inheritdoc
 */
public function attributeLabels()
{
    return [
        'ehaid' => 'Ehaid',
        'cidref' => 'Cidref',
        'status' => 'Status',
        'title' => 'Title',
        'forename' => 'Forename',
        'name' => 'Name',
        'bussuffix' => 'Bussuffix',
        'premesis' => 'Premesis',
        'thoroughfare' => 'Thoroughfare',
        'locality' => 'Locality',
        'postcode' => 'Postcode',
        'modified' => 'Modified',
    ];
}

/**
 * @return \yii\db\ActiveQuery
 */
public function getCallerIdentity()
{
    return $this->hasOne(CallerIdentity::className(), ['cidref' => 'cidref']);
}
}

Модель идентификации абонента

class CallerIdentity extends \yii\db\ActiveRecord
{

/**
 * @inheritdoc
 */
public static function tableName()
{
    return 'caller_ids';
}

/**
 * @inheritdoc
 */
public function rules()
{
    return [
        [['custref', 'caller_id', 'start_date'], 'required'],
        [['custref', 'status', 'target_index'], 'integer'],
        [['type', 'conf_call', 'magrathea', 'outbound_only', 'callrec_outbound', 'callrec_inbound'], 'string'],
        [['start_date', 'last_polled', 'last_modified', 'created'], 'safe'],
        [['caller_id'], 'string', 'max' => 15],
        [['location', 'destination', 'redirect'], 'string', 'max' => 120],
        [['country_code'], 'string', 'max' => 3],
        [['details'], 'string', 'max' => 180],
        [['expiry'], 'string', 'max' => 18],
        [['custref'], 'exist', 'skipOnError' => true, 'targetClass' => Customer::className(), 'targetAttribute' => ['custref' => 'custref']],
    ];
}

/**
 * @inheritdoc
 */
public function attributeLabels()
{
    return [
        'cidref' => 'Cidref',
        'custref' => 'Custref',
        'caller_id' => 'Caller ID',
        'location' => 'Location',
        'destination' => 'Destination',
        'redirect' => 'Redirect',
        'type' => 'Type',
        'conf_call' => 'Conf Call',
        'magrathea' => 'Magrathea',
        'outbound_only' => 'Outbound Only',
        'callrec_outbound' => 'Callrec Outbound',
        'callrec_inbound' => 'Callrec Inbound',
        'country_code' => 'Country Code',
        'details' => 'Details',
        'status' => 'Status',
        'start_date' => 'Start Date',
        'expiry' => 'Expiry',
        'target_index' => 'Target Index',
        'last_polled' => 'Last Polled',
        'last_modified' => 'Last Modified',
        'created' => 'Created',
    ];
}

/**
 * @return \yii\db\ActiveQuery
 */
public function getCustref()
{
    return $this->hasOne(Customer::className(), ['custref' => 'custref']);
}

/**
 * @return \yii\db\ActiveQuery
 */
public function getEhadata()
{
    return $this->hasMany(EHAData::className(), ['cidref' => 'cidref']);
}

/**
 * @inheritdoc
 * @return BanQuery the active query used by this AR class.
 */
public static function find()
{
    $query = new CallerIdentityQuery(get_called_class());

    $query->select([
        'caller_ids.cidref','custref', 'caller_id', 'expiry', 'conf_call',
        'type', 'redirect', 'destination', 'caller_ids.status', 'start_date',
        'statusDesc' => new \yii\db\Expression("CASE caller_ids.status 
            WHEN 0 THEN 'Deactivated' 
            WHEN 1 THEN 'Active' 
            WHEN 2 THEN 'Inactive' 
            WHEN 3 THEN 'Unregistered'
            ELSE 'Permanently Deleted' END")])
        ->with('ehadata')->asArray()->all();


    return $query;
}
public static function findNonGeo()
{
    $query = new CallerIdentityQuery(get_called_class());
    $query->select([
        'cidref', 'custref', 'caller_id', 'expiry', 'conf_call',
        'type', 'redirect', 'destination', 'status', 'start_date',
        'statusDesc' => new \yii\db\Expression("CASE status 
            WHEN 0 THEN 'Deactivated' 
            WHEN 1 THEN 'Active' 
            WHEN 2 THEN 'Inactive' 
            WHEN 3 THEN 'Unregistered'
            ELSE 'Permanently Deleted' END")])
        ->where(['caller_ids.status' => '1'])
        ->andWhere(['type' => 'N']);
    return $query;
}
public static function findFax()
{
    $query = new CallerIdentityQuery(get_called_class());

    $query->select([
        'cidref', 'custref','caller_id', 'expiry', 'conf_call',
        'type', 'redirect', 'destination', 'status', 'start_date',
        'statusDesc' => new \yii\db\Expression("CASE status 
            WHEN 0 THEN 'Deactivated' 
            WHEN 1 THEN 'Active' 
            WHEN 2 THEN 'Inactive' 
            WHEN 3 THEN 'Unregistered'
            ELSE 'Permanently Deleted' END")])
        ->where(['status' => '1'])
        ->andWhere(['type' => 'F']);
    return $query;
}
public static function findConference()
{
    $query = new CallerIdentityQuery(get_called_class());

    $query->select([
        'cidref', 'custref', 'caller_id', 'expiry', 'conf_call',
        'type', 'redirect', 'destination', 'status', 'start_date',
        'statusDesc' => new \yii\db\Expression("CASE status 
            WHEN 0 THEN 'Deactivated' 
            WHEN 1 THEN 'Active' 
            WHEN 2 THEN 'Inactive' 
            WHEN 3 THEN 'Unregistered'
            ELSE 'Permanently Deleted' END")])
        ->where(['status' => '1'])
        ->andWhere(['conf_call' => 'y']);
    return $query;
}

}

2 ответа

Решение

Решено путем добавления этого в модель CallerIdentity.

public function fields()
{
    $fields = parent::fields();
    $fields[] = 'ehadata'; //name of relation
    return $fields;  
}

Выход:

 {
    "cidref": 1234,
    "custref": 288,
    "caller_id": "01758364762",
    "expiry": null,
    "conf_call": "n",
    "type": "S",
    "redirect": null,
    "destination": "846285845@sip.example.co.uk",
    "status": 1,
    "start_date": "2011-11-01",
    "last_polled": "2011-11-01 13:59:51",
    "ehadata": [
        {
            "status": 0,
            "name": "Blah blah blah",
            "bussuffix": "Ltd",
            "premesis": "Blah House",
            "thoroughfare": "Blah Road",
            "locality": "Blahbach",
            "postcode": "BLAH BLAH"
        }
    ]
},

Просто дикая догадка...

  • также есть ->joinWith метод для вашего активного запроса, используемый для быстрой загрузки
  • добавлять function extraFields() { return ['ehadata']; } на ваш CallerIdentity модель; прочитайте больше здесь http://www.yiiframework.com/doc-2.0/guide-rest-resources.html и позвоните в ваш API с &expand=ehadata пары
Другие вопросы по тегам