Reputation: 478
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(['[email protected]'=>'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. I have also tried to do like $m->save(false)
but it didn't help me out.
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
As a quick update, I tried both print_r(Yii::$app->request->post())
and print_r($m->getErrors())
. Both are giving me and empty array Array()
Update 2
My table definition is as follows
Update 3
After running this 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
As per suggestion I have done the following
try {
if ($m->save(false)) {
Yii::$app->mailer->compose()
->setFrom(['[email protected]' => '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] => [email protected]
)
Any help would be highly appreciated.
Upvotes: 0
Views: 1042
Reputation: 478
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. Also, I have set all FK
fields to 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
Upvotes: 1