php - Doctrine (symfony) YAML schema/fixture 约束问题

标签 php mysql symfony1 doctrine symfony-1.4

早上好,
我在创建表格/加载固定装置时遇到了一些问题。
(symfony 1.4.6 与捆绑的 Doctrine 1.2.3(?))

表格(简化):

Horseman:
    tableName: Horseman    
    actAs: { Timestampable: ~ }
    columns:
        id:     { type: integer(4), primary: true, autoincrement: true, unsigned: true }
        name:   { type: string(100), notnull: true }

Stable:
    tableName: Stable
    actAs: { Timestampable: ~ }
    columns:
        id:         { type: integer(4), primary: true, autoincrement: true, unsigned: true }
        info:       { type: clob }
        id_owner:   { type: integer(4), unsigned: true }
    relations:
        Horseman:   { local: id_owner, foreign: id }

Horses:
    tableName: Horses
    actAs: { Timestampable: ~ }
    columns:
        id:         { type: integer(4), primary: true, autoincrement: true, unsigned: true }
        name:       { type: string(100), notnull: true }
        id_owner:   { type: integer(4), unsigned: true }
        id_stable:  { type: integer(4), unsigned: true }
    relations:
        Horseman:   { local: id_owner, foreign: id }
        Stable:     { local: id_stable, foreign: id }


“骑士”没有任何依赖
“马厩”有一个“骑士”
“Horses”有一个“Horseman”和一个“Stable”

固定装置:

Horseman:
    Hector:
        name: Hector

Stable:
    StableA:
        info: Lorem Ipsum Dolor Sit Amet
        id_owner: Hector

Horses:
    Ed:
        name: Ed
        id_owner: Hector
        id_stable: StableA

插入夹具时:

$ php symfony doctrine:build --all --and-load

我违反了约束:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`issuetracker/Stable`, CONSTRAINT `Stable_id_owner_Horseman_id` FOREIGN KEY (`id_owner`) REFERENCES `Horseman` (`id`))

插入“Horseman”条目没有问题。
手动插入其他两个条目也不成问题:

INSERT INTO Stable (id,info,id_owner,created_at,updated_at) VALUES (null,"foo",1,NOW(),NOW())
INSERT INTO Horses (id,name,id_owner,id_stable,created_at,updated_at) VALUES (null,"foo",1,1,NOW(),NOW())


(symfony 是否将创建的插入语句保存在某处?)

如果我做对了,Doctrine 应该自己负责插入订单(?)因为无论如何我给出了正确的订单,这不应该是问题..

希望我只是没有看到或没有得到一件小事,你们中的一个好人能告诉我为什么我会违反约束和/或如何解决它,非常拜托..

谢谢。

最佳答案

像这样尝试:

Horseman:
    Hector:
        name: Hector

Stable:
    StableA:
        info: Lorem Ipsum Dolor Sit Amet
        Horseman: Hector

Horses:
    Ed:
        name: Ed
        Horseman: Hector
        Stable: StableA

您不想使用相关对象的 ID,而是使用相关对象。这就是为什么您应该使用关系名称而不是字段名称。

关于php - Doctrine (symfony) YAML schema/fixture 约束问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3841033/

相关文章:

php - 在 PHP/MySQL 中对符合特定条件的行进行排序

MySQL - 从 LOCAL INFILE 加载数据 - 如何提高性能

mysql - 使用 Entity Framework 将对象保存到 MySQL 后,对象 id 仍然显示为 0

PHP:这对恶意输入安全吗?

symfony1 - 如何在 symfony 中修改/添加帖子参数?

php - 使用 Propel 计数和分组

php - 多个系列、多个图表 Highcharts

php - Cakephp 创建 secret 输入的最佳方法

symfony1 - 带有连接的 symfony 管理过滤器

php - 如何从 PHP 中的现有数组创建新数组?