[dev] Horde_Rdo composite key needs

Elier Delgado elier.delgado at gmail.com
Tue Aug 3 06:57:39 UTC 2010


> Hello everyone,
> I'm using Horde_Rdo and the moment to use a composite keys arose !
> Are anyone working on that? If not, can anyone instruct me to do it?
> Can anyone suggest any workaround while?

Hi chuck,

Working on this, initially in composites foreign keys problem,
I gave a solution that is working for me, please check it and let us know.

You suppose the following hypothetical example, we have 2 entities
types: entity1 and entity2 and eachone can perform orders.
In orders table we can have order_id, entity_id, entity_type, etc ..

In the actual version you must define in orders relationship:
'foreignKey' => 'entity_id',
and when you try to do entity1->orders you will get all orders without
evaluate the condition entity_type='entity1'

A workaround it's implement getOrders() method putting the conditions
manually by $query->addTest(),
I don't know if that is enough for you.

Anyway, I tried this and it works:

I put an list of fields to match primary keys with foreign keys
considering that theirs names can differs,
and a values entry to put fixed values like types:

'foreignKey' => array('fields'=>array('entity1_id'=>'entity_id'),
'values'=>array('entity_type'=>'entity1')),

So, in Rdo/Base.php when you try to access to relationships, I added


        case Horde_Rdo::ONE_TO_MANY:

            // processing composite foreign keys defined in relationships
            if (is_array($rel['foreignKey']))
            {
                $match_fields = $rel['foreignKey']['fields'];
                $match_values = $rel['foreignKey']['values'];

                $keys = array();
                foreach ($match_fields as $pkey=>$fkey)
                    $keys[$fkey] = $this->{$pkey};

                foreach ($match_values as $fkey=>$fkey_value)
                    $keys[$fkey] = $fkey_value;

                $this->_fields[$field] = $m->find(array('keys'=>$keys));
            }
            else
                $this->_fields[$field] =
$m->find(array($rel['foreignKey'] => $this->{$rel['foreignKey']}));
            break;



In Rdo/Mapper.php find() method I added:

           }elseif (isset($arg['keys'])) {
                $query = new Horde_Rdo_Query();
                $query->combineWith('AND');
                foreach ($arg['keys'] as $key => $value) {
                    $query->addTest($key, '=', $value);
                }


Any suggestion ?

Elier


More information about the dev mailing list