2013-05-09 40 views
0

我有以下代碼:Magento的資源模型不能正常工作

$recipients = Mage::getResourceModel('crm/crm_collection'); 
      $recipients->getSelect() 
        ->joinInner(array(
         'link' => $recipients->getTable('crm/bulkMailLink'), 
        ), "link.crm_id = e.entity_id", array(
         'link_id' => 'link.id', 
        )) 
        ->where("link.queue_id = ? AND link.sent_at IS NULL", $queue->getId()); 
      $recipients->addAttributeToSelect('title'); 
      $recipients->addAttributeToSelect('first_name'); 
      $recipients->addAttributeToSelect('chinese_name'); 
      $recipients->addAttributeToSelect('last_name'); 
      $recipients->addAttributeToSelect('email1'); 
      $recipients->addAttributeToFilter('email1', array('neq'=>'')); 
      $recipients->setPageSize(100); 
      $recipients->setCurPage(1); 

我再登錄select語句的代碼產生:

Mage::log("DEBUG: ".((string)$recipients->getSelect())); 

上述方法產生,在完全執行工作的SQL查詢phpmyadmin並返回我期望的結果。

我再登錄計數的$收件人

Mage::log("Loading recipients for queue: {$recipients->count()}"); 

這是代碼結束的地方。實際上它甚至不會記錄消息。如果我註釋掉上面的日誌代碼,並嘗試做一個

foreach ($recipients as $crm) 
{ 
    var_dump($crm); 
    die(); 
} 

它不會進入的foreach。解析器將在foreach之前停止。

最糟糕的是沒有打印出錯信息。我被嚴重卡住了。

謝謝

回答

1

無論是count()foreach操作觸發採集實際load(),這意味着執行查詢,結果集是牽強,並且每個結果被試圖上的實例作爲_data該集合的模型類。項目實現後,收集類嘗試通過addItem()將其添加到其內部存儲。

我懷疑你的集合顯式拋出異常,因爲結果集中有重複的主鍵;邏輯請參見Varien_Data_Collection::addItem()。暫時對此進行評論以進行測試。

要解決,請在您的集合類中本地更改您的查詢或覆蓋addItem()

+0

你先生,是個天才!非常感謝。 – Jason246 2013-05-10 13:49:58