控制器
$companyObj = new Company();
$city = City::findOne(['name' => $post["CabinetRegistrationForm"]["city_name"]]);
$companyObj->name = $post["CabinetRegistrationForm"]["company_name"];
$companyObj->activity = 1;
$companyObj->district_id = $city->district_id;
$companyObj->region_id = $city->region_id;
$companyObj->municipality_id = $city->municipality_id;
$companyObj->city_id = $city->id;
$companyObj->address = $post["CabinetRegistrationForm"]["address"];
$companyObj->inn = $post["CabinetRegistrationForm"]["inn"];
$companyObj->approved = 0;
$companyObj->head_fio = $post["CabinetRegistrationForm"]["head_fio"];
$companyObj->phone = $post["CabinetRegistrationForm"]["phone"];
$companyObj->save();
模型
namespace common\models;
use mdm\admin\models\AuthItem;
use Yii;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;
use yii\db\Query;
use yii\helpers\ArrayHelper;
use yii\web\IdentityInterface;
/**
* This is the model class for table "company".
*
* @property int $id
* @property string $name
* @property string $org_form
* @property string $activity
* @property int $district_id
* @property int $region_id
* @property int $municipality_id
* @property int $city_id
* @property string $address
* @property string $inn
* @property int $approved
* @property string $head_fio
* @property string $phone
* @property string $email
* @property string $site
*
* @property DistrictModel $district
* @property Municipality $municipality
* @property City $city
* @property User[] $users
* @property SchoolClass[] $classes
*/
class Company extends \common\components\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'company';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['name', 'org_form', 'activity'], 'required'],
[['district_id', 'region_id', 'municipality_id', 'city_id', 'approved'], 'integer'],
[['address'], 'string'],
[['name', 'org_form', 'activity', 'inn', 'head_fio', 'email', 'site', 'presentation_url'], 'string', 'max' => 255],
[['phone'], 'string', 'max' => 50],
[['district_id'], 'exist', 'skipOnError' => true, 'targetClass' => DistrictModel::className(), 'targetAttribute' => ['district_id' => 'id']],
[['municipality_id'], 'exist', 'skipOnError' => true, 'targetClass' => Municipality::className(), 'targetAttribute' => ['municipality_id' => 'id']],
[['presentation_url'], 'trim'],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => Yii::t('app', 'ID'),
'name' => Yii::t('app', 'Name'),
'org_form' => Yii::t('app', 'Org Form'),
'activity' => Yii::t('app', 'Activity'),
'district_id' => Yii::t('app', 'District ID'),
'region_id' => Yii::t('app', 'Region ID'),
'municipality_id' => Yii::t('app', 'Municipality ID'),
'city_id' => Yii::t('app', 'City ID'),
'address' => Yii::t('app', 'Address'),
'inn' => Yii::t('app', 'Inn'),
'approved' => Yii::t('app', 'Approved'),
'head_fio' => Yii::t('app', 'Head Fio'),
'phone' => Yii::t('app', 'Phone'),
'email' => Yii::t('app', 'Email'),
'site' => Yii::t('app', 'Site'),
'presentation_url' => Yii::t('app', 'Presentation Url'),
];
}
看法
<?php $form = ActiveForm::begin(['id' => 'form-signup']); ?>
<div class="hidden">
<?= $form->field($model, 'role')->textInput() ?>
</div>
<?= $form->field($model, 'second_name')->textInput() ?>
<?= $form->field($model, 'name')->textInput() ?>
<?= $form->field($model, 'last_name')->input('last_name') ?>
<?= $form->field($model, 'email')->input('email') ?>
<?= $form->field($model, 'inn')->widget(SuggestionsWidget::class, [
'token' => '*******************',
]) ?>
<?php if ($model->role == RoleModel::ROLE_RESEARCH_COORDINATOR_OO): ?>
<?= $form->field($model, 'company_name')->textarea(); ?>
<?= $form->field($model, 'city_name')->hiddenInput()->label(false); ?>
<?= $form->field($model, 'address')->hiddenInput()->label(false); ?>
<?= $form->field($model, 'head_fio')->hiddenInput()->label(false); ?>
<?= $form->field($model, 'phone')->hiddenInput()->label(false); ?>
<?php endif; ?>
..........
一切似乎都是正确的,没有显示错误,$companyObj->errors
- 一个空数组,但它$companyObj->save
返回 false。数据库中没有新行。请帮帮我。
你看,当你调用save()方法的时候,首先按照你在rules()中描述的规则对模型进行验证,如果一切正常,模型就飞到了数据库中。这意味着仍然存在验证错误。调用$companyObj->validate()方法,然后看看$companyObj->getErrors()是什么,会有错误,我保证)))
顺便说一句,保存时可以禁用验证,$companyObj->save(false)
我会重写控制器:
从控制器到模型的部分将被丢弃:
虽然这是你的基于表单数据的城市搜索,但xs不知何故是愚蠢的......封装也很难)拿它并替换你想要的数据)
请记住,在 rules() 中,您只描述表单中的那些字段。具有 3 个字段的表单,在 rules() 3 个字段中,您描述,否则所有其他属性都可以替换为任何属性))))
$model->load(Yii::$app->request->post()):