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

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex