duancai9010 2016-06-21 13:22
浏览 12

HasMany关联关联(belongsstomany through)模型

I'm trying to make this thing work :

Orders belongsToMany Products through ProductsOrders

ProductsOrders has a composite key (order_id, product_id) - At this point, everything work perfectly.

What I wan is to add a new association (ProductsOrders hasMany Extras) because a product can have a couple of data (sauces, kaking style, drink, ...).

So I've created the Extras model with :

$this->belongsTo('Orders', [
            'foreignKey' => 'order_id',
            'joinType' => 'INNER'
        ]);
$this->belongsTo('Products', [
            'foreignKey' => 'product_id',
            'joinType' => 'INNER'
        ]);

And I added the following in the ProductsOrders model

 $this->hasMany("Extras", [
            'foreignKey' => [
                'order_id',
                'product_id'
            ],
            'bindingKey' => [
                'order_id',
                'product_id'
            ]
        ]);

When patching the Order Entity, it seems to be correct : (simplified version)

object(App\Model\Entity\Order) {
    'establishment_id' => (int) 1,
    'state_id' => (int) 20,
    'products' => [
        (int) 0 => object(App\Model\Entity\Product) {

            'id' => (int) 32,
            'establishment_id' => (int) 1,
            'category_id' => (int) 11,
            '_joinData' => object(App\Model\Entity\OrdersProduct) {

                'quantity' => (int) 4,
                'price' => (float) 9,
                'extras' => [
                    (int) 0 => object(App\Model\Entity\Extra) {

                        'title' => 'Sauces',
                        'choice' => 'Mayonaise',
                        'price' => (float) 0,
                        '[new]' => true,
                        '[accessible]' => [
                            '*' => true
                        ],
                        '[dirty]' => [
                            'title' => true,
                            'choice' => true,
                            'price' => true
                        ],
                        '[original]' => [],
                        '[virtual]' => [],
                        '[errors]' => [],
                        '[invalid]' => [],
                        '[repository]' => 'Extras'

                    }
                ],
                '[new]' => true,
                '[accessible]' => [
                    '*' => true
                ],
                '[dirty]' => [
                    'quantity' => true,
                    'price' => true,
                    'tva' => true,
                    'extras' => true
                ],
                '[original]' => [],
                '[virtual]' => [],
                '[errors]' => [],
                '[invalid]' => [],
                '[repository]' => 'OrdersProducts'

            },
        }
    ]
}

The problem is, when saving (with success), there is no Extras data in table ... That's sad.

Where did I make a mistake ? Or is this still possible ?

Thanks in avdance

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP