2012-08-28 48 views
0

我有一個客戶希望獲得合作伙伴提供的一系列產品,但他們需要批准更改。保留合作伙伴變更歷史的最佳方法是什麼?使用MongoDB獲得批准/正在申請

因此,這裏是如何工作的

{ 
partner_id: {here} 
offer1: 
offer2: 
offer3: 
date: 
status: 
} 

我需要一種方法來檢查是否有任何報價變化(但只有1回),如果改變插入,使過去一個作爲狀態:歷史

有隻應該永遠是以下狀態:

歷史
待定
批准

但是如果他們插入新的報價,但有一個批准的報價,那麼它需要保持批准狀態,直到我們批准爲止。

我該怎麼做?這是我想:

<?php 

    function checkoffers($data) 
    { 
    $this->data = $data; 

    $collection = $this->db->partner_offers; 
    if($this->data["_id"] != null) 
    { 
     $cursor = $collection->insert(RUN INSERT ARRAY) 
    } 
    else 
    { 
     $cursor = $collection->find(array("_id"=>new MongoId($this->data["_id"]))); 

     if ($cursor->count() > 0) 
     { 
      $test = array(); 
      while($cursor->hasNext()) { 
       $test[] = ($cursor->getNext()); 
      } 


        foreach($test as $offers) 
        { 
         check to see if offer matches the past offer. if not then run new function and insert. 
} 


       } 


    } 

?> 

回答

0

一種方法是,每個報價文件組織架構:

{ 
    partner_id: 123, 
    offer: 'Even better than the last one', 
    date: ISODate("2012-08-28T10:00"), 
    status: 'pending' 
} 
{ 
    partner_id: 123, 
    offer: 'The best of the best', 
    date: ISODate("2012-08-28T09:00"), 
    status: 'approved' 
} 
{ 
    partner_id: 123, 
    offer: 'An offer you cannot refuse', 
    date: ISODate("2012-08-28T08:00"), 
    status: 'approved' 
} 

然後你輕鬆找到最新批准或待要約狀態和日期排序:

> db.partner.find({partner_id:123, status:'approved'}).sort({date:-1}).limit(1) 

{ 
    "_id" : ObjectId("503c6c8be16d4a06b6f0da17"), 
    "partner_id" : 123, 
    "offer" : "The best of the best", 
    "date" : ISODate("2012-08-28T09:00:00Z"), 
    "status" : "approved" 
}