Reputation: 855
So i am working with Yii2 and i'm trying to get what i think is relation databases in order. What im trying to do is have multiple tables return one set of data with the Yii2 rest api.
Ive been trying to find exactly what i need to do but im a bit confused. I believe i have to setup foreign keys and link them somehow but that's where i get lost. Also i think once the database portion is setup, does the gii tool pick up on the 'relationships' when i build a new model? I think ill have to manually build the new model/controller instead of using gii, that sends all templates to the 'frontend' directory and not my versioned 'module/v1' directory
table 'state':
- country_id is the index
+--------------------------------------+---------------------------------+
| Column | Internal Relations | Foreign Key constraint (INNODB) |
+--------------------------------------+---------------------------------+
| country_id | yii2.country.country_id | yii2.ountry.country_id |
+------------------------------------------------------------------------+
+-------------+------------------+----------------------+
| id (int, AI)| country_id (int) | state_name (varchar) |
+-------------+------------------+----------------------+
| 0 | 10 | Texas |
| 1 | 10 | New York |
| 2 | 20 | Glasgow |
+-------------+------------------+----------------------+
table 'country':
- country_id is the Primary Key
- column to display in relation-view is country_name
+------------------+--------------------------+
| country_id (int) | country_name (varchar) |
+------------------+--------------------------+
| 10 | United States of America |
| 20 | Germany |
+------------------+--------------------------+
So my goal is to have my api GET request of: localhost/yii2/api/web/vi/states/
And get a response of:
{ "success": true, "data": [ { "id": 1, "country_id": 10, "state_name": "Texas" }, { "id": 2, "country_id": 10, "state_name": "New York" }, { "id": 3, "country_id": 20, "state_name": "Glasgow" } ] } }
model (State.php):
<?php
namespace api\modules\v1\models;
use Yii;
/**
* This is the model class for table "state".
*
* @property integer $country_id
* @property string $state_name
*
* @property Country $country
*/
class State extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'state';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['country_id', 'state_name'], 'required'],
[['country_id'], 'integer'],
[['state_name'], 'string', 'max' => 55]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'country_id' => 'Country ID',
'state_name' => 'State Name',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getCountry()
{
return $this->hasOne(Country::className(), ['country_id' => 'country_id']);
}
}
controller (StateController.php):
<?php
namespace api\modules\v1\controllers;
use yii\rest\ActiveController;
/**
* Country Controller API
*
*/
class StateController extends ActiveController
{
public $modelClass = 'api\modules\v1\models\State';
}
Upvotes: 0
Views: 1572
Reputation: 151
Setup the foreign keys in the database and use gii to generate the models and it will link them automatically according to your relation and then you can use it like this ...
$states= State::find()->with('countries')->all();
this code will get you the states with their countries
Upvotes: 2
Reputation: 9357
I believe i have to setup foreign keys and link them somehow but that's where i get lost. Also i think once the database portion is setup, does the gii tool pick up on the 'relationships' when i build a new model?
Yes
I think ill have to manually build the new model/controller instead of using gii, that sends all templates to the 'frontend' directory and not my versioned 'module/v1' directory
You can make Gii send the files wherever you want, you just have to put the namespace in the proper place.
Upvotes: 0