我已經建立了這個yii2應用,在那裏我有一個型號命名客戶獲得值,和另一個名爲訂購,其中CUSTOMER_ID是訂購外鍵。Yii2從相關型號
我已經創建了一個名爲SendEmail的動作,這是在訂單索引頁使用,我需要得到的訂單將會被髮送到根據CUSTOMER_ID的電子郵件。
所以,我怎麼能得到根據客戶當前訂購的電子郵件?
客戶型號:
<?php
namespace app\models;
use Yii;
class Customer extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'customer';
}
public function rules()
{
return [
[['Name'], 'required'],
[['Archive', 'Credit'], 'integer'],
[['Address'], 'string'],
[['Name', 'Email'], 'string', 'max' => 50],
[['Tel'], 'string', 'max' => 14],
[['Category'], 'string', 'max' => 25],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'Id' => 'ID',
'Name' => 'Name',
'Tel' => 'Tel',
'Email' => 'Email',
'Archive' => 'Archive',
'Credit' => 'Credit',
'Address' => 'Address',
'Category' => 'Category'
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getOrders()
{
return $this->hasMany(Order::className(), ['Customer_Id' => 'Id']);
}
}
這裏是在控制器中的作用:
public function actionSendEmail($id)
{
//Here I should get the email according to the customer_id
if ($model->load(Yii::$app->request->post()))
{
$value= Yii::$app->mailer->compose()
->setFrom (['[email protected]'=>'Smth'])
->setTo ($model->Email)
->setSubject ("Price Offer")
->setHtmlBody ("Test")
->send();
$model->save();
return $this->redirect(['view','id'=>$model->id]);}
else
return $this -> render('create',['model'=>$model,]);
}
您是否定義了Yii 2中兩個模型之間的關係? – Bizley
是的,我確實做到了。 –
那麼究竟是什麼問題呢?你有關係,你可以獲取模型並獲得客戶的電子郵件。 – Bizley