I
I
Ivan Lykov2017-06-01 20:15:16
Yii
Ivan Lykov, 2017-06-01 20:15:16

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
bade0e6594f6459a8e661abc6c81fe36.png
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>

Model
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

[[+comments_count]] answer(s)
M
Maxim Timofeev, 2017-06-02
@Jhon_Light

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']);
    }

you can add a geter, for example:
public function getSomeString()
    {
        return implode(',',ArrayHelper::getColumn($this->riskRiskKills,'someattribute')); 
    }

and in gridView
pro arrayHelper: www.yiiframework.com/doc-2.0/guide-helper-array.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question