elgg_entity_relationships sql problem

Hi

I have product and bid entities. The product entity can have multiple bid entities. In the relationship table its like this:

guid_one                              relationship                             guid_two

[a prouct guid]                     product_bid                             [a bid guid]

 

I can get all the bids a user has placed on product by doing

elgg_get_entities_from_relationship(array(

                  'relationship' => 'product_bid'
                  'owner_guid' => [a owner guid]
                  'limit' => 999));

This is fine as the owner guid is checked on the guid_two.

Now for showing all bids placed on a users products, im running into trouble. I need to return all GUIDs from guid_two column of elgg_entity_relationships table which have relationship of 'product_bid' and the owner_guid of guid_one is the user id.

So owner_id is tested on the owner of the guid in column guid_one, not column guid-two.

Any advice on how I can do this?

Thanks

Mark

  • @Mark: in your elgg_get_entities_from_relationship call add the following:

    'inverse_relationship' => TRUE,

    It runs the call in reverse and gets all the other entities.

  • Hi Trajan

    The inverse_relationship parameter will return entities from the first column of relationship table. I still need the entities from the second column, but with owner_guid tested on the entities in the first coumn...

     

  • Ah I see what about this?

    $my_bids = array();

    $products = elgg_get_entities(array('type' => 'object', 'subtype' => 'product', 'owner_guid' => $guid, 'limit' => 0));

    foreach($products as $product){

    $bids = elgg_get_entities_from_relationship(array('relationship' => 'product_bid', 'relationship_guid' => $product->guid, 'type' => 'object', 'subtype' => 'bid', 'limit' => 0));

    foreach($bids as $bid){

    if(!in_array($bid,$my_bids) && ($bid->owner_guid == $product->owner_guid)){

    $my_bids[] = $bid;

    }

    }

    }

    echo elgg_view_entity_list ($my_bids, $offset=0, $limit=10, $full_view=false, $list_type_toggle=true, $pagination=true);

     

    This would do the following:

    Get all of User A's products, find bids on each one and check if the owner of the bid is the same as the owner of the product. If the owners match, the bid is pushed to an array which is finally echoed as a list.

    Is that what you meant?

     

  • Hi

    Yep that will do it. What I would prefer though is to get it all in one query because Im concentrating now on optimizing the site. What im thinking is have the bid container_guid as the guid of the product then basing a new function on elgg_get_entities_from_relationship() and including the following self-join:

    LEFT OUTER JOIN elgg_entities AS ee ON e.guid = ee.container_guid

    I think that will return the correct results and in an optimized fashion. am trying it now

  • Trajan, im going to do it your way for now. I cannot figure out the sql to get this back in with one DB call.

    I think it would be a useful function, to allow owner_guid be tested on the guids from guid_one whilst still returning the entities from guid_two. Im struggling with the sql required though

  • actually, my fault, i didnt explore elgg_get_entities_from_relationship() enough. It allows for a additional where clauses. So including below option returns what im looking for

            $options['wheres'] = "r.guid_one IN (select ee.guid from elgg_entities ee where ee.subtype=9 AND ee.owner_guid=".$product_owner_id.")";

  • excellent, so you got it working?

  • Yep, its working using the new 'where', thanks for help:)