Changeset 14789

Show
Ignore:
Timestamp:
02/07/08 15:27:58 (10 months ago)
Author:
bergie
Message:

Fix child reflection

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/midcom/midcom.helper.reflector/reflector_tree.php

    r14784 r14789  
    539539        $this->_sg_constraints($qb); 
    540540 
    541         // Figure out constraint to use to get child objects 
    542         // TODO: Review this code 
     541        // Figure out constraint(s) to use to get child objects 
    543542        $ref = new midgard_reflection_property($schema_type); 
    544         $upfield = midgard_object_class::get_property_up($schema_type); 
     543 
     544        $multiple_links = false;         
     545        $linkfields = array(); 
     546        $linkfields['up'] = midgard_object_class::get_property_up($schema_type); 
     547        $linkfields['parent'] = midgard_object_class::get_property_parent($schema_type); 
    545548         
    546         if (   !empty($upfield) 
    547             && !empty($parentfield)) 
    548         { 
     549        foreach ($linkfields as $link_type => $field) 
     550        { 
     551            if (empty($field)) 
     552            { 
     553                // No such field for the object 
     554                unset($linkfields[$link_type]); 
     555                continue; 
     556            } 
     557             
     558            $linked_class = $ref->get_link_name($field); 
     559            if (!is_a($for_object, $linked_class)) 
     560            { 
     561                // This link points elsewhere 
     562                unset($linkfields[$link_type]); 
     563                continue; 
     564            } 
     565        } 
     566         
     567        if (count($linkfields) > 1) 
     568        { 
     569            $multiple_links = true; 
    549570            $qb->begin_group('OR'); 
    550571        } 
    551572 
    552         if (!empty($upfield)) 
    553         { 
    554             $uptype = $ref->get_midgard_type($upfield); 
    555             $uptarget = $ref->get_link_target($upfield); 
    556  
    557             if (!isset($for_object->$uptarget)) 
    558             { 
    559                 $qb->end_group(); 
     573        foreach ($linkfields as $link_type => $field) 
     574        { 
     575            $field_type = $ref->get_midgard_type($field); 
     576            $field_target = $ref->get_link_target($field); 
     577                 
     578            if (   !$field_target 
     579                || !isset($for_object->$field_target)) 
     580            { 
     581                if ($multiple_links) 
     582                { 
     583                    $qb->end_group(); 
     584                } 
    560585                return false; 
    561586            } 
    562             switch ($uptype) 
     587            switch ($field_type) 
    563588            { 
    564589                case MGD_TYPE_STRING: 
    565590                case MGD_TYPE_GUID: 
    566                     $qb->add_constraint($upfield, '=', (string)$for_object->$uptarget); 
     591                    $qb->add_constraint($field, '=', (string) $for_object->$field_target); 
    567592                    break; 
    568593                case MGD_TYPE_INT: 
    569594                case MGD_TYPE_UINT: 
    570                     $qb->add_constraint($upfield, '=', (int)$for_object->$uptarget); 
     595                    if ($link_type == 'up') 
     596                    { 
     597                        $qb->add_constraint($field, '=', (int) $for_object->$field_target); 
     598                    } 
     599                    else 
     600                    { 
     601                        $qb->begin_group('AND'); 
     602                            $qb->add_constraint($field, '=', (int) $for_object->$field_target); 
     603                            // make sure we don't accidentally find other objects with the same id 
     604                            $qb->add_constraint($field . '.guid', '=', (string) $for_object->guid); 
     605                        $qb->end_group(); 
     606                    } 
    571607                    break; 
    572608                default: 
    573609                    debug_push_class(__CLASS__, __FUNCTION__); 
    574                     debug_add("Do not know how to handle upfield '{$upfield}' has type {$uptype}", MIDCOM_LOG_ERROR); 
     610                    debug_add("Do not know how to handle linked field '{$field}', has type {$field_type}", MIDCOM_LOG_INFO); 
    575611                    debug_pop(); 
    576                     if (   !empty($upfield) 
    577                         && !empty($parentfield)) 
     612                    if ($multiple_links) 
    578613                    { 
    579614                        $qb->end_group(); 
     
    582617            } 
    583618        } 
    584         $parentfield = midgard_object_class::get_property_parent($schema_type); 
    585         if (!empty($parentfield)) 
    586         { 
    587             $parenttype = $ref->get_midgard_type($parentfield); 
    588             $parenttarget = $ref->get_link_target($parentfield); 
    589             switch ($parenttype) 
    590             { 
    591                 case MGD_TYPE_STRING: 
    592                 case MGD_TYPE_GUID: 
    593                     $qb->add_constraint($parentfield, '=', (string)$for_object->$parenttarget); 
    594                     break; 
    595                 case MGD_TYPE_INT: 
    596                 case MGD_TYPE_UINT: 
    597                         $qb->begin_group('AND'); 
    598                             $qb->add_constraint($parentfield, '=', (int)$for_object->$parenttarget); 
    599                             // make sure we don't accidentally find other objects with the same id 
    600                             $qb->add_constraint($parentfield . '.guid', '=', (string) $for_object->guid); 
    601                         $qb->end_group(); 
    602                     break; 
    603                 default: 
    604                     debug_push_class(__CLASS__, __FUNCTION__); 
    605                     debug_add("Do not know how to handle parentfield '{$parentfield}' has type {$parenttype}", MIDCOM_LOG_INFO); 
    606                     debug_pop(); 
    607                     if (   !empty($upfield) 
    608                         && !empty($parentfield)) 
    609                     { 
    610                         $qb->end_group(); 
    611                     } 
    612                     return false; 
    613             } 
    614         } 
    615619         
    616         if (   !empty($upfield) 
    617             && !empty($parentfield)) 
     620        if ($multiple_links) 
    618621        { 
    619622            $qb->end_group(); 
    620623        } 
    621         // TODO: /Review this code 
    622624 
    623625        return $qb;