title: How to check an already existing record in Magento?
body: <p>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.</p>
  
  <p>My table sample contains fields like <code>sample_id (auto-incremented), order_id, order_email_id, review_request, coupon_sent</code>. It has got 4 records having <code>order_ids (71, 74, 126, 165)</code></p>
  
  <p>To obtain $ids, I have this query,</p>
  
  <pre><code>$to_date = date('Y-m-d H:i:s',strtotime('-3 days'));
  $orders = Mage::getModel('sales/order')-&gt;getCollection()-&gt;addFieldToFilter('status', 'complete')-&gt;addFieldToFilter('updated_at',array('to' =&gt; $to_date ))-&gt;addAttributeToSelect('customer_email')-&gt;addAttributeToSelect('entity_id');
  foreach($orders as $order)
  {
    $email = $order-&gt;getCustomerEmail();
    $id = $order-&gt;getEntityId();
    $sample = Mage::getModel('sample/sample')-&gt;getCollection()-&gt;addFieldToFilter('order_id', $id)-&gt;addFieldToSelect('order_id');
    if($sample != null)
    {echo "Record already exists";}
    else
    { 
       echo "Insert this record";
       $sample-&gt;setOrderId($id);
    } 
  }
  </code></pre>
  
  <p>My <code>$id</code> contains <code>(71, 74, 126, 165, 166, 167)</code></p>
  
  <p>So ideally the below query should check against existing records <code>(71, 74, 126, 165)</code> and insert new ones <code>(166, 167)</code>. 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</p>
  
  <p>Is there any other way to check the value of an existing field in the table in Magento?</p>
  
  <p>Any help is appreciated. Thanks in advance</p>