如何获取 joinWith 查询出的数据字段 [ 2.0 版本 ]
直接上代码:
Model:
public function getIpaddrs()
{
return $this->hasMany(Ipaddr::className(), ['deviceID' => 'id']);
}
controller 中查询关联字段:
$existDevice = Device::find()->where(['deviceMac'=>$val['deviceMac']])->joinWith('ipaddrs')->all();
dd($existDevice);die;
可以查询打印出来如下结果:
array(1) {
[0]=>
object(app\models\Device)#95 (18) {
["deviceCount"]=>
NULL
["deviceIp"]=>
NULL
["printerBrand"]=>
NULL
["printerModle"]=>
NULL
["isPrinterConn"]=>
NULL
["printerCreatetime"]=>
NULL
["isNetworkConn"]=>
NULL
["devcieLog"]=>
NULL
["_attributes":"yii\db\BaseActiveRecord":private]=>
array(13) {
["id"]=>
int(10)
["deviceNum"]=>
string(9) "002550006"
["deviceBrand"]=>
string(8) "MECHREVO"
["deviceType"]=>
string(2) "PC"
["deviceModle"]=>
string(21) "Z2 Air Series GK5CN5X"
["deviceHostName"]=>
string(9) "Breakwind"
["deviceMac"]=>
string(17) "B0:25:AA:2E:7F:FA"
["deviceUsage"]=>
string(6) "办公"
["deviceUsername"]=>
string(6) "王鹏"
["deviceLocation"]=>
string(9) "信息科"
["deviceCreatetime"]=>
string(10) "2018-11-01"
["deviceStatusID"]=>
int(2)
["deviceLog"]=>
string(138) "2020-02-19 11:47:53王鹏 为设备添加使用记录,分配设备号为:002550006使用地点及使用人:信息科-王鹏;
"
}
["_oldAttributes":"yii\db\BaseActiveRecord":private]=>
array(13) {
["id"]=>
int(10)
["deviceNum"]=>
string(9) "002550006"
["deviceBrand"]=>
string(8) "MECHREVO"
["deviceType"]=>
string(2) "PC"
["deviceModle"]=>
string(21) "Z2 Air Series GK5CN5X"
["deviceHostName"]=>
string(9) "Breakwind"
["deviceMac"]=>
string(17) "B0:25:AA:2E:7F:FA"
["deviceUsage"]=>
string(6) "办公"
["deviceUsername"]=>
string(6) "王鹏"
["deviceLocation"]=>
string(9) "信息科"
["deviceCreatetime"]=>
string(10) "2018-11-01"
["deviceStatusID"]=>
int(2)
["deviceLog"]=>
string(138) "2020-02-19 11:47:53王鹏 为设备添加使用记录,分配设备号为:002550006使用地点及使用人:信息科-王鹏;
"
}
["_related":"yii\db\BaseActiveRecord":private]=>
array(1) {
["ipaddrs"]=>
array(1) {
[0]=>
object(app\models\Ipaddr)#115 (12) {
["ipstart"]=>
NULL
["ipend"]=>
NULL
["_attributes":"yii\db\BaseActiveRecord":private]=>
array(7) {
["id"]=>
int(158)
["ipType"]=>
string(9) "办公网"
["station"]=>
string(6) "永安"
["ipaddr"]=>
string(14) "10.138.237.159"
["netmask"]=>
string(13) "255.255.255.0"
["gateway"]=>
string(12) "10.138.237.1"
["deviceID"]=>
int(10)
}
["_oldAttributes":"yii\db\BaseActiveRecord":private]=>
array(7) {
["id"]=>
int(158)
["ipType"]=>
string(9) "办公网"
["station"]=>
string(6) "永安"
["ipaddr"]=>
string(14) "10.138.237.159"
["netmask"]=>
string(13) "255.255.255.0"
["gateway"]=>
string(12) "10.138.237.1"
["deviceID"]=>
int(10)
}
["_related":"yii\db\BaseActiveRecord":private]=>
array(0) {
}
["_relationsDependencies":"yii\db\BaseActiveRecord":private]=>
array(0) {
}
["_errors":"yii\base\Model":private]=>
NULL
["_validators":"yii\base\Model":private]=>
NULL
["_scenario":"yii\base\Model":private]=>
string(7) "default"
["_events":"yii\base\Component":private]=>
array(0) {
}
["_eventWildcards":"yii\base\Component":private]=>
array(0) {
}
["_behaviors":"yii\base\Component":private]=>
array(0) {
}
}
}
}
["_relationsDependencies":"yii\db\BaseActiveRecord":private]=>
array(0) {
}
["_errors":"yii\base\Model":private]=>
NULL
["_validators":"yii\base\Model":private]=>
NULL
["_scenario":"yii\base\Model":private]=>
string(7) "default"
["_events":"yii\base\Component":private]=>
array(0) {
}
["_eventWildcards":"yii\base\Component":private]=>
array(0) {
}
["_behaviors":"yii\base\Component":private]=>
array(0) {
}
}
}
可以看到查询出了关联的ip地址表的内容,但是我使用 $existDevice->ipaddrs
就是取不到内容一直为 null
求解如何调取其中 ipaddr
的值
breakwind 补充于 2020-02-19 20:24
找到调取的方法了,土了点但好歹是调出来了,发出来给大家分享下,
$existDevice->getRelatedRecords()['ipaddrs'][0]->ipaddr
也欢迎大家补充更好的办法
共 0 个回答
没有找到数据。
breakwind
注册时间:2018-01-23
最后登录:2020-03-27
在线时长:2小时7分
最后登录:2020-03-27
在线时长:2小时7分
- 粉丝0
- 金钱10
- 威望0
- 积分30