0

私は propel の初心者で、propel の多対多の関係に関連する問題に直面しています。私は2つのテーブルを持っています。ユーザー:

  <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;

誰かが私が間違っていることを指摘できますか?

4

1 に答える 1

0

外部キーをサポートしない ENGINE=MyISAM を使用しています。多分これはそれと関係があります。

build.properties以下が含まれていることを確認してください。

; mysql options
propel.mysql.tableType = InnoDB

生成された「ブリッジ」テーブル 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`),
    CONSTRAINT `users_companies_FK_1`
        FOREIGN KEY (`company_id`)
        REFERENCES `companies` (`id`),
    CONSTRAINT `users_companies_FK_2`
        FOREIGN KEY (`user_id`)
        REFERENCES `users` (`id`)
) ENGINE=InnoDB;

これらすべてが役に立たない場合は、最新の Propel (1.6.8) を使用していることを確認してください。

生成された BaseUser::getCompanys() と BaseCompany::getUsers() もあります。

于 2013-06-10T07:37:11.127 に答える