使用WHERE子句更新Magento数据库表

时间:2012-01-09 13:46:27

标签: php zend-framework magento

我有一个名为'sample'的表,其字段为'sample_id,'order_id',order_email_id','review_request','coupon_sent'。

$to_date = date('Y-m-d H:i:s',strtotime('-1 days'));
        $orders = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('status', 'complete')->addFieldToFilter('updated_at',array('to' => $to_date ))->addAttributeToSelect('customer_email')->addAttributeToSelect('entity_id');
        foreach ($orders as $order) 
        {
            $email = $order->getCustomerEmail();
            $id = $order->getEntityId();
            //echo 'Email: ' . $email . ' Id: ' .$id .'<br/>' ;     
            $sample = Mage::getModel('sample/sample');
            $sample->setOrderId($id);
            $sample->setOrderEmailId($email);
            $sample->save();
            echo '<br/>Record Added';

        }

        $posts = Mage::getModel('sample/sample')->getCollection();
        foreach($posts as $sample_post)
        {
            if($sample_post->getReviewRequest()==0)
            {
                     code to send email to email id's from this table's order_email_id field
                    try
                    {
                       (if($mail->send() == true)
           { 
            echo "<br> Mail Sent ";
                       //**WHERE clause condition to update 'review_request' field for all successful emails sent**

                      }
                      else {echo 'Mail not Sent';}
                   }
                   catch(Exception e) {}

因此,根据我的代码,我如何设置WHERE子句,为成功发送电子邮件的所有记录设置review_request = 1。

提前致谢

2 个答案:

答案 0 :(得分:3)

感谢@clockworkgeek再次提供该链接并进行了低调评估,我进一步探究了解决方案。 :P我想我找到了它。

 if($mail->send() == true)
 { 
     echo "<br> Mail Sent ";
 $model1 = Mage::getModel('sample/sample')->getCollection();
            $model1->load()->addFieldToFilter('order_email_id',array('eq' => $toEmail))->getSelect());
foreach($model1 as $final_model)
{ 
       $final_model->setReviewRequest(1)->save();
    }
 }

这完美地更新了我的表格,其中包含发送电子邮件的所有记录,其中review_request = 1 谢谢。

答案 1 :(得分:2)

这对我不起作用:

$model1->load()->addFieldToFilter('order_email_id',array('eq' => $toEmail))->getSelect();

虽然这有效:

$model1->addFieldToFilter('order_email_id',array('eq' => $toEmail))->load();