Skip to main content

How to check an already existing record in Magento?



I am trying to perform an SQL query kind of thing where I need to check if my database table contains a particular record or not. If it does, do nothing else add the record which isn't present.





My table sample contains fields like sample_id (auto-incremented), order_id, order_email_id, review_request, coupon_sent . It has got 4 records having order_ids (71, 74, 126, 165)





To obtain $ids, I have this query,







$to_date = date('Y-m-d H:i:s',strtotime('-3 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();

$sample = Mage::getModel('sample/sample')->getCollection()->addFieldToFilter('order_id', $id)->addFieldToSelect('order_id');

if($sample != null)

{echo "Record already exists";}

else

{

echo "Insert this record";

$sample->setOrderId($id);

}

}







My $id contains (71, 74, 126, 165, 166, 167)





So ideally the below query should check against existing records (71, 74, 126, 165) and insert new ones (166, 167) . I echoed the query and it returned right but it still echoed 'Record already exists' for ids 166 and 167 which ideally it shouldn't





Is there any other way to check the value of an existing field in the table in Magento?





Any help is appreciated. Thanks in advance


Comments

  1. Connect magento database :



    $conn = Mage::getSingleton('core/resource')->getConnection('core_read');

    // perform sql queries
    $result = $conn->fetchAll("SELECT * FROM sample;");
    foreach($result as $rows) {
    echo $rows['order_ids'] . "\n";
    }


    So, you can use a simple SQL expression with this way.

    ReplyDelete
  2. It can be done in a few steps using collection filtering.

    $wantedIds = $orders->getAllIds();
    $sample = Mage::getModel('sample/sample')->getCollection()
    ->addFieldToFilter('order_id', array('in' => $wantedIds)
    ->addFieldToSelect('order_id');
    $presentIds = $sample->getColumnValues('order_id');
    $newIds = array_diff($wantedIds, $presentIds);


    The final step gives you the IDs that still need to be created. It is easy to use them since they are an array.

    foreach ($newIds as $id) {
    Mage::getModel('sample/sample')
    ->setOrderId($id)
    ->save();
    }

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

CCNA 3 Final Exam => latest version

1 . Which security protocol or measure would provide the greatest protection for a wireless LAN? WPA2 cloaking SSIDs shared WEP key MAC address filtering   2 . Refer to the exhibit. All trunk links are operational and all VLANs are allowed on all trunk links. An ARP request is sent by computer 5. Which device or devices will receive this message? only computer 4 computer 3 and RTR-A computer 4 and RTR-A computer 1, computer 2, computer 4, and RTR-A computer 1, computer 2, computer 3, computer 4, and RTR-A all of the computers and the router   3 . Refer to the exhibit. Hosts A and B, connected to hub HB1, attempt to transmit a frame at the same time but a collision occurs. Which hosts will receive the collision jamming signal? only hosts A and B only hosts A, B, and C only hosts A, B, C, and D only hosts A, B, C, and E   4 . Refer to the exhibit. Router RA receives a packet with a source address of 192.168.1.65 and a destination address of 192.168.1.161...