donglaoe2979 2016-03-27 18:04
浏览 24
已采纳

cakePhp 3加入两个不相关的mysql表(松散相关)

I inherited a database with a weird (maybe not) design. I'm migrating to cakePHP but modifying it will break up lot of things so I want to keep it as it is.

I have two tables clothes and pictures,clothes has this fields :

clothes
{
ean
reference
reference_name
size 
color
}

ean (european article number) is the unique id, every ean is a unique combination of reference color and size, but a reference has multiple pictures that are shared by the different clothes (sizes and colors, there is no consistency between the color or sizes and the pictures).

pictures
{
id
id_reference
reference_name
picture
}

"id_reference" matches the "reference" field in the clothes table but they are not formally related they are not marked as foreign keys.

I have this function to retrieve a JSON array

public function women() 
 {
 $page = $_POST['page'];
 $this->autoRender = false;
$articles = TableRegistry::get('Clothes');
$results = $page;
if(is_numeric($page))
{
$pageNumber = (int)$page;
$results = $articles->find('all',['page'=>1])
    ->select(['reference','nombre'])
    ->where(['Clothes.reference_name' => 'WOMEN'])
    ->group('reference')
    ->limit(10)
    ->contain(['Pictures']);
}

 $this->response->body(json_encode($results));
 return $this->response;

}

class ClothesTable extends Table
{
public function initialize(array $config)
    {
        $this->hasMany('Pictures', [
            'className' => 'Pictures',
            'foreignKey' => 'id_reference',
            'bindingKey' => 'reference'
        ]);
    }
}

I tried some of the methods in the oficial documentation but cakephp throws an error (the two tables are not related). Right now the JSON string contains an empty pictures=[] array. Does anyone knows how can I retrieve the picture list in the JSON response?

  • 写回答

1条回答 默认 最新

  • drlndkhib08556095 2016-03-27 18:10
    关注

    i guess there are two ways to do this. Either you declare a explicit foreign key. Or else use plain sql in place of cakephp's. As you say you dont have the luxury to modify, i bet going the sql way is better.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接