【问题标题】:Can't find model associated model data找不到模型相关的模型数据
【发布时间】:2014-06-26 08:36:59
【问题描述】:

我是 CakePHP 的新手,我在构建一个要学习的示例时遇到问题。

我正在构建一个包含 3 个模型(Categoria、Plato、Imagen)的插件

它们之间的关系如下: 分类 - 柏拉图 (n-n) 柏拉图 - Imagen (1-n)

如果我去 Plato 视图,我会通过关系获取所有 Imagen,但是当我通过一个类别访问时,我不能只访问与每个 Plato 关联的 Imagen。为什么?有什么问题?

型号代码:

柏拉图:

App::uses('AppModel', 'Model');
class Plato extends ErestaurantAppModel {   
    public $name = 'Plato';
    //public $actsAs = array('Containable');
    public $hasAndBelongsToMany = array(
        'Categoria' => array(
            'className' => 'Categoria',
            'joinTable' => 'categorias_platos',
            'foreignKey' => 'plato_id',
            'associationForeignKey' => 'categoria_id',
            'unique' => 'keepExisting',
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
        )
    );
    public $hasMany = array('Imagen' =>
                            array('foreingkey' => array('plato_id')));
}

类别:

App::uses('AppModel', 'Model');
class Categoria extends ErestaurantAppModel {
    public $name = 'Categoria';
    //public $actsAs = array('Containable');
    public $hasAndBelongsToMany = array(
        'Plato' => array(
            'className' => 'Plato',
            'joinTable' => 'categorias_platos',
            'foreignKey' => 'categoria_id',
            'associationForeignKey' => 'plato_id',
            'unique' => 'keepExisting',
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
        )
    );
}

图像:

App::uses('AppModel', 'Model');
class Imagen extends ErestaurantAppModel {  
    public $name = 'Imagen';
    //public $actsAs = array('Containable');
    public $belongsTo = array('Plato');
}

最后,当我进入分类视图时,我要启动的代码。

App::uses('AppController', 'Controller');
class CategoriasController extends ErestaurantAppController {
    public $uses = array('Erestaurant.Categoria');

    public function index() {
        $this->Categoria->recursive = 0;
        $this->set('data', $this->Categoria->find('all'));
    }

    public function view($id = null) {
        if (!$this->Categoria->exists($id)) {
            throw new NotFoundException(__('Registro inválido.'));
        }
        /*
        $this->Categoria->recursive = -1;
        $options = array('conditions' => 
                            array('Categoria.'.$this->Categoria->primaryKey => $id),
                         'contain' => array(
                            'Plato' => array(
                                'Imagen'
                            )
                         )
                        );              
        */
        $this->Categoria->recursive = 2;
        $options = array('conditions' => 
                            array('Categoria.'.$this->Categoria->primaryKey => $id));
        $this->set('item', $this->Categoria->find('first', $options));


    }

}

您看到的注释代码是我尝试使用 Containable 的另一种方式,但最后我收到错误“Plato is not associated with model Imagen”

数据库似乎一切正常,每个表都根据需要创建(categorias、platos、imagens、categorias_platos)并创建了所有foering键并尝试保持 CakePHP 的默认名称。

谢谢!

【问题讨论】:

    标签: cakephp recursion model relationship


    【解决方案1】:

    要访问深层关系,您必须使用可包含的行为。您的模型中有一个可包含行为的定义,但出于某种原因,它被注释了。

    public $actsAs = array('Containable');
    

    在您的类别中取消注释此行后,您可以通过以下方式访问深层关系:

    $this->Categoria->find('all', array(
        'contain'=>array(
            'Plato.Imagen'
        )
    ));
    

    ...或类似的东西。欲了解更多信息,请访问此链接Containable Behavior

    【讨论】:

    • 我开始使用 Containable Behavior,但后来我收到一条消息,告诉我 Plato 和 Imagen 模型没有关联:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    • 1970-01-01
    相关资源
    最近更新 更多