CakePHP bindModel 工作,预设模型关联不工作

发布于 2024-10-14 20:53:06 字数 1308 浏览 1 评论 0原文

我正在开发一个个人项目。

我有两个模型“Show”和“Episode”。我有一个控制器“Ops”。

显示模型:

class Show extends AppModel
{
 var $name = 'Show';

 var $hasMany = array(
  'Episode' => array(
   'className' => 'Episode',
   'foreignKey' => 'show_id'
  )
 );
}

剧集模型:

class Episode extends AppModel
{
 var $name = 'Episode';

 var $belongsTo = array(
  'Show' => array(
   'className' => 'Show',
   'foreignKey' => 'show_id'
  )
 ); 
}

Ops 控制器:

class OpsController extends AppController
{
 var $name = 'Ops';
 var $uses = array('Show','Episode');

 function index()
 {
  $episodes = $this->Episode->find('all',array(
   'limit' => 10,
   'order' => array('Episode.first_aired' => 'DESC'),
   )
  );
  debug($this->Episode);
  debug($episodes);

 }
}

运行 Ops 控制器时,我得到了我想要的“Episode”记录,但没有根据“belongsTo”配置中的“show_id”获得关联的“Show”记录。看起来它根本没有引用模型,因为我可以故意破坏模型类,而请求仍然继续。

经过大量检查、研究和测试后,我能够通过在 find() 请求之前将以下内容添加到 Ops 控制器中来使其工作:

$this->Episode = ClassRegistry::init('Episode');
$this->Episode->bindModel(
 array('belongsTo' => array(
   'Show' => array(
    'className' => 'Show'
   )
  )
 )
);

现在,虽然这有效,但我仍然想知道为什么我的模型不工作被正确调用。任何帮助将不胜感激。谢谢!

I am in development of a personal project.

I have two models 'Show' and 'Episode'. I have one controller 'Ops'.

Show model:

class Show extends AppModel
{
 var $name = 'Show';

 var $hasMany = array(
  'Episode' => array(
   'className' => 'Episode',
   'foreignKey' => 'show_id'
  )
 );
}

Episode model:

class Episode extends AppModel
{
 var $name = 'Episode';

 var $belongsTo = array(
  'Show' => array(
   'className' => 'Show',
   'foreignKey' => 'show_id'
  )
 ); 
}

Ops controller:

class OpsController extends AppController
{
 var $name = 'Ops';
 var $uses = array('Show','Episode');

 function index()
 {
  $episodes = $this->Episode->find('all',array(
   'limit' => 10,
   'order' => array('Episode.first_aired' => 'DESC'),
   )
  );
  debug($this->Episode);
  debug($episodes);

 }
}

When running the Ops controller I get the 'Episode' records like I want but don't get the associated 'Show' record based on the 'show_id' in the 'belongsTo' configuration. It appears that it is not referencing the model at all as I can purposefully break the model class an the request still goes on.

After doing a lot of checking, researching, and testing, I was able to get it to work by adding the following into the Ops controller before the find() request:

$this->Episode = ClassRegistry::init('Episode');
$this->Episode->bindModel(
 array('belongsTo' => array(
   'Show' => array(
    'className' => 'Show'
   )
  )
 )
);

Now while this works I would still like to know why my models are not being called properly. Any help would be most appreciated. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北方的巷 2024-10-21 20:53:06

如果您以同样的方式查询 Show 会发生什么?

您确定 id 字段定义正确吗?

在两个表上,您都应该有 id(INTsize),在episodes 上,还应该有 show_id(INTsize)。

如果它是根据 Cake 约定设置的,您应该能够删除 'foreignKey' =>; 'show_id' 行,Cake 会自行解决。

What happens if you query Show in the same way?

Are you certain the id fields are defined correctly?

On both tables you should have id(INTsize) and on episodes there should also be show_id(INTsize).

If it's set up according to Cake convention, you should be able to remove the 'foreignKey' => 'show_id' line and Cake will sort it out itself.`

一腔孤↑勇 2024-10-21 20:53:06

听起来 Cake 没有使用您的模型文件,而是根据表格自动生成一些模型文件。
这听起来很愚蠢,但请检查文件夹和文件名是否有拼写错误以及它们是否为小写。

It sounds like Cake isn't using your model files and instead automagically generating some based on the tables.
It sounds dumb, but check the folders and file names for spelling errors and that they are lowercase.

め七分饶幸 2024-10-21 20:53:06

在您的 Show 模型中,您将 Episode 外键设置为 show_id,它应该是 Episode_id。但是,我不认为这是造成问题的原因。

据我所知,您没有更改任何 CakePHP 命名约定,因此只需删除定义关联的数组并保留为字符串,例如,

class Show extends AppModel
{
 var $name = 'Show';

 var $hasMany = array(
  'Episode'
 );
}

class Episode extends AppModel
{
 var $name = 'Episode';

 var $belongsTo = array(
  'Show'
 ); 
}

这可能不起作用,但我之前遇到过类似的问题,这解决了它。祝你好运。

In your Show model, you have the Episode foreign key set to show_id, which should be episode_id. However, I don't think that is causing the problem.

You aren't changing any CakePHP naming conventions, from what I can tell, so just remove the arrays that define the association and leave as string, e.g.

class Show extends AppModel
{
 var $name = 'Show';

 var $hasMany = array(
  'Episode'
 );
}

class Episode extends AppModel
{
 var $name = 'Episode';

 var $belongsTo = array(
  'Show'
 ); 
}

This may not work, but I have bumped into similar issues before and this resolved it. Good luck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文