/**
  * - use relations to save lead products
  * - remove old crm products tables
  * 
  * @return void
  */
 public function update_2()
 {
     if (Setup_Controller::getInstance()->isInstalled('Crm')) {
         // get linked products
         $select = $this->_db->select()->from(SQL_TABLE_PREFIX . 'metacrm_leads_products');
         $stmt = $this->_db->query($select);
         $queryResult = $stmt->fetchAll();
         //if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . print_r($queryResult, TRUE));
         // insert values into relations table
         $relationsBackend = new Tinebase_Relation_Backend_Sql();
         foreach ($queryResult as $row) {
             $relation = new Tinebase_Model_Relation(array('own_model' => 'Crm_Model_Lead', 'own_backend' => 'Sql', 'own_id' => $row['lead_id'], 'own_degree' => Tinebase_Model_Relation::DEGREE_SIBLING, 'type' => 'PRODUCT', 'related_model' => 'Sales_Model_Product', 'related_backend' => 'Sql', 'related_id' => $row['product_id'], 'remark' => Zend_Json::encode(array('description' => $row['product_desc'], 'price' => $row['product_price'], 'quantity' => 1))));
             try {
                 $relationsBackend->addRelation($relation);
             } catch (Zend_Db_Statement_Exception $zdse) {
                 Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Found duplicate, increasing quantity (' . $zdse->getMessage() . ')');
                 // increase quantity
                 $updateRelation = $relationsBackend->search(new Tinebase_Model_RelationFilter(array(array('field' => 'own_id', 'operator' => 'equals', 'value' => $relation->own_id), array('field' => 'related_id', 'operator' => 'equals', 'value' => $relation->related_id), array('field' => 'related_model', 'operator' => 'equals', 'value' => 'Sales_Model_Product'))))->getFirstRecord();
                 $remark = $updateRelation->remark;
                 $remark['quantity'] = $remark['quantity'] + 1;
                 $updateRelation->remark = $remark;
                 //if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' ' . print_r($updateRelation->toArray(), TRUE));
                 $relationsBackend->updateRelation($updateRelation);
             }
         }
         // drop table metacrm_leadsproducts and metacrm_products
         $this->dropTable('metacrm_leads_products');
         $this->dropTable('metacrm_products');
     }
     $this->setApplicationVersion('Sales', '2.3');
 }
 /**
  * get list of relations
  *
  * @param Tinebase_Model_Filter_FilterGroup|optional $_filter
  * @param Tinebase_Model_Pagination|optional $_pagination
  * @param boolean $_onlyIds
  * @return Tinebase_Record_RecordSet|array
  */
 public function search(Tinebase_Model_Filter_FilterGroup $_filter = NULL, Tinebase_Record_Interface $_pagination = NULL, $_onlyIds = FALSE)
 {
     return $this->_backend->search($_filter, $_pagination, $_onlyIds);
 }