我是yii2的新手,并试图在yii2中下拉,但我无法从表中以正确的方式获取数据。这是角色和docente,它们通过id_persona相关联。我需要的字段是docente.id_docente和persona.nombre,我通过加入来获取它们:
$personas = Persona::find()->with('docentes')->all();
然后我映射下拉列表所需的数据:
$data = ArrayHelper::map($personas, 'docente.id_docente','nombre');
我收到错误以下错误:
Getting unknown property: app\models\Persona::docente
我做错了什么?
当我做var_dump($ personas)时,我得到:
array (size=4)
0 =>
object(app\models\Persona)[71]
private '_attributes' (yii\db\BaseActiveRecord) =>
array (size=15)
'id_persona' => int 15
'nombre' => string 'prueba' (length=6)
'apellido' => string 'prueba' (length=6)
...
1 =>
object(app\models\Persona)[90]
private '_attributes' (yii\db\BaseActiveRecord) =>
array (size=15)
'id_persona' => int 16
'nombre' => string 'sebastian' (length=9)
'apellido' => string 'carreras' (length=8)
...
表docente中的字段不在那里,我不明白......
模特角色:
/**
* @return \yii\db\ActiveQuery
*/
public function getAlumnos()
{
return $this->hasMany(Alumno::className(), ['id_persona' =>'id_persona']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getDocentes()
{
return $this->hasMany(Docente::className(),['id_persona'=>'id_persona']);
}
模型docente
/**
* @return \yii\db\ActiveQuery
*/
public function getIdPersona()
{
return $this->hasOne(Persona::className(), ['id_persona' => 'id_persona']);
}