简体   繁体   中英

Yii2 - using 2 models in 1 view

I tried using 2 models in 1 view, but the fields I added are "(not set)" but they're deffinetly set in the database so what did i do wrong

Here you see the result of the added fields

here is the view code:

    <?php

    use yii\helpers\Html;
    use yii\widgets\DetailView;
    use app\models\Facturen;
    /* @var $this yii\web\View */
    /* @var $model app\models\Facturen */
    /* @var $modelProducten app\models\Producten */

    $this->title = $model->factuur_id;
    $this->params['breadcrumbs'][] = ['label' => 'Factures', 'url' => ['index']];
    $this->params['breadcrumbs'][] = $this->title;
    ?>
    <div class="facturen-view">

        <h1><?= Html::encode($this->title) ?></h1>

        <p>
            <?= Html::a('Update', ['update', 'id' => $model->factuur_id], ['class' => 'btn btn-primary']) ?>
            <?= Html::a('Delete', ['delete', 'id' => $model->factuur_id], [
                'class' => 'btn btn-danger',
                'data' => [
                    'confirm' => 'Are you sure you want to delete this item?',
                    'method' => 'post',
                ],
            ]) ?>
        </p>

        <?= DetailView::widget([
            'model' => $model,
            'modelProducten' => $modelProducten,
            'attributes' => [
                'factuur_id',
                'company.company_name',
                'person.first_name',
                'product.product_id',
                'product.product_name',
                'product.amount',
                'product.price',
                'date',
            ],
        ]) ?>

    </div>
also the create code might be usefull

    public function actionCreate()
    {
        $model = new Facturen();
        $modelProducten = [new Producten];

        if ($model->load(Yii::$app->request->post())) {
            $transaction = Yii::$app->db->beginTransaction();
            $success = true;

            $modelProducten = Model::createMultiple(Producten::classname());
            Model::loadMultiple($modelProducten, Yii::$app->request->post());  

            if ($model->save()) {
                foreach($modelProducten as $modelProduct) {
                    $modelProduct->factuur_id = $model->factuur_id;
                    if (! $modelProduct->save()) {
                        $success = false;
                        break;
                    }
                }
            } else {
                $success = false;
            }

            if ($success) {
                $transaction->commit();
                return $this->redirect(['view', 'id' => $model->factuur_id]);
            } else {
                Yii::$app->session->setFlash('danger', 'Kan niet opslaan, validate errors');
            }
        }

        return $this->render('create', [
            'model' => $model,
            'modelProducten' => (empty($modelProducten)) ? [new Producten] : $modelProducten
        ]);
    }

if you need any extra code just ask I'll provide it!

hopefully you guys understand the problem and are able to help me out

~ edit ~

@Gunnrryy

the relations are here also the person works totaly fine only the "Producten" fields don't work

the Facturen model Code

    <?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "facturen".
 *
 * @property integer $factuur_id
 * @property string $date
 * @property integer $company_id
 * @property integer $person_id
 *
 * @property Companies $company
 * @property Spokepersons $person
 * @property Producten[] $productens
 */
class Facturen extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'facturen';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['date', 'company_id', 'person_id'], 'required'],
            [['date'], 'string', 'max' => 64],
            [['company_id', 'person_id'], 'integer'],
            [['company_id'], 'exist', 'skipOnError' => true, 'targetClass' => Companies::className(), 'targetAttribute' => ['company_id' => 'company_id']],
            [['person_id'], 'exist', 'skipOnError' => true, 'targetClass' => Spokepersons::className(), 'targetAttribute' => ['person_id' => 'person_id']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'factuur_id' => 'Factuurnummer',
            'date' => 'Factuurdatum',
            'company_id' => 'Bedrijfsnaam',
            'person_id' => 'Contactpersoon',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getCompany()
    {
        return $this->hasOne(Companies::className(), ['company_id' => 'company_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getPerson()
    {
        return $this->hasOne(Spokepersons::className(), ['person_id' => 'person_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getProduct()
    {
        return $this->hasMany(Producten::className(), ['factuur_id' => 'factuur_id']);
    }
}

do the fact you already have function for relation you could add a getter for the related field you neeed eg for company name you could add this function in you Facturen model

  /* Getter for Company name  name */
  public function getCompanyName() {
      return $this->company->company_name;
  }

then in detail view you can simply companyName

<?= DetailView::widget([
        'model' => $model,
        'modelProducten' => $modelProducten,
        'attributes' => [
            'factuur_id',
            'companyName',
            'date',
        ],
    ]) ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM