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?

Java Urban Myths

Along the line of C++ Urban Myths and Perl Myths : What are the Java Urban Myths? That is, the ideas and conceptions about Java that are common but have no actual roots in reality . As a Java programmer, what ideas held by your fellow Java programmers have you had to disprove so often that you've come to believe they all learned at the feet of the same drunk old story-teller? Ideally, you would express these myths in a single sentence, and include an explanation of why they are false.