2012-12-05 86 views
0

我想過濾一個eZPublish提取。從多個對象ID過濾

我有一個有對象關係屬性的類。我的過濾器允許獲取由對象關係屬性過濾的此類的對象。

我已經開始使用增強對象關係篩選擴展(http://projects.ez.no/index.php/enhanced_object_relation_filter),但是這個只適用於「AND」條件,我想要「或」。

我可以編輯該文件以添加「OR」的邏輯,這是我得到了什麼:

<?php 
class EORExtendedFilter 
{ 

    function CreateSqlParts($params) 
    { 
     $db =& eZDB::instance(); 

     $tables = array(); 
     $joins = array(); 

     // foreach filtered attribute, we add a join the relation table and filter 
     // on the attribute ID + object ID 
     foreach($params as $param) 
     { 
      if (!is_array($param)) 
       continue; 

      if (!is_numeric($param[0])) 
      { 
       $classAttributeId = eZContentObjectTreeNode::classAttributeIDByIdentifier($param[0]); 
      } 
      else 
      { 
       $classAttributeId = $param[0]; 
      } 

      // multiple objects ids 
      if (is_array($param[1])) 
      { 
       foreach($param[1] as $objectId) 
       { 
        $join = array(); // 'OR' logic 

        if (is_numeric($objectId)) 
        { 
         $tableName = 'eor_link_' . $objectId; 
         $tables[] = 'ezcontentobject_link ' . $tableName; 

         $join[] = $tableName . '.from_contentobject_id = ezcontentobject.id'; 
         $join[] = $tableName . '.from_contentobject_version = ezcontentobject.current_version'; 
         $join[] = $tableName . '.contentclassattribute_id = ' . $classAttributeId; 
         $join[] = $tableName . '.to_contentobject_id = ' . $objectId; 
        } 

        // 'OR' logic 
        $joins[] = $join; 

       } 
      } 
      // single object id 
      else 
      { 
       $objectId = $param[1]; 

       $tableName = 'eor_link_' . $objectId; 
       $tables[] = 'ezcontentobject_link ' . $tableName; 

       $joins[] = $tableName . '.from_contentobject_id = ezcontentobject.id'; 
       $joins[] = $tableName . '.from_contentobject_version = ezcontentobject.current_version'; 
       $joins[] = $tableName . '.contentclassattribute_id = ' . $classAttributeId; 
       $joins[] = $tableName . '.to_contentobject_id = ' . $objectId; 
      } 
     } 

     if (!count($tables) or !count($joins)) 
     { 
      $tables = $joins = ''; 
     } 
     else 
     { 
      $tables = "\n, " . implode("\n, ", $tables); 

      // 'OR' logic 
      if (is_array($param[1])) 
      { 
       $andClauses = array(); 
       foreach ($joins as $attr) 
       { 
        $andClauses[] = implode(" AND\n ", $attr); 
       } 

       $joins = implode(" OR\n ", $andClauses) . " AND\n ";    
      } 
      else 
      { 
       $joins = implode(" AND\n ", $joins) . " AND\n "; 
      } 

     } 

     return array('tables' => $tables, 'joins' => $joins); 
    } 
} 

此擴展屬性過濾器生成字符串:

eor_link_126.from_contentobject_id = ezcontentobject。 id AND eor_link_126.from_contentobject_version = ezcontentobject.current_version AND eor_link_126.contentclassattribute_id = 537 AND eor_link_126.to_contentobject_id = 126 OR(eor_link_127.from_contentobject_id = ezcontentobject.id AND eor_link_127.from_contentobject_version = ezcontentobject.current_version AND eor_link_127.contentclassattribute_id = 537 AND eor_link_127.to_contentobject_id = 127 AND

我知道它遺漏了一些括號,所以我編輯了字符串以直接在phpMyAdmin中測試它。這是查詢:

SELECT DISTINCT 
         ezcontentobject.*, 
         ezcontentobject_tree.*, 
         ezcontentclass.serialized_name_list as class_serialized_name_list, 
         ezcontentclass.identifier as class_identifier, 
         ezcontentclass.is_container as is_container 

         , ezcontentobject_name.name as name, ezcontentobject_name.real_translation 
         , a0.sort_key_int 

        FROM 
         ezcontentobject_tree, 
         ezcontentobject,ezcontentclass 
         , ezcontentobject_name 
         , ezcontentobject_attribute a0 


, ezcontentobject_link eor_link_126 
, ezcontentobject_link eor_link_127 

        WHERE 
         ezcontentobject_tree.parent_node_id = 1443 and 
         ((eor_link_126.from_contentobject_id = ezcontentobject.id AND 
eor_link_126.from_contentobject_version = ezcontentobject.current_version AND 
eor_link_126.contentclassattribute_id = 537 AND 
eor_link_126.to_contentobject_id = 126) OR 
(eor_link_127.from_contentobject_id = ezcontentobject.id AND 
eor_link_127.from_contentobject_version = ezcontentobject.current_version AND 
eor_link_127.contentclassattribute_id = 537 AND 
eor_link_127.to_contentobject_id = 127)) AND 


            a0.contentobject_id = ezcontentobject.id AND 
            a0.contentclassattribute_id = 191 AND 
            a0.version = ezcontentobject_name.content_version AND 
(a0.language_id & ezcontentobject.language_mask > 0 AND 
    (( ezcontentobject.language_mask - (ezcontentobject.language_mask & a0.language_id)) & 1) 
    + (((ezcontentobject.language_mask - (ezcontentobject.language_mask & a0.language_id)) & 2)) 
    < 
    (a0.language_id & 1) 
    + ((a0.language_id & 2)) 
) 
AND 

         ezcontentclass.version=0 AND 

         ezcontentobject_tree.contentobject_id = ezcontentobject.id AND 
         ezcontentclass.id = ezcontentobject.contentclass_id AND 

         ezcontentobject.contentclass_id IN (17) AND 
         ezcontentobject_tree.contentobject_id = ezcontentobject_name.contentobject_id and 
            ezcontentobject_tree.contentobject_version = ezcontentobject_name.content_version and 
(ezcontentobject_name.language_id & ezcontentobject.language_mask > 0 AND 
    (( ezcontentobject.language_mask - (ezcontentobject.language_mask & ezcontentobject_name.language_id)) & 1) 
    + (((ezcontentobject.language_mask - (ezcontentobject.language_mask & ezcontentobject_name.language_id)) & 2)) 
    < 
    (ezcontentobject_name.language_id & 1) 
    + ((ezcontentobject_name.language_id & 2)) 
) 

         AND ezcontentobject_tree.is_invisible = 0 


         AND 
ezcontentobject.language_mask & 3 > 0 

       ORDER BY a0.sort_key_int DESC 
LIMIT 0, 10 

事情是,當我執行上面的查詢,沒有什麼會返回,因爲查詢永遠不會停止。 CPU達到100%,我不得不重新啓動mysql,這意味着它不是一個語法問題。

下面是該查詢的解釋:

MySQL explain

如果任何人有這個任何線索,將是非常有益的。

回答

0

我已經通過使用eZPersistentObject::fetchObjectList()重寫所有內容來解決它。