Search code examples
sqlyii2mariadbright-join

Yii2 right join only returning 2 columns


Working on an API and I have this query:

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

        echo $query->getSQL();die;

The output of the ->getSQL() function is:

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

and the result of running the query through the function in postman returns:

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

However when I run the query in adminer the result is:

Result from adminer

Has anyone got any idea why this might be? I am well and truly confused. Any help is massively appreciated.

EhaData model

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']);
}
}

Caller Identity model

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;
}

}

Solution

  • Solved by adding this in to the CallerIdentity model.

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

    Output:

     {
        "cidref": 1234,
        "custref": 288,
        "caller_id": "01758364762",
        "expiry": null,
        "conf_call": "n",
        "type": "S",
        "redirect": null,
        "destination": "[email protected]",
        "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"
            }
        ]
    },