Answer the question
In order to leave comments, you need to log in
How to correctly display information from the database in YII2?
Good day
Guys, tell my stupid head where I'm missing the moment. I added fields to the "view", in the model I registered the fields from the table that need to be displayed. But this is not a task where I miss the moment (I repeat). the data is not displayed in the table. It returns "Not Set" instead.
Tried to remove "Not Set" via config/web.php using "formatter". "Not Set". disappears, but no data is output.
Help, I really need it
Here is a screenshot
View
<div class="risk-index">
<h1><?= Html::encode($this->title) ?></h1>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<p>
<?= Html::a('Create Risk', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'name_risk',
'status_risk',
'name_project',
'name_issue',
'name_kill_risk',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
</div>
class risk extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'Risk';
}
/**
* @inheritdoc
*/
public $name_kill_risk;
public $name_issue;
public $name_project;
public function rules()
{
return [
[['name_risk', 'status_risk'], 'string', 'max' => 20],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'name_risk' => 'Name Risk',
'status_risk' => 'Status Risk',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getRiskRiskKills()
{
return $this->hasMany(RiskRiskKill::className(), ['risk_id' => 'id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getIssueRisks()
{
return $this->hasMany(IssueRisk::className(), ['risk_id' => 'id']);
}
}
Answer the question
In order to leave comments, you need to log in
Hell of a thing you do. If you have hasMany, then there is an array of objects, and you are trying to display them as a string? Moreover, public variables were not created for some reason.
If you need to display hasMany in the string, then for example,
for communication
public function getRiskRiskKills()
{
return $this->hasMany(RiskRiskKill::className(), ['risk_id' => 'id']);
}
public function getSomeString()
{
return implode(',',ArrayHelper::getColumn($this->riskRiskKills,'someattribute'));
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question