# Yii2 Model hasOne & hasMany 用法 model設定關聯表 hasOne & hasMany 用法 ```php /** * This is the model class for table "deposit". * * @property integer $customer_id * @property integer $channel_id * @property string $order_sn * @property double $Order_amount * * @property Customer $customer * @property Channel $channel */ class Deposit extends BaseActiveRecord { // set table name public static function tableName() { return 'deposit'; } public function rules() { // 設定table的欄位規則 } public function attributeLabels() { // 設定屬性名稱 } public function getChannel() { return $this->hasOne(Channel::className(), ['CID' => 'CID']); } public function getCustomer() { return $this->hasOne(Customer::className(), ['UID' => 'UID']); } } ``` 使用`$this->hasOne('關聯表明稱', ['關聯表欄位' => '本表欄位']);`取得相對應的資料 ###### tags: `Yii2` `PHP` `model`