简体   繁体   English

CAKEPHP陷入了关联模式

[英]CAKEPHP Stucked in Associating Models

Im using cakephp and Im stuck about the idea of listing multiple models, here's my scenario: 我正在使用cakephp,我坚持列出多个模型的想法,这是我的场景:

I have two major models namely Task and Events. 我有两个主要模型,即任务和事件。 I used the Events model to track all changes. 我使用Events模型来跟踪所有更改。 this has the ff fields: id, model, model_id, changed I used Events table to track multiple model changes. 这有ff字段:id,model,model_id,更改我使用Events表来跟踪多个模型更改。 so when something changed to Task model it will be logged to Events models (get the idea??) 因此,当某些内容更改为任务模型时,它将被记录到事件模型中(获取想法??)

What I want to do is whenever i used the find method for Event model, i want to list the model information (for example, the Task model) together with my Events information like this: 我想要做的是每当我使用事件模型的find方法时,我想列出模型信息(例如,任务模型)以及我的事件信息,如下所示:

array(
    [0] => array (
        [Event] => array(
            id => 1
            model => Task
            model_id => 2
            changed => array()
        )

        [Task] => array(
            id => 2
            name => Task 1
            descp => Test Task
        )

    )
)

Note: The model can be any model, it can be Projects model. 注意:模型可以是任何模型,也可以是Projects模型。

At my task.php, it is no problem because i can easily declare: 在我的task.php,这没问题,因为我可以很容易地声明:

var $hasMany = array(
    'Events' => array(
            'className' => 'Events',
            'foreignKey' => 'model_id',
            'dependent' => false,
            'conditions' => array('Events.model' => 'Task')
        )
    );

I can get Events information using the find method on task although using 我可以使用任务上的find方法获取事件信息,尽管使用

 $this->Task->Event->find('all'); 

will not work, i dont know why. 不会工作,我不知道为什么。

So as soon as I declare, something like this in my Event Model: 所以,一旦我在我的事件模型中声明,这样的事情:

var $belongsTo = array(
    'Task' => array(
        'className' => 'Task',
        'foreignKey' => 'model_id',
        'conditions' => array('Event.model' => 'Task', 'Event.model_id' => 'Task.id'),
        'fields' => '',
        'order' => ''
    )
);

will throw an SQL error. 将抛出SQL错误。 Do you guys have an idea how to implement it?? 你们有没有想法如何实现它? Thanks in advanced. 提前致谢。 :) :)

I use polymorphic models all the time. 我一直使用多态模型。 The reason that $this->Task->Event->find('all') doesn't work is that you were performing the find on the Event model which, until you defined its belongsTo association, didn't have any means of retrieving the Task info. $this->Task->Event->find('all')不起作用的原因是你在Event模型上执行find,直到你定义了belongsTo关联,才没有任何方法检索Task信息。

As for now, the only thing I see that looks odd is that you've aliased your Task association as Events (plural) rather than its singular version which is the convention for models. 至于现在,我看到的唯一看起来奇怪的是你把你的Task关联别名为Events (复数)而不是它的单数版本,这是模型的约定。 You also identified the className as the plural version. 您还将className标识为复数版本。

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

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