Hello i am new in MVC and i am learning Yii 2.
[b]I want to find a country show all the countries and their respective cities.
I know I must change the " CountrySearch " but I do not know how.
I use only GII and manually add relationships hasMany and hasOne.
Sorry for my English is bad please help thanks
[/b]
Code sql of me tables
CREATE TABLE IF NOT EXISTS `bdyii`.`Country` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `bdyii`.`City` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`country_ID` INT UNSIGNED NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
model/Country
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "country".
*
* @property string $id
* @property string $name
*/
class Country extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'country';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['name'], 'required'],
[['name'], 'string', 'max' => 45]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'name' => 'Name',
];
}
public function getCitys() {
return $this->hasMany(City::className(), ['country_ID' => 'id']);
}
}
CountrySearch
<?php
namespace app\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\Country;
/**
* CountrySearch represents the model behind the search form about `app\models\Country`.
*/
class CountrySearch extends Country
{
/**
* @inheritdoc
*/
public function rules()
{
return [
[['id'], 'integer'],
[['name'], 'safe'],
];
}
/**
* @inheritdoc
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = Country::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
]);
$query->andFilterWhere(['like', 'name', $this->name]);
return $dataProvider;
}
}
view/_search
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model app\models\CountrySearch */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="country-search">
<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
]); ?>
<?= $form->field($model, 'id') ?>
<?= $form->field($model, 'name') ?>
<div class="form-group">
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
<?= Html::resetButton('Reset', ['class' => 'btn btn-default']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
model/city
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "city".
*
* @property string $id
* @property string $name
* @property string $country_ID
*/
class City extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'city';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['name', 'country_ID'], 'required'],
[['country_ID'], 'integer'],
[['name'], 'string', 'max' => 45]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'name' => 'Name',
'country_ID' => 'Country ID',
];
}
public function getCountry() {
return $this->hasOne(Country::className(), ['id' => 'country_ID']);
}
}