php - 无法让外键在 Propel2 中工作

标签 php mysql orm propel

我正在尝试将 Propel2 用于我的网络应用程序。我只是不明白为什么我的外键连接不起作用。它与书籍示例非常相似,并且运行良好。

我的 schema.xml:

<database name="default" defaultIdMethod="native"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:noNamespaceSchemaLocation="http://xsd.propelorm.org/1.6/database.xsd">

    <!-- Table Structure: 'Games' -->
    <table name="games" phpName="Game">
        <column name="game_id" type="INTEGER" required="true" sqlType="int(11) unsigned" primaryKey="true" autoIncrement="true"/>
        <column name="game_name" type="VARCHAR" size="150" required="true"/>
        <unique name="UNIQUE_game_name">
            <unique-column name="game_name" size="150" />
        </unique>
    </table>

    <!-- Table Structure: 'Teams' -->
    <table name="teams" phpName="Team">
        <column name="team_game" type="INTEGER" required="true" sqlType="int(11) unsigned"/>
        <column name="team_id" type="INTEGER" required="true" sqlType="int(11) unsigned" primaryKey="true" autoIncrement="true"/>
        <column name="team_name" type="VARCHAR" size="150" required="true"/>
        <index name="KEY_teams_for_constraints">
            <index-column name="team_game" size="11" />
            <index-column name="team_id" size="11" />
        </index>
        <index name="KEY_teams_name_check">
            <index-column name="team_game" size="11" />
            <index-column name="team_name" size="150" />
        </index>
        <foreign-key name="FK_team_game" foreignTable="games" phpName="Game" refPhpName="Team">
            <reference local="team_game" foreign="game_id"/>
        </foreign-key>
    </table>

</database>

我的 PHP 测试代码:

echo "----- ----- -----  PROPEL TEST (START) ----- ----- ----- <BR><BR>";

echo "--1--<BR>";
$game = new Game();
$game->setGameName('PropelTestGame10');
echo "--2--<BR>";
$team = new Team();
$team->setTeamName('Propel Test Team10');
$team->setTeamGame($game);
echo "--3--<BR>";
$team->save();
echo "--4--<BR>";
var_dump($team->toArray());
echo "<BR>";
echo "--5--<BR>";

echo "----- ----- -----  PROPEL TEST (END) ----- ----- ----- <BR><BR><BR><BR><BR>";

PHP 代码生成以下输出:

----- ----- ----- PROPEL TEST (START) ----- ----- -----
--1--
--2--
Notice: Object of class Game could not be converted to int in E:\MyApp\Model\Base\Team.php on line 351
--3--
--4--
array(3) { ["TeamGame"]=> int(1) ["TeamId"]=> int(10) ["TeamName"]=> string(18) "Propel Test Team10" }
--5--
----- ----- ----- PROPEL TEST (END) ----- ----- -----

如果游戏表为空,则数据库中不会存储任何内容。在这种情况下,游戏表中有一个旧行,并且插入的团队与该游戏相关联 (TeamGame=1)。游戏 table 上没有保存任何新内容。

我做错了什么? 我不明白为什么当书本示例可以工作时我的结构不应该工作( Propel 2 - Book-example )。

最佳答案

这是你的错误:

$team->setTeamGame($game);

您正在尝试将 team_game 列设置为对象值。您在架构中写道,这将是整数,因此基类中的这个自动生成的函数需要整数值。您应该使用:

$team->setGame($game);

如果你想设置相关对象或者你可以使用:

$game->save();
$team-setTeamGame($game->getPrimaryKey());

这是您的working example

关于php - 无法让外键在 Propel2 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30682279/

相关文章:

php - Json 变量值丢失

mysql - MySQL的 'UNION'如何删除多列的重复项?

java - JPA:OneToOne 关系所有者

php - php orm 学说如何获取对象及其相关连接

php - Doctrine SQL/表生成失败

php - 将邮件通过管道传输到 PHP 时提取附件

php - 在我的网站中创建一个图片库,其中有包含许多图片的相册

php - 将平面数组中的每个整数乘以 60

PHP插入带有特殊字符的MySQL表文本?

mysql - 无法授予远程访问权限