我已经建立了这个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