简体   繁体   中英

find query for many to many relation propel

i am new for propel . i face a problem to fetch all record form both table which have many to many relation .

i have a user and group table. and a join table user_group.

user and group have many to many relation

i and using follow method to find all related data in a single query .

schema.xml file

<table name="user" phpName="User" idMethod="native">
    <column name="id" type="INTEGER" primaryKey="true" autoIncrement="true" required="true" />
    <column name="name" type="VARCHAR" size="100" required="true" />
    <column name="email" type="VARCHAR" size="100" required="true" />
    <column name="age" type="INTEGER" required="true" />
    <column name="gender" type="VARCHAR" size="50" required="true" />
    <column name="company" type="VARCHAR" size="100" required="true" />
    <column name="address" type="VARCHAR" size="100" required="true" />
    <column name="country" type="VARCHAR" size="100" required="true" />
    <column name="mobileno" type="DOUBLE" required="true" />
    <column name="comment_about" type="VARCHAR" size="200" required="true" />
    <foreign-key foreignTable="post" name="post" phpName="postWriter">
        <reference local="id" foreign="user_id" />
    </foreign-key>
</table>

<table name="group">
    <column name="id" type="INTEGER" primaryKey="true" autoIncrement="true" />
    <column name="name" type="VARCHAR" size="32" />
</table>


<table name="user_group" isCrossRef="true">
    <column name="user_id" type="INTEGER" primaryKey="true" />
    <column name="group_id" type="INTEGER" primaryKey="true" />
    <foreign-key foreignTable="user">
        <reference local="user_id" foreign="id" />
    </foreign-key>
    <foreign-key foreignTable="group">
        <reference local="group_id" foreign="id" />
    </foreign-key>
</table>

and and i try to find related data like this

$userObj = UserQuery::create()
       ->join('Group')
       ->find();

but above query give me a fatal error

Fatal error: Uncaught exception 'PropelException' with message 'Unable to execute SELECT statement [SELECT user.id, user.name, user.email, user.age, user.gender, user.company, user.address, user.country, user.mobileno, user.comment_about FROM `user` INNER JOIN `` ON ()  

please help me Ho we can solve this.

You cannot join a many-to-many relation directly since there is no such thing in MySQL. You can do one of two things...

If you have a User object already, then you can simply "get" the related Groups this way:

$relatedGroups = $userObject->getGroups();

If you don't have a User object yet, and you want to populate all records (users and groups), then I think you could do this:

$users = UserQuery::create()
           ->join('User.UserGroup')
           ->join('UserGroup.Group')
           ->with('Group')  // this line hydrates Group objects as well as Users
           ->find();

Now in your code you can loop through User s and get each one's Group s without an extra DB hit:

foreach ($users as $user) {
  $user->getGroups();
  // normally this would be an extra DB hit, but because we used "->with('Group')"
  // above in the query, the objects are already hydrated.
}

Hope this helps. There is info on minimizing queries using "with()" on the Propel site as well info on many-to-many relationships (with query examples).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM