php - 插入多对多关系不起作用

标签 php mysql propel

我是一个推进新手,遇到了与推进多对多关系相关的问题。 我有两张 table 。 用户:

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

谁能指出我做错了什么吗?

最佳答案

您使用的 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()。

关于php - 插入多对多关系不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17013834/

相关文章:

javascript - 如何防止整数onreadystatechange的改变

php - magento 不发送任何邮件,如何调试?

php - PHP 中的曲线拟合

mysql - MySQL 中的 UNION 问题

php - 我如何获取数据库更新行中的最后一个 id?

php - 如何在正确的目录中使用 namespace 构建推进模型?

php - 数据库错误处理: what if you have to call outside service and the transaction fails?

php - 插值是 LESS 变量

c++ - 何时调用 mysql_free_result (Resuing mysql_store_result, MYSQL_RES)

php - 如何使用 Propel ORM 来 Zend Framework