数据未保存到数据库中yii2

时间:2018-02-08 10:42:19

标签: php yii2 yii2-basic-app yii2-model

我正在尝试将一些数据插入到我的一个表中。但是我无法保存它。

public static function setupEmail($ref_no,$customer_id,$install_id)
{
    $sql = "SELECT DISTINCT es.`id` AS 'setup_id',es.`user_id` AS 'user_id',u.`email` AS 'email', es.`circle_code` AS 'circle_code',
es.`circle_name` AS 'circle_name',es.`email_group` AS 'email_group',ins.`meter_msn` AS 'msn', ins.`istallation_status` AS 'install_stat'
,ins.`comm_status` AS 'comm_stat'
FROM `email_setup` es
INNER JOIN `survey_hesco_subdivision` sd ON es.`circle_code` = sd.`circle_code`
INNER JOIN `survey` sur ON sd.`sub_div_code` = sur.`sub_division`
INNER JOIN `user` u ON es.`user_id` = u.`id` 
INNER JOIN `installations` ins ON sur.`customer_id` = ins.`customer_id`
WHERE sur.`customer_id` = '$customer_id'";

    $result = Yii::$app->db->createCommand($sql)->queryAll();

    foreach ($result as $result_set)
    {
       $email_setup_id = $result_set['setup_id'];
       $msn = $result_set['msn'];
       $email_to = $result_set['email'];
       $install_status = $result_set['install_stat'];
       $communication_stat = $result_set['comm_stat'];

       $m = new EmailTransaction;
       $m -> load(Yii::$app->request->post());
       $m->email_setup_id = $email_setup_id;
       $m->install_id = $install_id;
       $m->ref_no = $ref_no;
       $m->meter_msn = $msn;
       $m->email_to = $email_to;
       $m->email_datetime = date('Y-m-d H:i:s');
        try{
            if($m->save())
            {
                Yii::$app->mailer->compose()
                    ->setFrom(['xxx@xx.com'=>'Inventory Admin'])
                    ->setTo($email_to)
                    ->setSubject('New Installation')
                    ->setTextBody('The Meter# '.$msn.' against Reference# '.$ref_no.' is '.$communication_stat.' and '.$install_status)
                    ->setHtmlBody('<b>HTML content</b>')
                    ->send();
            }
        }
        catch (Exception $ex)
        {
            return ['status' => 'ERROR', 'message' => $ex->getMessage()];
        }

    }


}

我试图调试它,但它没有进入$m->save()部分。我也试过像$m->save(false)这样做,但它没有帮助我。

我无法发送数据和电子邮件。以下是型号代码

 public function rules()
{
    return [
        [['email_setup_id', 'install_id'], 'integer'],
        [['email_datetime'], 'safe'],
        [['meter_msn','email_to'], 'string', 'max' => 200],
        [['ref_no'], 'string', 'max' => 50],
        [['install_id'], 'exist', 'skipOnError' => true, 'targetClass' => Installations::className(), 'targetAttribute' => ['install_id' => 'id']],
        [['meter_msn'], 'exist', 'skipOnError' => true, 'targetClass' => Meters::className(), 'targetAttribute' => ['meter_msn' => 'meter_msn']],
        [['ref_no'], 'exist', 'skipOnError' => true, 'targetClass' => RefNumbers::className(), 'targetAttribute' => ['ref_no' => 'ref_no']],
    ];
}

更新1

作为快速更新,我尝试了print_r(Yii::$app->request->post())print_r($m->getErrors())。两者都给我和空数组Array()

更新2

我的表定义如下

enter image description here

enter image description here

更新3

运行此SHOW CREATE TABLE email_transaction后。以下是我得到的内容

enter image description here

全文

CREATE TABLE `email_transaction` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`email_setup_id` int(11) DEFAULT NULL,
`install_id` int(11) DEFAULT NULL,
`meter_msn` varchar(200) DEFAULT NULL,
`ref_no` varchar(50) DEFAULT NULL,
`email_datetime` datetime DEFAULT NULL,
`email_to` varchar(200) DEFAULT NULL,
 PRIMARY KEY (`id`),
 KEY `FK_REF_NO` (`ref_no`),
 KEY `FK_METER_MSN` (`meter_msn`),
 KEY `FK_INS_ID` (`install_id`),
 CONSTRAINT `FK_INS_ID` FOREIGN KEY (`install_id`) REFERENCES `installations` (`id`),
 CONSTRAINT `FK_METER_MSN` FOREIGN KEY (`meter_msn`) REFERENCES `meters` (`meter_msn`),
 CONSTRAINT `FK_REF_NO` FOREIGN KEY (`ref_no`) REFERENCES `ref_numbers` (`ref_no`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1

更新3

根据建议我做了以下

 try {
            if ($m->save(false)) {
                Yii::$app->mailer->compose()
                    ->setFrom(['faisal.qayyum@accurate.com.pk' => 'Inventory Admin'])
                    ->setTo($email_to)
                    ->setSubject('New Installation')
                    ->setTextBody('Salam')
                    ->setHtmlBody('The Meter# ' . $msn . ' against Reference# ' . $ref_no . ' is ' . $communication_stat . ' and ' . $install_status)
                    ->send();
            } else {
                //throw new \Exception('error');;
                echo "No Email Sent";
            }
        }catch(Exception $ex)
        {
            print_r($m->attributes); return;
        }

输出

Array
(
[id] => 
[email_setup_id] => 1
[install_id] => 1
[meter_msn] => 002999000071
[ref_no] => 20371110314300U
[email_datetime] => 2018-02-09 07:00:18
[email_to] => aaa.aa@aa.com
)

任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:1)

所以,我能够解决我的问题。我所做的事情在下面

分配给我的表格列sub test selection.MoveDown unit:=wdLine, count:=2 selection.EndKey unit:=wdLine, Extend:=wdExtend selection.Cut selection.MoveDown unit:=wdLine selection.Paste end sub 的{​​{1}}来自不同的表格,所以首先,我删除了所有foreign keys,然后再次从同一个表中添加它们。另外,我已将所有email_transaction字段设置为FK's

enter image description here

在服务器端,我删除了模型并再次重新生成它。插入所有记录并成功插入。

FK

它确实适合我。希望它能帮助一些人