简体   繁体   English

数据未保存到数据库yii2

[英]Data not saved into database yii2

I am trying to insert some data into one of my tables. 我正在尝试将一些数据插入到我的一个表中。 But I am unable to save it. 但是我无法保存。

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()];
        }

    }


}

I have tried to debug it and it doesn't get into the $m->save() part. 我尝试调试它,但它没有进入$m->save()部分。 I have also tried to do like $m->save(false) but it didn't help me out. 我也尝试过像$m->save(false)一样做,但是它没有帮助我。

I am unable to send data and email. 我无法发送数据和电子邮件。 Below is the model code 下面是型号代码

 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']],
    ];
}

Update 1 更新1

As a quick update, I tried both print_r(Yii::$app->request->post()) and print_r($m->getErrors()) . 作为快速更新,我尝试了print_r(Yii::$app->request->post())print_r($m->getErrors()) Both are giving me and empty array Array() 都给我和空数组Array()

Update 2 更新2

My table definition is as follows 我的表定义如下

在此处输入图片说明

在此处输入图片说明

Update 3 更新3

After running this SHOW CREATE TABLE email_transaction . 运行此SHOW CREATE TABLE email_transaction Below is what I get 以下是我得到的

在此处输入图片说明

The full text is 全文是

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

Update 3 更新3

As per suggestion I have done the following 根据建议,我做了以下工作

 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;
        }

The output is 输出是

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
)

Any help would be highly appreciated. 任何帮助将不胜感激。

So, I am able to solve my problem. 因此,我能够解决我的问题。 The things I have done is below 我所做的事情如下

The foreign keys that were assigned to my table column email_transaction were from different tables so first of all, I deleted all my FK's and then again added them again from the same table. 分配给我的表列email_transactionforeign keys来自不同的表,因此,首先,我删除了所有FK's ,然后再次从同一表中添加了它们。 Also, I have set all FK fields to NOT NULL . 另外,我将所有FK字段都设置为NOT NULL

在此处输入图片说明

At the server side, I have deleted the model and again regenerated it. 在服务器端,我删除了模型,然后再次重新生成它。 Inserted all the records and they were successfully inserted. 插入所有记录,它们已成功插入。

 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(['sender_email' => 'Inventory Admin'])
                    ->setTo($email_to)
                    ->setCc("cc_email")
                    ->setSubject('New Installation')
                    ->setTextBody('hi')
                    ->setHtmlBody('The Meter# <b> '  . $msn . '</b>'. ' against Reference# <b> ' . $ref_no . '</b>'.' is <b> ' . $communication_stat . '</b>'. ' and <b> ' . $install_status)
                    ->send();
                echo "email sent";
            } else {
                //throw new \Exception('error');;
                echo "No Email Sent";
            }
        }catch(Exception $ex)
        {
            print_r($m->attributes); return;
        }


    }

It does workes for me. 它确实为我工作。 Hope that it will help some one 希望对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM