0

我有 3 张桌子:

证书、请求和请求c

请求有很多请求c。Requestc 使用certificates_requests 表和hasid request_idcertificate_idcolouns。

我正在尝试从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 回答