# think-mongot模型关联无效解决
在使用think-mongo做模型关联时,无效
```php
$mongoHouse->with('community')->get("6164fd567c018d840200000a");
$mongoHouse->community->name
```
发下报错:
```php
[think\exception\ErrorException]
Illegal offset type in isset or empty
```
### 原因
```php
/**
* @property CommunitiesMongo $community 小区模型
*/
class HousesMongo extends BaseCityMongo
{
public $table = "houses";
/**
* 小区 相对关联模型
* @return \think\model\relation\BelongsTo
*/
public function community()
{
//think-mongo 做了_id到id的映射,所以这里的关联模型主键用id代替_id
return $this->belongsTo(CommunitiesMongo::class, 'community_id', 'id')->bind([
'community_name' => 'base.name',
]);
}
}
```
使用为mongo的ObjectId关联会错误,BelongsTo::eagerlyOne()方法在处理关联的时候出错
263行左右
```php
if (!isset($data[$result->$foreignKey]))
```
\$result->\$foreignKey可能是一个mongo的ObjectId对象,不是一个常量。所以可能报错或找不到关联