在HABTM模型中找到cakephp

时间:2013-09-02 19:23:39

标签: cakephp cakephp-2.0 has-and-belongs-to-many

我有一个HABTM模型Request HABTM Certificates

当我$this->Request->find('all)时,我得到了这个数组:

(int) 0 => array(
    'Request' => array(
        'id' => '114',
        'motivo' => 'Licitação',
        'status' => false,
        'solicitante' => ''
    ),
    'Certificate' => array(
        (int) 0 => array(
            'id' => '1',
            'nome' => '545.15',
            'responsavel' => '123',
            'email' => 'eu@vc.com',
            'emailcc' => '',
            'CertificatesRequest' => array(
                'id' => '53',
                'request_id' => '114',
                'certificate_id' => '1'
            )
        ),
        (int) 1 => array(
            'id' => '3',
            'nome' => 'Certidao',
            'responsavel' => '10',
            'email' => 'eu@vc.com',
            'emailcc' => '',
            'CertificatesRequest' => array(
                'id' => '54',
                'request_id' => '114',
                'certificate_id' => '3'
            )
        )....

我需要list Certificates.nome对应某个Request.id

我正在尝试这个,没有成功:

   $arquivos = $this->Request->find('list', array('conditions' => array('id' => 114),
    'contain' => array('Certificate' => array('fields' => array('nome')))));

如何list Certificates.nome对应某个Request.id

2 个答案:

答案 0 :(得分:0)

试试这个:

 $arquivos = $this->Request->Certificate->find('list', array(
                'conditions' => array('Request.id' => 114),
                'fields' => array('Certificate.id', 'Certificate.nome')
               ));

答案 1 :(得分:0)

  1. 创建模型CertificatesRequest(belongsTo Request,Certificate)。
  2. 查询证书请求 - &gt;查找('all',数组('包含'=&gt;数组('证书'),'条件'=&gt;数组('CertificatesRequest.request_id'=&gt; $ request_id))< / LI>