【问题标题】:cakephp 2.2.3 - cannot get associated data with hasMany throught (Join Model)cakephp 2.2.3 - 无法通过 hasMany 获取关联数据(加入模型)
【发布时间】:2012-11-07 13:10:12
【问题描述】:

我无法通过(加入模型)关联使用 hasMany 获取关联的模型数据。 我正在使用 cakephp 2.2.3 在 cakephp 1.3 中同样的发现是好的,所以我不知道是怎么回事......

Event hasMany ScoresToEvent。 Scores 有很多 ScoresToEvent。 ScoresToEvent 属于事件和分数。

ScoresToEvent 会有额外的信息,所以我不能使用 HATBM。

一些代码:

event.php    
class Event extends AppModel{
      public $name='Event';
      public $hasMany=array('ScoresToEvent');
      public $belongsTo=array('Entity');
      public $actsAs=array('containable');
} 

score.php
class Score extends AppModel{
      public $name='Score';
      public $hasMany=array('ScoresToEvent');
      public $belongsTo=array('Entity');
}

scores_to_event.php
class ScoresToEvent extends AppModel{
   public $name='ScoresToEvent';
   public $belongsTo=array('Event','Score');
}

当我检索数据时,我得到以下结果:

$this->Event->ScoresToEvent->find('all', array('recursive'=>2))
array(
    (int) 0 => array(
        'ScoresToEvent' => array(
            'id' => '8',
            'event_id' => '7',
            'score_id' => '1'
        )
    ),
    (int) 1 => array(
        'ScoresToEvent' => array(
            'id' => '9',
            'event_id' => '7',
            'score_id' => '3'
        )
    )
) 

在这种情况下,我必须获取事件和分数数据。

如果我尝试使用可包含,它会返回 Model "ScoresToEvent" 与模型 "Score" 和此数组无关,因此它不会检索 Score 数据...

$this->Event->find('all', array(
      'contain'=>array(
         'Entity',
         'ScoresToEvent'=>array('Score'=>array('Entity'))
      ),
      'conditions'=>array('Event.id'=>7));

array(
    (int) 0 => array(
        'Event' => array(
            'id' => '7',
            'entity_id' => '17',
            'start_date' => '2012-07-24',
            'status' => null,
            'end_date' => null
        ),
        'Entity' => array(
            'id' => '17',
            'title' => 'y',
            'content' => '',
            'subtitle' => '',
            'type' => 'Evento',
            'avatar' => null,
            'image' => null
        ),
        'ScoresToEvent' => array(
            (int) 0 => array(
                'id' => '8',
                'event_id' => '7',
                'score_id' => '1'
            ),
            (int) 1 => array(
                'id' => '9',
                'event_id' => '7',
                'score_id' => '3'
            )
        )
    )
)

我的错在哪里?代码的哪一部分是错误的? 我在全新的 cakephp 2.2.3 安装上试过这个

谢谢大家

附言相同的代码在 cakephp 1.3 中正常工作 pps不要考虑“实体”。

【问题讨论】:

    标签: cakephp cakephp-2.2


    【解决方案1】:

    我认为是模型名称的问题。尝试将文件scores_to_event.php 重命名为ScoresToEvent.php。 但在这种情况下,我认为这个模型的最佳名称是:ScoreEvent.php,更合适。 在尝试向您的模型插入更多信息后,如下例所示: 进入 ScoreEvent.php

    public $belongsTo = array(
            'Event' => array(
                'className'    => 'Event',
                'foreignKey'   => 'event_id'
            ),
           'Score' => array(
                'className'    => 'Score',
                'foreignKey'   => 'score_id'
            )
    );
    

    【讨论】:

    • Grazie 无限,sapevo che doveva essere un dettaglio importante che non avevo 考虑!!! Ho cambiato il nome del file come mi hai consigliato e tutto ha rispreso a funzionare。在 effetti ero rimasto alle convenzioni della 版本 1.3。安科拉感恩!非常感谢,我知道它必须是一个重要的,而不是考虑的细节。我按照您的建议更改了文件名,并且有效。我仍然使用 1.3 版本的约定。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-04
    • 2018-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多