我是一个推进新手,遇到了一个与推进多对多关系相关的问题。我有两个表。用户:
<table name="users" phpName="User">
<!-- column and foreign key definitions go here -->
<column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true"/>
<column name="username" type="varchar" size="50" required="true" />
<column name="password" type="varchar" size="256" required="true" />
<unique>
<unique-column name="username"/>
</unique>
<index>
<index-column name="username"/>
</index>
<behavior name="timestampable"/></table>
公司:
<table name="companies" phpName="Company">
<!-- column and foreign key definitions go here -->
<column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true"/>
<column name="name" type="varchar" size="256" required="true" />
<behavior name="timestampable"/></table>
这是桥牌桌
<table name="users_companies" isCrossRef="true">
<!-- column and foreign key definitions go here -->
<column name="user_id" type="integer" primaryKey="true" />
<column name="company_id" type="integer" primaryKey="true" />
<foreign-key foreignTable = "companies" phpName="Company">
<reference local="company_id" foreign="id"/>
</foreign-key>
<foreign-key foreignTable = "users" phpName="User">
<reference local="user_id" foreign="id"/>
</foreign-key>
<behavior name="timestampable"/>
</table>
当我尝试获取与用户相关的公司时,
CompanyQuery::create() ->filterByUser($user)->find();
我可以成功获得所有用户。但是,当我尝试获取与公司相关的所有用户时
$user = UserQuery::create()->filterByCompany($company)->find();
我没有任何用户。我还检查了生成的sql,它只有一种方式关系
CREATE TABLE `users_companies`(
`user_id` INTEGER NOT NULL,
`company_id` INTEGER NOT NULL,
`created_at` DATETIME,
`updated_at` DATETIME,
PRIMARY KEY (`user_id`,`company_id`),
INDEX `users_companies_FI_1` (`company_id`)) ENGINE=MyISAM;
谁能指出我做错了什么?