【问题标题】:retrieving the 'belongsTo' part of model data in cakephp在 cakephp 中检索模型数据的“belongsTo”部分
【发布时间】:2012-09-15 00:18:57
【问题描述】:

也许是因为我累死了,但我在这里完全错过了一些东西。我从一个朋友那里继承了一个蛋糕 1.3 应用程序,他正确设置了他的桌子,但我丢失了一些数据。当我打印数据数组时,我期望的是:

 Array
(
[Listing] => Array
    (
        [id] => 74
        [listing_name] => El Toro
        [category_id] => 3
        [location_id] => 2
        [long_desc] => 
    ),

[Category] => Array
    (
        [id] => 3
        [category_name] => Restaurant

    ),
)

(列出属于类别)。控制器函数定义如下:

function view_detail(){

    $this->set('um','true');
    $l=$this->Listing->find('first', array('conditions'=>array('id'=>$this->params['cid'])));
    $this->set('lst', $l);


}

相反,我得到了这个:

 Array
(
[Listing] => Array
    (
        [id] => 74
        [listing_name] => El Toro
        [category_id] => 3
        [location_id] => 2
        [long_desc] => 
    )
)

查看他设置的数据库表,所有的蛋糕的约定都遵循:listings 有 category_id,categories 有 listing_id。我相信不久前有人告诉我,如果您有简单的千篇一律的模型/关联,您甚至不需要设置模型页面,因为蛋糕会自动知道关联。但无论如何我都做了,并没有什么不同。我尝试将递归一直设置为 2,但没有任何区别。 Sooooo ....不擅长cakephp,我不知道我做错了什么。顺便说一下,我正在寻找列出列表的类别名称(餐厅)。

更新

这是我的 2 个模型:

class Category extends AppModel{

var $hasMany = array( 'Listing' );

}


class Listing extends AppModel{
var $name='Listing';
var $belongsTo = array( 'Location'=>array('foreignKey'=>'location_id'),   'Category'=>array('foreignKey'=>'category_id') );

}

【问题讨论】:

  • categories.listing_id 错误/不必要。您确定在列表模型中有public $belongsTo = array('Category');,在Category 模型中有public $hasMany = array('Listing');
  • categories.listing_id 只是在我输入这些数组时意外包含在内(实际上不是在数据库表中,抱歉!)。 Listing Model 和 Category 模型都具有正确的语法及 belongsTo 和 hasMany。这是 cake 1.3,所以我只使用 var $hasMany 等,而不是 public $hasMany。
  • debug(get_class($this->Listing)) 的输出是什么?是 AppModel 还是列表?递归应该可以工作,但尝试 Containable 行为不会有什么坏处。
  • 那是你的问题。列表模型的文件名、路径(相对于您的应用目录)和类名是什么
  • 它是 /app/models/Listing.php,我没有在 Listing.php 或 Category.php 中声明类名

标签: model cakephp-1.3 belongs-to cakephp-model


【解决方案1】:

在 1.x 模型文件中应该是小写和下划线,所以app/model/listing.php。 当您的模型类显示为 AppModel 时,这意味着 cake 没有找到您的模型类并基于 AppModel 制作通用模型。

【讨论】:

  • 就是这样。我一直在使用 Cake 2.x 的命名约定
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多