douban2014 2014-06-26 22:33
浏览 16

模特没有加入

I have 4 tables:

  • wizards (id, name)
  • pages (id, page_number, wizard_id)
  • modules (id, name)
  • modules_pages(id, page_id, module_id, wizard_id)

The models are baked defaults.

A Wizard has a Page, a Page has a Module. modules_pages is a junction table. I cannot get a list of modules when I display an array from wizard or pages that is filtered by the current wizard_id of the page, or one that I pass. It just displays the modules multiple times.


Page Model

    public $belongsTo = array(
    'Wizard' => array(
        'className' => 'Wizard',
        'foreignKey' => 'wizard_id',
    ),
);
public $hasAndBelongsToMany = array(
    'Module' => array(
        'className' => 'Module',
        'joinTable' => 'modules_pages',
        'foreignKey' => 'page_id',
        'associationForeignKey' => 'module_id',
        'unique' => 'keepExisting',
    )
);

Module Model

public $hasAndBelongsToMany = array(
    'Page' => array(
        'className' => 'Page',
        'joinTable' => 'modules_pages',
        'foreignKey' => 'module_id',
        'associationForeignKey' => 'page_id',
        'unique' => 'keepExisting',
    ),
);

public $hasOne = array(
    'CodeReference' => array(
        'className' => 'CodeReference',
        'foreignKey' => 'code_reference_id',
    ),
);

Wizard Model

    public $hasMany = array(
    'Page' => array(
        'className' => 'Page',
        'foreignKey' => 'wizard_id',
        'dependent' => false,
        'conditions' => 'ModulesPage.wizard_id = Wizard.id',
    )
);

The Controller

$this->loadModel('Page');
    $this->Page->recursive = 1;
    $options = array('Page.wizard_id' => $wizard_id);
    $page = $this->Page->find('first', $options);
    $this->set('page');

    $this->loadModel('ModulesPage');
    $this->ModulesPage->recursive = 2;
    $options = array('ModulesPage.wizard_id ' => $wizard_id,
                     'ModulesPage.page_number' => $page_number,
                     'ModulesPage.enabled' => 1);
    $modules = $this->ModulesPage->find('all', $options);
  • 写回答

1条回答 默认 最新

  • douzi6992 2014-07-04 12:32
    关注

    Start by setting the following in your AppModel: public $recursive = -1;

    What Controller are you putting that code in? You shouldn't need to use loadModel - because you have joined the models together in the model files, you can access like this.

    // access Module model from within PagesController
    $this->Page->Module->find('all', $options);
    

    There is also an error in your $options set-up. Conditions are provided in an array but they need to be inside another array called 'conditions', e.g.

    $options = array(
        'conditions' => array(
            //conditions go here
        )
    );
    

    Finally, after turning off recursive you need to enable containable behaviour. Change the $options array I've just given above as follows (this example would get all Pages along with their Wizards and Modules)

    $options = array(
        'conditions' => array(
            //conditions go here
        ),
        'contain' => array(
            'Wizard',
            'Module'
        )
    );
    $data = $this->Page->find('all', $options);
    

    You can further define additional options for the query with other array keys, e.g. 'order', 'group', 'limit', etc.

    评论

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真