0

私は3つのテーブルを持っています:

証明書、リクエスト、リクエストc

Request hasMany Requestc. id request_idリクエストは、certificate_requests テーブルを使用し、列を持っていcertificate_idます。

テーブルからデータを取得Requestしようとしていますが、そのリクエストに関連付けられている証明書の名前を取得しようとしています。

私は成功せずにこれを試しています:

$options['joins'] = array (
                    array( 'table' => 'certificates_requests',
                            'alias' => 'Requestc',
                            'type' => 'left',
                            'conditions' => array('Certificate.id = Requestc.certificate_id')

                    )
    );
$certidoes = $this->Request->find('all', $options);

私のモデル:

class Certificate extends AppModel {

public $name = 'Certificate';}




class Request extends AppModel {

public $name = 'Request';

public $hasMany = array(
    'Requestc' => array(
        'foreignKey'    => 'request_id'
    )
); }



class Requestc extends AppModel {

public $name = 'Requestc';
public $belongsTo = 'Request';
public $useTable = 'certificates_requests'; }
4

1 に答える 1

1

それは私には HABTM 関係のように見えます。

封じ込め可能な行動はあなたの問題を解決します。詳細はこちら

<?php

// Request.php // Model
var $actsAs = array('Containable');
var $hasAndBelongsToMany = array(
    'Certificate' => array(
        'className' => 'Certificate',
        'joinTable' => 'certificates_requests',
        'foreignKey' => 'request_id',
        'associationForeignKey' => 'certificate_id'
    )
);

// Certificate.php // Model
var $actsAs = array('Containable');
var $hasAndBelongsToMany = array(
    'Request' => array(
        'className' => 'Request',
        'joinTable' => 'certificates_requests',
        'foreignKey' => 'certificate_id',
        'associationForeignKey' => 'request_id'
    )
);

// RequestsController.php
$this->Request->contain(array(
    'Certificate' => array(
        'fields' => array('id', 'name')
    )
));

$request = $this->Request->read(null, <requestIdHere>);
// $request = array(
//     'Request' => array(
//          '<requestData>',
//          ...
//     ),
//     'Certificate' => array(
//          [0] => array(
//              'id'     => <idGoesHere>,
//              'name'   => "<nameGoesHere>'
//          ),
//          [1] => array(
//              'id'     => <idGoesHere>,
//              'name'   => "<nameGoesHere>'
//          ),
//          [2] => array(
//              'id'     => <idGoesHere>,
//              'name'   => "<nameGoesHere>'
//          ),
//     )
// )
?>

これで問題が解決するかどうかお知らせください。:)

-ドリュー

于 2013-04-12T01:35:31.377 に答える