English 中文(简体)
CakePHP:通过协会进行双向自我参照
原标题:CakePHP: bi-directional self-referential hasMany Through associations

我试图通过卡纳克民阵(mouth!)的关系,把我的头接在两头上。

I m working on a picture matching website.

  • Pictures are associated to other pictures via a match (the join model).
  • Each match has two pictures and stores the current rating and the total number of votes.
  • When viewing a picture, all of its related images from either direction should be available (via its matches).

我先通过与加入模式的关系来界定 has。

表格中的图象是:

id | picture_id | partner_id | rating | total_votes

我加入示范协会就是这样:

class PictureMatch extends AppModel {

...

    public $belongsTo = array(
         Picture  => array(
             className  =>  Picture ,
             foreignKey  =>  picture_id ,
             conditions  =>   ,
             fields  =>   ,
             order  =>   
        ),
         Partner  => array(
             className  =>  Picture ,
             foreignKey  =>  partner_id ,
             conditions  =>   ,
             fields  =>   ,
             order  =>   
        )
    );
}

Each picture needs to be able to access its related pictures from either direction, but this is where my grasp is slipping. It looks like I need to save both sides of the relationship but this destroys the extra data stored in the join model - with two db entries, voting could vary depending on the direction.

Can anyone shed any light on the best way to do this in CakePHP? I m rather confused.
Is it possible to create the inverse relationships on the fly?

问题回答

你可以就“爱生模式”提出现实:Bbind Model()非常有用,这预示着你要控制逆向关系,或者说是你希望的飞跃。

http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html

此外,你还利用可维持的行为,可以建立无限链,使您的相关日期提前结束。

包含(照片:PictureMatch.Partner.PictureMatch.Picture....)

基本上,只要每一链条都与下一个链条有某种关系,就可以通过你的所有模式来解释其更简单的例子(请在其中不考虑逻辑)

Circle belongsTo Square Square belongsTo Triangle

So Triangle与环圈(直接)没有关系,但广场在环圈之间是友好的。

Circle->find( all , array( ... , contain => array( Square.Triangle )); 

or to have more fun lets get circle by circle with loop around

Circle->find( all , array( ... , contain => array( Square.Trinagle.Square.Circle ));

因此,当然,这些例子是毫无用处的,没有任何方案逻辑,但我希望你理解,你可以 lo住多少关系。

I am not sure if this is the best solution but if you did this:

public $belongsTo = array(
     Picture1  => array(
         className  =>  Picture ,
         foreignKey  =>  picture_id ,
    ),
     Picture2  => array(
         className  =>  Picture ,
         foreignKey  =>  picture_id ,
    ),
     Partner  => array(
         className  =>  Partner ,
         foreignKey  =>  partner_id ,
    ),
);

然后,在你进行查询时,你才查询<代码>($this-gt;data[图片] = var ́ ́ ́ «this->data[] 页: 1 页: 1

我假定这一点已经放弃,但很容易再解决——它必须与阶段有关。

销毁合并模型中储存的额外数据

这意味着你们的储蓄正在运行一个<代码>deleteAll,并插入关于匹配的记录......相反,你需要找到并更新这一记录。

可以用几种方式做到这一点,但最容易的是,在你发出呼吁之前,看着这种呼吁,并将主要关键内容列入相应的记录数据。 基本上不将其作为现金自动取款机予以节省,而且只有在你试图找到现有对应记录的主要钥匙(id)并更新数据以予以节省时,才将其作为 has予以节省。





相关问题
PHP Framework: Ebay Like Site

I am going to be builiding a site like ebay - with all the features of ebay. Please note my payment method is limited to paypal. What would be the best PHP framework to use to build this quickly, ...

specifying date format when using $form->inputs() in CakePHP

I am wondering if there is a way to specify the date format in the forms created using CakePHP s $form->inputs(); Please note that this is not the individual $form->input() but instead $form->inputs() ...

Using DISTINCT in a CakePHP find function

I am writing a CakePHP 1.2 app. I have a list of people that I want the user to be able to filter on different fields. For each filterable field, I have a drop down list. Choose the filter ...

Assistance with CakePHP model relationships

How would I represent the following in a CakePHP model? Product ======= product_id .... Cart ==== cart_id .... Carts_Products ============== cart_id product_id quantity

Prevent controller from trying to autoload model

I am a beginning Cake user and trying to do some work on an already existing application. Running into a problem when I create a new controller. I have created StoreController and when I try to call ...

热门标签