CakePHP:在HABTM中保存数据的正确格式是什么

时间:2022-08-07 15:16:04

I have an artist that has and belongs to many related artists, in this example i want to save Madonna and her 2 related artists Cher and Kylie Minogue in the database.

我有一位艺术家,他拥有并且属于许多相关艺术家,在这个例子中,我想在数据库中保存麦当娜和她的2位相关艺术家Cher和Kylie Minogue。

DB: 2 tables: artists (id, name, created) and artists_related (id, artist_id, related_id)

DB:2个表:艺术家(id,name,created)和artists_related(id,artist_id,related_id)

Model relationship setup:

模型关系设置:

<?php
class Artist extends AppModel {
    public $hasAndBelongsToMany = array(
        'Related' =>
            array(
                'className' => 'Artist',
                'joinTable' => 'artists_related',
                'associationForeignKey' => 'related_id',
                'unique' => 'keepExisting'
            )
        );

I'm trying to save the data like this:

我正在尝试保存这样的数据:

    $test_data = array(
        'Artist' => array(
            'name' => 'Madonna'
         ),
        'Related' => array(
            0 => array(
                'name' => 'Kylie Minogue'
            ),
            1 => array(
                'name' => 'Cher'
            )
        )
    );

    $result = $this->saveAll($test_data, array('deep' => true));

What happens is that only Madonna is saved in the artists table, the two related artists arent saved, and nothing is saved in the join table.

发生的事情是,只有麦当娜被保存在艺术家表中,两位相关艺术家没有保存,并且没有任何内容保存在连接表中。

How should the data array be formatted to accomplish this? Or do i need to save first Madonna, and get the id and then save all the related artists in a different saveAll() ?

如何格式化数据数组以实现此目的?或者我需要保存第一个麦当娜,并获取ID,然后将所有相关的艺术家保存在另一个saveAll()?

2 个解决方案

#1


2  

As I know the related artist must contain Related Object that have key Related with some artist_id. This is example:

据我所知,相关艺术家必须包含具有与某些artist_id相关的键的相关对象。这是一个例子:

$test_data = array(
    'Artist' => array(
        'name' => 'Madonna'
     ),
    'Related' => array(
         'Related' => array(
             0 => '1',
             1 => '2'
         )
     )
);

#2


0  

You data has to be in this format.

您的数据必须采用此格式。

Array
(
[0] => Array
    (
        ['Artist'] => Array
            (
                [name] => 'Madonna'
            )
        ['Related'] => Array
            (
                [name] => 'Kylie Minogue'
            )
    )
[1] => Array
    (
        ['Artist'] => Array
            (
                [name] => 'Madonna'
            )
        ['Related'] => Array
            (
                [name] => 'Cher'
            )
    )
)

#1


2  

As I know the related artist must contain Related Object that have key Related with some artist_id. This is example:

据我所知,相关艺术家必须包含具有与某些artist_id相关的键的相关对象。这是一个例子:

$test_data = array(
    'Artist' => array(
        'name' => 'Madonna'
     ),
    'Related' => array(
         'Related' => array(
             0 => '1',
             1 => '2'
         )
     )
);

#2


0  

You data has to be in this format.

您的数据必须采用此格式。

Array
(
[0] => Array
    (
        ['Artist'] => Array
            (
                [name] => 'Madonna'
            )
        ['Related'] => Array
            (
                [name] => 'Kylie Minogue'
            )
    )
[1] => Array
    (
        ['Artist'] => Array
            (
                [name] => 'Madonna'
            )
        ['Related'] => Array
            (
                [name] => 'Cher'
            )
    )
)