我有3个班级Persona,Alumno和factura。我需要使用gridview和搜索功能在factura视图上显示person fullname。全名在Persona中。 Factura只通过id_alumno与alumno相关,alumno通过id_persona与persona相关。 我尝试通过创建一个新的变量fullname和一个函数来解决这个问题,以便在类factura上获得角色。
class Factura extends \yii\db\ActiveRecord{
public $full_name;
}
public static function tableName()
{
return 'factura';
}
public function rules()
{
return [
[['id_factura_reemplazo', 'id_obra_social', 'id_alumno',], 'integer'],
[['id_obra_social', 'id_alumno'], 'required'],
[['fecha_factura','fullname','id_persona'], 'safe'],
];
}
public function attributeLabels()
{
return [
'id_factura' => 'Id Factura',
'id_factura_reemplazo' => 'Id Factura Reemplazo',
'id_obra_social' => 'Id Obra Social',
'id_alumno' => 'Id Alumno',
'fullName'=>Yii::t('app', 'Nombre y Apellido'),
];
}
public function getIdAlumno()
{
return $this->hasOne(Alumno::className(), ['id_alumno' => 'id_alumno']);
}
public function getIdPersona() {
return $this->hasOne(Persona::className(), ['id_persona' => 'id_persona'])
->via('idAlumno');
}
public function getFullname(){
if($this->idPersona)
return $this->idPersona->nombre." ".$this->idPersona->apellido;
return null;
}
我得到: 无效的调用 - yii \ base \ InvalidCallException
设置只读属性:app \ models \ Facturasearch :: fullName
if (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
} else {
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
}
它就像它没有吸气剂......当它试图做$ this-> load($ params);
时问题从线上跳了出来class Facturasearch extends Factura{
public function rules()
{
return [
[['id_factura', 'id_factura_reemplazo', 'id_alumno', 'numero'], 'integer'],
[['id_obra_social','fecha_factura', 'observacion','id_alumno','fullName','id_persona'], 'safe'],
];
}
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
public function search($params)
{
$query = Factura::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
...
答案 0 :(得分:1)
您应该在FacturaSearch中添加pubblic var以获取全名
class Facturasearch extends Factura{
public $fullname;
public function rules()
{
return [
[['id_factura', 'id_factura_reemplazo', 'id_alumno', 'numero'], 'integer'],
[['id_obra_social','fecha_factura', 'observacion','id_alumno','fullName','id_persona'], 'safe'],
];
}
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
public function search($params)
{
$query = Factura::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
...