Mysql 在模型中指定复杂关系

Mysql 在模型中指定复杂关系,mysql,cakephp,Mysql,Cakephp,目前,我正在使用(大致)以下代码从数据库中检索一些数据: $artists = $this->Artist->findAll($conditions); foreach($artists as $artist) { $photo_id = $artist['Artist']['photo_id']; $uid = $this->Uid->field('id', array( 'Uid.type'=>'Photo',

目前,我正在使用(大致)以下代码从数据库中检索一些数据:

$artists = $this->Artist->findAll($conditions);

foreach($artists as $artist) {
    $photo_id = $artist['Artist']['photo_id'];
    $uid = $this->Uid->field('id', array(
        'Uid.type'=>'Photo', 
        'Uid.foreign_id'=>$photo_id
    ));
    $photo_url = $this->ImageRef->field('url', array(
        'ImageRef.type' => 'Photo',
        'ImageRef.foreign_id' => $uid
        ), 'id ASC');
    $artist['Artist']['photo_url'] = $photo_url;

    $this->returnData[] = $artist;

}
显然,要找到每位艺术家的照片url,这是一条相当冗长的路线。理想情况下,我想在模型中指定这一点,也许是作为一个HasOne关系-每个艺术家都有一个photo\u url?但是我感觉有点被如何在模型中实现这一点的挑战压倒了,因为这不是在ImageRef表中查找photo_id的简单情况,而是中间的Uid表

有人能给我一些建议,告诉我如何在模型中正确地表示这种类型的多表跨越关系吗

编辑:

我觉得我没有很好地解释我自己。基本上,艺术家模型与Uid有$hasOne关系:

var $hasOne = array(
    'Uid' => array(
        'conditions' => "Uid.type = 'Artist'",
        'foreignKey' => 'foreign_id',
        'dependent' => true),
    );
然后我希望它通过Uid与ImageRef建立$hasOne关系,类似于:

'ImageRef' => array(
        'conditions' => "Uid.type = 'Photo'",
        'foreign_key' => "Uid.id"
    )

虽然看起来这样的事情并不容易。但是,如果有人知道一种更简单的方式来谈判这些一级删除的关系,我会很高兴

你能提供你拥有的表格,它们之间的关系,以及你想要的最终数据集是什么吗?基本上有一个艺术家表格,带有照片id字段。带有“type”和“foreign_id”的Uid表-如果type是“Photo”,那么它通过foreign_id连接到Artist表。然后有一个ImageRef表以相同的方式连接到Uid-如果ImageRef.type是“Photo”,那么foreign_id是对Uid表中上述条目的引用。基本上,我希望['Artist']['photo\u url'](或类似)是ImageRef.photo\u url,其中ImageRef.type='photo'和ImageRef.foreign\u id=(Uid.id其中Uid.type='photo'和Uid.foreign\u id=Artist.photo\u id)。这是一个比我以前更复杂的连接:(