我尝试在1个视图中使用2个模型,但是我添加的字段是“(未设置)”但是它们在数据库中被设置为deffinetly,所以我做错了什么
Here you see the result of the added fields
这是视图代码:
<?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
]);
}
如果你需要任何额外的代码,请问我会提供它!
希望你们能够理解这个问题并能够帮助我
〜编辑〜
@Gunnrryy
the relations are here 这个人的工作总体上只有“Producten”字段不起作用
Facturen模型代码
<?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']);
}
}
答案 0 :(得分:3)
你已经拥有关系功能的事实你可以为你需要的相关领域添加一个getter 例如,对于公司名称,您可以在Facturen模型中添加此功能
/* Getter for Company name name */
public function getCompanyName() {
return $this->company->company_name;
}
然后在详细视图中您可以简单地使用companyName
<?= DetailView::widget([
'model' => $model,
'modelProducten' => $modelProducten,
'attributes' => [
'factuur_id',
'companyName',
'date',
],
]) ?>