CakePhp属于关系问题

时间:2012-01-17 16:40:40

标签: cakephp

我有2个模型个人资料和消息。当我尝试在其控制器中获取所有消息时,cake不会返回结果数组中的任何配置文件。 在消息控制器中:

$this->Message->recursive=3;
$m=$this->Message->find('all');

型号:

class Message extends AppModel {    
var $name = 'Message';
var $primaryKey = 'id';
var $useTable = 'messages';
var $belongsTo = array(
  'Profile' => array(
   'className' => 'Profile',
   'foreignKey' => 'author_id',
   'conditions' => '',
   'fields' => '',
       'order' => ''
  ));

 class Profile extends AppModel {   
    var $name = 'Profile';
    var $primaryKey = 'id';
    var $useTable = 'profiles';
    var $belongsTo = array(
  'Account' => array(
   'className' => 'Account',
   'foreignKey' => 'account_id',
   'conditions' => '',
   'fields' => '',
   'order' => ''
  ));
  var $hasMany = array(
      'Message' => array(
           'className' => 'Message',
           'foreignKey' => 'Author_id',
           'dependent' => false,
           'conditions' => '',
           'fields' => '',
           'order' => '',
           'limit' => '',
           'offset' => '',
           'exclusive' => '',
           'finderQuery' => '',
           'counterQuery' => ''
      ));

P.S。我不能等待7个小时的答案选项。 我最终找到了解决方案,我的消息模型文件名是messageS.php,一旦我将其重命名为message.php,就得到了我需要的一切。我帮助了一个人

2 个答案:

答案 0 :(得分:0)

根据您的回答(但也与其他人相关),如果您非常确定您的关联是正确的,则下一步是检查您的文件名以确保他们fit the conventions(奇异模型)。< / p>

另一个注意事项,如果你使用递归3,(不知道它高于2,但显然确实如此),你可能最好使用CakePHP的Containable Behavior。即使使用2的递归通常也是过度的。

答案 1 :(得分:0)

写入Message.php模型文件:

var $ belongsTo = array(

'个人资料'=&gt;阵列(

'className' => 'Profile',

'foreignKey' => 'author_id',

'conditions' => '',

'fields' => '',

'order' => ''

));


在Profile.php模型文件中写入:

var $ hasMany = array(

'讯息'=&gt;阵列(

   'className' => 'Message',

   'foreignKey' => 'Author_id',  // Hear you "Author_id" should change as "author_id"

   'dependent' => false,

   'conditions' => '',

   'fields' => '',

   'order' => '',

   'limit' => '',

   'offset' => '',

   'exclusive' => '',

   'finderQuery' => '',

   'counterQuery' => ''

));


<强>控制器:

$ M = $这 - &GT; MESSAGE-&GT;发现( '全部');

现在它的工作正常......享受..

相关问题