Cakephp:HasOne 关系

发布于 2024-10-09 00:16:43 字数 1329 浏览 3 评论 0原文

我正在尝试建模以下内容:

旅程有一个 id、一个 fromCity 和一个 toCity。

我的数据库如下所示:

表旅程:

id | fromCity | toCity
1    2           4
2    4           2

来自城市的表:

id  | name
2     paris
4     london     

我为城市和旅程定义了模型。

在我的旅程模型文件中,我想声明一个 $hasOne 归档,以便将 fromCity id 解析为城市名称。

我尝试遵循 hasOne 上的 CakePHP 教程 (http://book.cakephp.org/view/80 /hasOne),但我无法理解如何解析两个外键。

有人可以向我解释一下 var $hasone = ... 必须如何查找这种情况吗?

编辑:模型:

<?php class City extends AppModel { var $name='City';} ?> 

<?php class Journey extends AppModel { var $name='Journey'; /*var $hasOne=array(...)*/} ?>

编辑2:

var $hasOne = array(
   'CityFrom' => array(
      'className'    => 'City',
      'conditions'   => 'Journey.fromAirport = CityFrom.id',
      'dependent'    => true,
      'foreignKey'  => 'id = Journey.fromCity'    ),
   'CityTo' => array (
      'className' => 'City',
      'conditions'   => 'Journey.toCity = CityTo.id',
      'dependent' => true,
      'foreignKey' => 'id = Journey.toCity'
   )
   );

似乎适用于旅程表的第一个条目。所有其他值均为空。我认为问题是由于此查询中有两列具有相同名称而引起的。

I'm trying to model the following:

A journey has an id, a fromCity and a toCity.

My DB looks like this:

Table journey:

id | fromCity | toCity
1    2           4
2    4           2

Table fromCity:

id  | name
2     paris
4     london     

I have models defined for the city and for the journey.

In my model file for the journey I want to declare a $hasOne filed in order resolve the fromCity id to the city's name.

I tried to follow the CakePHP tutorial on hasOne (http://book.cakephp.org/view/80/hasOne) but I can't wrap my head around how to resolve two foreign keys.

Can someone please explain to me how a var $hasone = ... has to look for this case?

Edit: The models:

<?php class City extends AppModel { var $name='City';} ?> 

<?php class Journey extends AppModel { var $name='Journey'; /*var $hasOne=array(...)*/} ?>

Edit 2:

var $hasOne = array(
   'CityFrom' => array(
      'className'    => 'City',
      'conditions'   => 'Journey.fromAirport = CityFrom.id',
      'dependent'    => true,
      'foreignKey'  => 'id = Journey.fromCity'    ),
   'CityTo' => array (
      'className' => 'City',
      'conditions'   => 'Journey.toCity = CityTo.id',
      'dependent' => true,
      'foreignKey' => 'id = Journey.toCity'
   )
   );

Seems to work for the first entry of the journey table. All other's values are null. I think the problem occours from having two columns with the same name in this query.

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

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

发布评论

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

评论(2

弄潮 2024-10-16 00:16:43

正如 Rin 提到的,您需要使用 beLongsTo 而不是 hasOne。这是一个关于 与同一模型的多个关系 来自 Cookbook。希望有帮助。

As Rin mentioned,you need to use beLongsTo instead of hasOne.Here's a example about Multiple relations to the same model from Cookbook.Hope it helps.

一抹微笑 2024-10-16 00:16:43

首先,您的链接指向 1.2 文档,您可能下载了 1.3

1.3 中,您可能还应该在 FromCityToCity 模型上使用 belongsTo (但我可能是错的,长不久前我用过它:)

First of all your link leads to 1.2 docs, you probably downloaded 1.3.

In 1.3, you should probably also use belongsTo on FromCity and ToCity Models (but I could be wrong, long time ago I used it :)

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