CakePHP属于具有可变“模型”字段的关系 [英] CakePHP belongsTo relationship with a variable 'model' field

查看:50
本文介绍了CakePHP属于具有可变“模型”字段的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在CakePHP中,belongTo关系存在问题。

I've got a problem with a belongsTo relationship in CakePHP.

我有一个使用 actions表并属于其的 Action模型其他两个模型之一,即交易或标签。这样的想法是,每当用户完成交易或添加标签时,都会创建操作模型以保存其日志。我已经做好了这部分的工作,只要保存了交易或标签,aftersave()方法还会添加一个动作记录。问题是当我尝试在Action模型上执行find('all')时,未返回相关的Transaction或Tag记录。

I've got an "Action" model that uses the "actions" table and belongs to one of two other models, either "Transaction" or "Tag". The idea being that whenever a user completes a transaction or adds a tag, the action model is created to keep a log of it. I've got that part working, whenever a Transaction or Tag is saved, the aftersave() method also adds an Action record. The problem comes when I try to do a find('all') on the Action model, the related Transaction or Tag record is not being returned.

actions:

id
model
model_id
created

我想我可以在属关系中使用 conditions参数,例如:

I thought I could use the "conditions" parameter in the belongsTo relationship like this:

<?php
class Action extends AppModel {

var $name = 'Action';
var $actsAs = array('Containable');
var $belongsTo = array(
    'Transaction' => array(
        'foreignKey' => 'model_id',
        'conditions' => array("Action.model"=>"Transaction")
    ),
    'User' => array(
        'fields' => array('User.username') 
    ),
    'Recommendation' => array(
        'conditions' => array("Action.model"=>"Recommendation"),
        'foreignKey' => 'model_id'
    )
);  
}
?>

但这不起作用。

我在这里错过了什么吗,我的人际关系错了吗(我怀疑是这样)?谷歌搜索此问题后,我遇到了一个叫做多态行为的东西,但我不是

Am I missing something here, are my relationships wrong (I suspect so)? After Googling this problem I cam across something called Polymorphic Behaviour but I'm not sure this will help me.

推荐答案

如果我理解正确,听起来多态行为是一个很好的答案。它允许您将给定的 Action 记录与任何其他模型相关联(在您的情况下,为 Transaction 标签)。如果您随后检索 Action 并提供了内存(我只使用过一次),那么关联的模型也将返回(假设适当的递归设置。)

If I understand correctly, it sounds like the Polymorphic Behavior is a great answer. It allows you to tie a give Action record to any other model (in your case, a Transaction or Tag). If you then retrieve an Action--and memory serves (I've only used this once)--then the associated model will come back too (assuming the appropriate recursive setting).

例如,在最近的项目中,我构建了 Alert 模型。警报可以附加到任何模型。任何给定的警报记录都标识了它属于哪个模型以及适当的记录ID:

As an example, in a recent project I built an Alert model. Alerts could be attached to any model. Any given alert record identified which model it belonged to and the appropriate record ID:

class Alert extends AppModel {
  public $actsAs    = array (
    'Polymorphic' => array (
      'classField' => 'model',
      'foreignKey' => 'entity_id'
    )
  );

  # Additional model code
}

我的桌子是根据以下规范构建:

And my table was built to the following spec:

CREATE TABLE alerts (
  id CHAR(36) NOT NULL,
  alert_template_id VARCHAR(255) NOT NULL,
  model VARCHAR(255) NOT NULL,
  entity_id CHAR(36) NOT NULL,
  valid_from BIGINT NULL,
  valid_to BIGINT NULL,
  replacement_keys TEXT NULL,
  active BOOL NOT NULL DEFAULT 0,
  created BIGINT NOT NULL,
  updated BIGINT NOT NULL,
  PRIMARY KEY(id),
  FOREIGN KEY(alert_template_id)
    REFERENCES alert_templates(id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION
)TYPE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

请注意 entity_id model 字段。对于每个 Alert 记录,它们分别具有唯一的标识符和模型名称(例如 User )。听起来这可以很好地满足您的需求。

Note the entity_id and model fields. For each Alert record, these hold a unique identifier and model name (e.g. User), respectively. It sounds like this would meet your needs nicely.

这篇关于CakePHP属于具有可变“模型”字段的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆