PDF settings with MySql

Good evening

I need to create a pdf but with specific information
There is data that has a field in the database that is called Enabled, that field handles 2 data 1 for when it is active and 0 when it is inactive, what I need is that the pdf only shows the data that is enabled 1, since try putting in if the query but it doesn’t work for me, it doesn’t show me the correct data it shows everything that is stored in the DB

Sounds like missing if check. Need more details.

Hello
I have several data in a table in Mysql that have a field called enabled, that field handles 2 numbers, 0 when it is disabled and 1 when it is enabled, I have a foreach that shows me the data that is linked to the foreign key ejm:
the main table is called accidents, the other table is called methods, methods have a foreign key to accidents, the foreach that I have in the pdf is that it shows me all the methods that have the same id as this in accidents, what I need is that if the method has enabled 0, it will not show it and only show those that have enabled 1

Thanks in advance

Yes. Got it. You’re definitely missing a check in if. Try adding it.

Look at this is my code

<td align="center"><strong>
                        <?= Html::encode('VI. METODOLOGIA DE INVESTIGACION') ?>
                    </strong>
                </td>
            </tr>
            <?php
            if (Metodologia::find()->where("accidente_id = $model->id")->andWhere(['habilitado' => '0'])) {
                    ?>
                    <tr>
                        <td style="border: 0 #ffffff" align="center"><?= Html::encode('No hay Metodologia Asignada') ?></td>
                    </tr>
                    <?php
                } else {
            foreach ($model->qhseAccidentesMetodologia as $metodologiam) {
                    ?> 
                    <tr>
                        <td style="border: 0 #ffffff" align="center"><?= $metodologiam->metodologia0->sublist_name ?></td>
                    </tr>
                    <tr>
                        <td style="border: 0 #ffffff" align="center"><?= $metodologiam->listarp($metodologiam->id); ?></td>
                        <?php
                    }
                }
                ?>

but at the moment of showing it only shows me the message that is in the if, it does not show me the data that has 1 as enabled and in the database I have 2 records with enabled 1

I’m afraid I can’t help you debug your application. I’ve pointed out what could be wrong but it’s for you to check it.

This condition will be always true, because it only constructs a query and doesn’t execute it.

How can I build it and that the truth be executed? I do not know how to do it, I understand the logic but not how to translate it into code

Try the following:

if (Metodologia::find()->where("accidente_id = $model->id")->andWhere(['habilitado' => '0'])->one())

Please check the guide and/or API for “ActiveQuery::one()”.

Hello
I already implemented the If but it doesn’t show me anything in that part neither the message of the If nor the information that it has enabled in 1

Please post your code.

this is the pdf

    <tbody>
        <tr>
            <td align="center"><strong>
                    <?= Html::encode('VI. METODOLOGIA DE INVESTIGACION') ?>
                </strong>
            </td>
        </tr>
        <?php
        if (Metodologia::find()->where("accidente_id = $model->id")->andWhere(['habilitado' => '0'])->one()) {
                ?>
                <tr>
                    <td style="border: 0 #ffffff" align="center"><?= Html::encode('No hay Metodologia Asignada') ?></td>
                </tr>
                <?php
            } else {
        foreach ($model->qhseAccidentesMetodologia as $metodologiam) {
                ?> 
                <tr>
                    <td style="border: 0 #ffffff" align="center"><?= $metodologiam->metodologia0->sublist_name ?></td>
                </tr>
                <tr>
                    <td style="border: 0 #ffffff" align="center"><?= $metodologiam->listarp($metodologiam->id); ?></td>
                    <?php
                }
            }
            ?>
        </tr>
    </tbody>
</table>

this is the function of the foreign key in the accident model

public function getQhseAccidentesMetodologia() {
    return $this->hasMany(Metodologia::className(), ['accidente_id' => 'id']);
}

this is the function of the methodologies model

public function getMetodologia0() {
    return $this->hasOne(Sublistas::className(), ['sublist_id' => 'metodologia']);
}

/**
 * Gets query for [[Accidente]].
 *
 * @return \yii\db\ActiveQuery
 */
public function getAccidente() {
    return $this->hasOne(Accidentes::className(), ['id' => 'accidente_id']);
}

Ah, sorry.

if (Metodologia::find()->where(['accidente_id' => $model->id])->andWhere(['habilitado' => '0'])->one()){

Hello
I already implemented the if in which you helped me but it does not show me the information that is in one the message of the If

Do you mean to say that “if” condition is always evaluated as “false”?

no, it is being evaluated true, the condition must be that if there is any value for the accident id in enabled 1, it should show only that data, regardless of the fact that they find more data with the same accident id but in enabled 0

I’m not sure, but I guess your “if” condition should be in the “foreach” loop, like this:

<tbody>
    <tr>
        <td><strong>VI. METODOLOGIA DE INVESTIGACION'</strong></td>
    </tr>
    <?php foreach ($model->qhseAccidentesMetodologia as $metodologiam) : ?>
        <?php if ($metodologiam->metodologia0->habilitado == 0) : ?>
            <tr>
                <td>No hay Metodologia Asignada</td>
            </tr>
        <?php else: ?>
            <tr>
                <td><?= $metodologiam->metodologia0->sublist_name ?></td>
            </tr>
            <tr>
                <td><?= $metodologiam->listarp($metodologiam->id); ?></td>
            </tr>
         <?php endif; ?>
    <?php endforeach; ?>
    </tbody>
</table>

yes perfect thank you very much it worked as I needed it