3

次のように、HABTM を使用する必要があるデータ モデルがあります。

surveys 
(
    id int(11) NOT NULL AUTO_INCREMENT,
    user_id int(11) NOT NULL,
    title varchar(50) DEFAULT NULL,
    created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    modified timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
    PRIMARY KEY (id),
    KEY user_id (user_id)
);

questions 
(
    id int(11) NOT NULL AUTO_INCREMENT,
    user_id int(11) NOT NULL,
    title varchar(50) NOT NULL,
    body text NOT NULL,
    created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    modified timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
    PRIMARY KEY (id),
    KEY user_id (user_id)
);

questions_surveys  
(  
    id int(11) NOT NULL AUTO_INCREMENT,  
    survey_id int(11) NOT NULL,  
    question_id int(11) NOT NULL,  
    created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,  
    modified timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',  
    PRIMARY KEY (id),  
    KEY survey_id (survey_id),  
    KEY question_id (question_id)  
);

そして、関連する外部キー:

ALTER TABLE questions_surveys
ADD CONSTRAINT questions_surveys_ibfk_1 FOREIGN KEY(survey_id) REFERENCES surveys(id);

ALTER TABLE questions_surveys
ADD CONSTRAINT questions_surveys_ibfk_2 FOREIGN KEY(question_id) REFERENCES questions(id);

質問と調査には HABTM 関係があるため、1 つの調査に多くの質問があり、1 つの質問が多くの異なる調査に含まれる場合があります。

Survey.php で:

public $hasAndBelongsToMany = array(
    'Question' => array(
        'className' => 'Question',
        'joinTable' => 'questions_surveys',
        'foreignKey' => 'survey_id',
        'associationForeignKey' => 'question_id'
    )
);

Question.php で:

public $hasAndBelongsToMany = array(
    'Survey' => array(
        'className' => 'Survey',
        'joinTable' => 'questions_surveys',
        'foreignKey' => 'question_id',
        'associationForeignKey' => 'survey_id'
    )
);

これが、SurveysController.php からの追加コントローラーです。

public function add()
{
    $this->set('fields', $this->Survey->getFields());
    $this->set('users', $this->Survey->User->find('list', array('fields' => array('id', 'username'))));
    $this->set('questions', $this->Question->find('list', array('fields' => array('id', 'body'))));

    if (!empty($this->data))
    {
        $this->Survey->saveAll($this->data['Survey']);

        foreach($this->data['Question']['id'] as $question_id)
        {
            $newdata[] = array('Survey' => array('id' => $this->Survey->getInsertID()), 'Question' => array('id' => $question_id));
        }

        if ($this->Survey->saveAll($newdata))
        {
            $this->Session->setFlash('The survey was successfully added!');
            $this->redirect(array('action'=>'index'));
        }
        else
        {
            $this->Session->setFlash('Unable to add survey.');
        }
    }
}

最初に新しい調査が保存され、次にすべての question_survey が配列に追加され、それらすべてが一度に追加されます。データは次のようになります。

Array
(
    [0] => Array
        (
            [Survey] => Array
                (
                    [id] => 17
                )

            [Question] => Array
                (
                    [id] => 1
                )

        )

    [1] => Array
        (
            [Survey] => Array
                (
                    [id] => 17
                )

            [Question] => Array
                (
                    [id] => 2
                )

        )

    [2] => Array
        (
            [Survey] => Array
                (
                    [id] => 17
                )

            [Question] => Array
                (
                    [id] => 3
                )

        )

    [3] => Array
        (
            [Survey] => Array
                (
                    [id] => 17
                )

            [Question] => Array
                (
                    [id] => 4
                )

        )

    [4] => Array
        (
            [Survey] => Array
                (
                    [id] => 17
                )

            [Question] => Array
                (
                    [id] => 5
                )

        )

    [5] => Array
        (
            [Survey] => Array
                (
                    [id] => 17
                )

            [Question] => Array
                (
                    [id] => 6
                )

        )

    [6] => Array
        (
            [Survey] => Array
                (
                    [id] => 17
                )

            [Question] => Array
                (
                    [id] => 7
                )

        )

    [7] => Array
        (
            [Survey] => Array
                (
                    [id] => 17
                )

            [Question] => Array
                (
                    [id] => 8
                )

        )

    [8] => Array
        (
            [Survey] => Array
                (
                    [id] => 17
                )

            [Question] => Array
                (
                    [id] => 9
                )

        )

    [9] => Array
        (
            [Survey] => Array
                (
                    [id] => 17
                )

            [Question] => Array
                (
                    [id] => 10
                )

        )

)

このエラーが発生し続けます:

エラー: SQLSTATE[42S22]: 列が見つかりません: 1054 不明な列 'QuestionsSurvey.survey_id' 'where 句'
SQL クエリ: SELECT AppModel. question_idからengagequestions_surveysどこにAppModelでもQuestionsSurveysurvey_id= 13

私が知る限り、すべての名前は CakePHP の標準に従っており、'with' => 'QuestionsSurvey' を使用してみましたが、次のエラーが発生しました。

データベース テーブルの欠落エラー: モデル QuestionsSurvey のテーブル app_models がデータソースのデフォルトで見つかりませんでした。

そして 'with' => 'QuestionsSurveys' ですが、同じエラーが発生します:

データベース テーブルの欠落エラー: モデル QuestionsSurveys のテーブル app_models がデータソースのデフォルトで見つかりませんでした。

モデルトリオをモデルを介して hasMany に変換しようとしました (機能しませんでした。すべて HABTM に戻ると言われました)。

私はデータにあらゆる種類の異なるフォーマットを使用しましたが(CakePHP Saving Your Data)、運もありません。

私は困惑しています。私が間違っていることを誰かが知っていますか?また、エントリとコードのセクションが非常に長くなってしまったことをお詫びしますが、徹底したかったのです。

お時間をいただきありがとうございます!
マット

4

2 に答える 2

4

データは次のようになります。

array(
    'Survey' => array(
        'title' => 'example',
    ),
    'Question' => array(
        (int) 0 => '1',
        (int) 1 => '2',
    )
)

データを保存するには: $this->Survey->saveAll($data);

ありがとう

于 2013-09-23T08:31:53.287 に答える
3

そのため、本が間違っているか、私が誤解していることがわかりました。これが機能する形式です。

Array
(
    [0] => Array
    (
        [survey_id] => 33
        [question_id] => 9
    )

    [1] => Array
    (
        [survey_id] => 33
        [question_id] => 10
    )
)

そして、これはコントローラに保存する必要がある方法です:

if ($this->Survey->QuestionsSurvey->saveAll($newdata))
于 2013-02-01T21:55:04.187 に答える