public $js = [
'js/gmaps.js',
'//maps.googleapis.com/maps/api/js?key=***&libraries=places&callback=initMap',
];
<div class="col-md-4 col-sm-6 m-b30">
<div id="map"></div>
</div>
<?php
$js = <<< JS
var autocompletes, marker, infowindow, map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -33.8688, lng: 151.2195},
zoom: 13
});
infowindow = new google.maps.InfoWindow();
marker = new google.maps.Marker({
map: map
});
// адрес откуда
var inputs = document.querySelector('#place_departure');
autocompletes = new google.maps.places.Autocomplete(inputs);
google.maps.event.addListener(autocompletes, 'place_changed', function () {
marker.setVisible(false);
infowindow.close();
var place = autocompletes.getPlace();
if (!place.geometry) {
window.alert("No details available for input: '" + place.name + "'");
return;
}
if (place.geometry.viewport) {
map.fitBounds(place.geometry.viewport);
} else {
map.setCenter(place.geometry.location);
map.setZoom(17);
}
marker.setIcon(({
url: place.icon,
scaledSize: new google.maps.Size(35, 35)
}));
marker.setPosition(place.geometry.location);
marker.setVisible(true);
var place_departure = '';
if (place.address_components) {
place_departure = [
(place.address_components[0] && place.address_components[0].short_name || ''),
(place.address_components[1] && place.address_components[1].short_name || ''),
(place.address_components[2] && place.address_components[2].short_name || '')
].join(' ');
}
infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + place_departure);
infowindow.open(map, marker);
});
}
JS;
$this->registerJs($js, $position = yii\web\View::POS_READY, $key = null );
?>
$listing = app\models\City::find()->indexBy('city_name')->orderBy('city_name')->asArray()->all();
$city = ArrayHelper::map($listing,'id','city_name');
Error: Call to a member function isAttributeRequired() on null in J:\OSPanel\domains\forms6.loc\vendor\yiisoft\yii2\widgets\ActiveField.php:915
Stack trace:
#0 J:\OSPanel\domains\forms6.loc\vendor\yiisoft\yii2\widgets\ActiveField.php(776): yii\widgets\ActiveField->addAriaAttributes('<span class="st...')
#1 J:\OSPanel\domains\forms6.loc\views\accept\form_user.php(36): yii\widgets\ActiveField->widget('kartik\\select2\\...', Array)
#2 J:\OSPanel\domains\forms6.loc\vendor\yiisoft\yii2\base\View.php(336): require('J:\\OSPanel\\doma...')
#3 J:\OSPanel\domains\forms6.loc\vendor\yiisoft\yii2\base\View.php(254): yii\base\View->renderPhpFile('J:\\OSPanel\\doma...', Array)
#4 J:\OSPanel\domains\forms6.loc\vendor\yiisoft\yii2\base\View.php(156): yii\base\View->renderFile('J:\\OSPanel\\doma...', Array, NULL)
#5 J:\OSPanel\domains\forms6.loc\views\accept\_form.php(29): yii\base\View->render('form_user', Array)
#6 J:\OSPanel\domains\forms6.loc\vendor\yiisoft\yii2\base\View.php(336): require('J:\\OSPanel\\doma...')
#7 J:\OSPanel\domains\forms6.loc\vendor\yiisoft\yii2\base\View.php(254): yii\base\View->renderPhpFile('J:\\OSPanel\\doma...', Array)
#8 J:\OSPanel\domains\forms6.loc\vendor\yiisoft\yii2\base\View.php(156): yii\base\View->renderFile('J:\\OSPanel\\doma...', Array, NULL)
#9 J:\OSPanel\domains\forms6.loc\views\accept\update.php(17): yii\base\View->render('_form', Array)
#10 J:\OSPanel\domains\forms6.loc\vendor\yiisoft\yii2\base\View.php(336): require('J:\\OSPanel\\doma...')
#11 J:\OSPanel\domains\forms6.loc\vendor\yiisoft\yii2\base\View.php(254): yii\base\View->renderPhpFile('J:\\OSPanel\\doma...', Array)
#12 J:\OSPanel\domains\forms6.loc\vendor\yiisoft\yii2\base\View.php(156): yii\base\View->renderFile('J:\\OSPanel\\doma...', Array, Object(app\controllers\AcceptController))
#13 J:\OSPanel\domains\forms6.loc\vendor\yiisoft\yii2\base\Controller.php(384): yii\base\View->render('update', Array, Object(app\controllers\AcceptController))
#14 J:\OSPanel\domains\forms6.loc\controllers\AcceptController.php(129): yii\base\Controller->render('update', Array)
#15 [internal function]: app\controllers\AcceptController->actionUpdate('818')
#16 J:\OSPanel\domains\forms6.loc\vendor\yiisoft\yii2\base\InlineAction.php(57): call_user_func_array(Array, Array)
#17 J:\OSPanel\domains\forms6.loc\vendor\yiisoft\yii2\base\Controller.php(157): yii\base\InlineAction->runWithParams(Array)
#18 J:\OSPanel\domains\forms6.loc\vendor\yiisoft\yii2\base\Module.php(528): yii\base\Controller->runAction('update', Array)
#19 J:\OSPanel\domains\forms6.loc\vendor\yiisoft\yii2\web\Application.php(103): yii\base\Module->runAction('accept/update', Array)
#20 J:\OSPanel\domains\forms6.loc\vendor\yiisoft\yii2\base\Application.php(386): yii\web\Application->handleRequest(Object(yii\web\Request))
#21 J:\OSPanel\domains\forms6.loc\web\index.php(13): yii\base\Application->run()
#22 {main}
модель Address
public function getAccepts(){
return $this->hasMany(Accept::className(), ['id' => 'accept_id']);
}
модель Accept
public function getAddress(){
return $this->hasOne(Address::className(), ['accept_id' => 'id']);
}
$listing = app\models\City::find()->indexBy('city_name')->orderBy('city_name')->asArray()->all();
$city = ArrayHelper::map($listing,'id','city_name');
<?=
$form->field($address, 'city_id', ['template' => "{label}\n{input}"])->widget(Select2::classname(), [
'data' => $city,
'options' => ['placeholder' => 'Выбор станции'],
'pluginOptions' => [
'allowClear' => true
],
]);
?>
<?=
$form->field($address, 'city_id', ['template' => "{label}\n{input}"])->widget(Select2::classname(), [
'data' => $city,
'options' => ['placeholder' => 'Выбор станции'],
'pluginOptions' => [
'allowClear' => true
],
]);
?>
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'accept_id' cannot be null
The SQL being executed was: INSERT INTO `address` (`accept_id`, `city_id`, `street`, `house`, `name_building`, `floor`, `cabinet`) VALUES (NULL, 1, 'Дзержинского', '5', NULL, '1', 'б/н')
public function actionCreate()
{
$model = new Accept();
$address = new Address();
$address->accept_id = $accept->id;
if ($model->load(Yii::$app->request->post()) && $address->load(Yii::$app->request->post()) && $model->save()) {
if ($address->save()) {
$address->accept_id = $accept->id;
return $this->redirect(['view', 'id' => $model->id]);
}
}
return $this->render('create', compact('model', 'address'));
}
public function getDocument(){
return $this->hasOne(Document::className(), ['id' => 'doc_id']);
}
public function getUser(){
return $this->hasOne(User::className(), ['id' => 'user_id']);
}
public function getTypes(){
return $this->hasOne(Documenttype::className(), ['id' => 'type_id']);
}
public function getDocsign(){
return $this->hasMany(Docsign::className(), ['doc_id' => 'id']);
}
public function getDocUser(){
return $this->hasMany(Docsign::className(), ['user_id' => 'id']);
}
Если нет, то тяните через связь с той моделью, где оно есть.