简体   繁体   English

CakePHP-合并两个表

[英]CakePHP - Merge two Table in One

I have a problem, i have two models they are "Clientes" and "Residencias" (it's in portuguese). 我有一个问题,我有两个模型,分别是“客户”和“ Residencias”(葡萄牙语)。

My problem is.. im trying to link these two in just one...i created a new table with "cliente_id" and "residencia_id"... 我的问题是..我试图将这两个链接成一个...我用“ cliente_id”和“ residencia_id”创建了一个新表...

the output using the relation $belongsTo is like that... 使用关系$ belongsTo的输出是这样的...

array(
    0 => array(
        'bd' => array(
            'id' => 1,
            'cliente_id' => null,
            'residencia_id' => 2
        ),
        'Cliente' => array(),
        'Residencia' => array(
            'id' => 2
            'nome'  => 'aaa',
            'telefone'  => '333'
        )
    ),
    1 => array(
        'bd' => array(
            'id' => 2,
            'cliente_id' => 1,
            'residencia_id' null
        ),
        'Cliente' => array(
            'id' => 1,
            'nome' => 'bbb',
            'telefone' => '666'
        ),
        'Residencia' => array()
    )
)

get it?? 得到它??

i wanna do this: 我想这样做:

array(
    0 => array(
        'bd' => array(
            'id' => 1,
            'cliente_id' => null,
            'residencia_id' => 2
        ),
        'Result' => array(
            'id' => 2
            'nome'  => 'aaa',
            'telefone'  => '333'
        )
    ),
    1 => array(
        'bd' => array(
            'id' => 2,
            'cliente_id' => 1,
            'residencia_id' null
        ),
        'Result' => array(
            'id' => 1,
            'nome' => 'bbb',
            'telefone' => '666'
        )
    )
)

get it?? 得到它?? i wanna do this in the model and Paginate that...someone can help me?? 我想在模型中做到这一点并分页...有人可以帮助我吗? i didnt find answer for that...i dont know how... 我没有找到答案...我不知道...

thank you 谢谢

You could create an afterFind callback in the model you are querying that would modify the result and return it in the format you want. 您可以在查询的模型中创建afterFind回调,该回调将修改结果并以所需的格式返回。 It could look something like this: 它可能看起来像这样:

    public function afterFind($results, $primary = false) {
    if ($this->findQueryType == 'count' || !$primary) {
        return parent::afterFind($results, $primary);
    }

    foreach ($results as $k => $result) {
        if (!empty($result['Cliente'])) {
            $id = $result['Cliente']['id'];
            $nome = $result['Cliente']['nome'];
            $telefone = $result['Cliente']['telefone'];
        } else if (!empty($result['Residencia'])) {
            $id = $result['Residencia']['id'];
            $nome = $result['Residencia']['nome'];
            $telefone = $result['Residencia']['telefone'];
        }

        $results[$k]['Result'] = array(
            'id' => $id,
            'nome' => $nome,
            'telefone' => $telefone
        );
        unset($results[$k]['Cliente']);
        unset($results[$k]['Residencia']);
        continue;
    }

    return $results;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM