how to query many-to-many relationship in same table by redbeanphp?

Navid_pdp11

I have a table named "user" and this table has many to many relation over its own such as favorite action. this means a user can favorite multiple users and can be Favorited by multiple users.

now I want query over this relation and get every users those Favorite a spacial user.

I use this code :

$users = $agent->via($TB_Favorite,"agent_id=? and favorite.action=?", array($userId,FAVORITE_TYPE_USER_FAVORITE))->sharedUsers ;

I debug this code by profiling database and get this result:

        SELECT
            `users`.*  ,
            COALESCE(
            NULLIF(`favorite`.`users_id`, `users`.id),
            NULLIF(`favorite`.`users2_id`, `users`.id)) AS linked_by
        FROM `favorite`
        INNER JOIN `users` ON
        ( `users`.id = `favorite`.`users2_id` AND `favorite`.`users_id` IN ('13') ) OR
        ( `users`.id = `favorite`.`users_id` AND `favorite`.`users2_id` IN ('13') )

but in this case it must use favorite.agent_id instead favorite.users2_id how can i resolve this problem ?

this is my table diagram : enter image description here

may be this make my position clear.

Gabor de Mooij

Okay, thanks for the diagram and the additional explanation. So, you want to query the users associated with the favorite activities of a user. Here's one possible solution (tested on an empty db here):

$x = R::dispense(array(
'_type' => 'user',
'name' => 'Q',
'ownActivity' => array(
    array('_type'=>'favorite', 
         'name'=>'A', 
         'agent'=> array(
              '_type'=>'user', 
              'name'=>'X')),
    array('_type'=>'favorite', 
          'name'=>'B', 
          'agent'=> array(
              '_type'=>'user', 
              'name'=>'Y')),)));
R::store($x);
$me = R::findOne('user', ' `name` = ? ', array('Q'));
$favoriteActivities = $me->ownFavoriteList;
foreach($favoriteActivities as $activity) {
    $agent = $activity->fetchAs('user')->agent;
    $favoriteUsers[$agent->id] = $agent;
    echo $agent->name;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

query over many to many relationship in redbeanphp

From Dev

Many to many relationship in the same table?

From Dev

how to query a many to many relationship?

From Dev

How to query a pivot table data in MySQL (many to many relationship)

From Dev

One to many relationship on the same table

From Dev

many to many in redbeanphp is not full

From Dev

How to populate a many-to-many relationship as table

From Dev

How to map a double Many To One relationship to the same table in Doctrine 2?

From Dev

How to query a one to many/many to many relationship in Flask SQL Alchemy?

From Dev

Fetching one to many relationship from same table

From Dev

Laravel 4 how to set up many-to-many relationship with the same table

From Dev

EntityFramework CodeFirst: CASCADE DELETE for same table many-to-many relationship

From Dev

Many-to-many relationship between same table with extra column in EF

From Dev

How to get all books from another table when using many to many relationship Laravel / Eloquent / Query builder

From Dev

hibernate not locating junction table in query of many to many relationship

From Dev

Query for retrieving data from a bridging table of many to many relationship

From Dev

hibernate not locating junction table in query of many to many relationship

From Dev

Query many to many relationship with DetachedCriteria

From Dev

eloquent: query many to many relationship

From Dev

Recursive relationship on a many to many table

From Dev

SQL many to many relationship table

From Dev

Junction table/many to many relationship

From Dev

How to query one-to-many relationship in JPQL?

From Dev

How to query for many to many relationship between products and filters in MySQL?

From Dev

How to efficiently query many-to-many relationship on two columns

From Dev

how to write a Linq query with a EF code first Many to Many relationship

From Dev

One to many relationship table

From Dev

How can I reference an intersection table of a many-to-many relationship?

From Dev

MySQL - How to insert into table that has many-to-many relationship

Related Related

  1. 1

    query over many to many relationship in redbeanphp

  2. 2

    Many to many relationship in the same table?

  3. 3

    how to query a many to many relationship?

  4. 4

    How to query a pivot table data in MySQL (many to many relationship)

  5. 5

    One to many relationship on the same table

  6. 6

    many to many in redbeanphp is not full

  7. 7

    How to populate a many-to-many relationship as table

  8. 8

    How to map a double Many To One relationship to the same table in Doctrine 2?

  9. 9

    How to query a one to many/many to many relationship in Flask SQL Alchemy?

  10. 10

    Fetching one to many relationship from same table

  11. 11

    Laravel 4 how to set up many-to-many relationship with the same table

  12. 12

    EntityFramework CodeFirst: CASCADE DELETE for same table many-to-many relationship

  13. 13

    Many-to-many relationship between same table with extra column in EF

  14. 14

    How to get all books from another table when using many to many relationship Laravel / Eloquent / Query builder

  15. 15

    hibernate not locating junction table in query of many to many relationship

  16. 16

    Query for retrieving data from a bridging table of many to many relationship

  17. 17

    hibernate not locating junction table in query of many to many relationship

  18. 18

    Query many to many relationship with DetachedCriteria

  19. 19

    eloquent: query many to many relationship

  20. 20

    Recursive relationship on a many to many table

  21. 21

    SQL many to many relationship table

  22. 22

    Junction table/many to many relationship

  23. 23

    How to query one-to-many relationship in JPQL?

  24. 24

    How to query for many to many relationship between products and filters in MySQL?

  25. 25

    How to efficiently query many-to-many relationship on two columns

  26. 26

    how to write a Linq query with a EF code first Many to Many relationship

  27. 27

    One to many relationship table

  28. 28

    How can I reference an intersection table of a many-to-many relationship?

  29. 29

    MySQL - How to insert into table that has many-to-many relationship

HotTag

Archive