我的 CakePHP BelongsTo 关系是否创建空白条目?

发布于 2024-10-10 10:27:28 字数 1728 浏览 5 评论 0原文

我有两个数据库表,Homes 和 Assets,具有 HasMany-BelongsTo 关系。

直到最近,每一项资产都属于一个家庭。最近这变得更加复杂:现在一些资产属于房地产经纪人或地区而不是家庭。

今天,我看到我的 Homes 表中出现了数十个空白条目。似乎几乎可以肯定,它们是与房屋无关的新资产被保存的结果。

资产模型中的 BelongsTo Home 关系是否可能强制创建这些空白条目? (或者还有其他可能的原因吗?)如果是关系,是否有一种快速的方法来修改关系,以便资产的生成可以(但不一定必须)与生成新资产齐头并进入户?

已编辑:以下是模型之间的当前关系:

资产:

var $belongsTo = array(
   'Home' =>array('className' => 'Home', 'foreignKey' => 'home_num'));

var $hasOne = array(
   'Landmark' =>array('className' => 'Landmark', 'foreignKey' => 'asset_num',
              'dependent' => true ));

主页:地

var $hasOne = array('HighlightImage' => array(
    'className'     => 'Asset',
    'conditions'    => 'highlight_image = TRUE',
    'order'         => '',
    'foreignKey'    => 'home_num',
    'dependent'     => false,
    'exclusive'     => false,
    'finderQuery'   => ''));

var $hasMany = array('Assets' => array(
    'className'     => 'Asset',
    'conditions'    => '',
    'order'         => '(Assets.title + 0 ) ASC',
    'limit'         => '200',
    'foreignKey'    => 'home_num',
    'dependent'     => false,
    'exclusive'     => false,
    'finderQuery'   => ''));

标:

var $belongsTo = array(
    'Assets' => array(
        'className' => 'Asset',
        'foreignKey' => 'asset_num'),
'Regions' =>array(
        'className' => 'Region',
        'foreignKey' => 'region_num'));

区域:

var $hasMany = array('Landmarks' => array(
     'className' => 'Landmark',
     'foreignKey' => 'region_num'));

I have two database tables, Homes and Assets, with a HasMany-BelongsTo relationship.

Until recently, every Asset belonged to one Home. This has become more complex recently: now some Assets belong to a Realtor or a Region instead of a Home.

Today I've been seeing dozens of blank entries appearing in my Homes table. It seems almost certain that they're the result of new Assets, that are unconnected to a Home, being saved.

Is it likely that the BelongsTo Home relationship in the Asset model is forcing the creation of these blank entries? (Or is there another probable reason?) If it is the relationship, is there a quick way of modifying the relationship so the generation of an Asset can, but doesn't necessary have to, go hand-in-hand with generating a new Home entry?

EDITED: Here are the current relationships between the models:

Asset:

var $belongsTo = array(
   'Home' =>array('className' => 'Home', 'foreignKey' => 'home_num'));

var $hasOne = array(
   'Landmark' =>array('className' => 'Landmark', 'foreignKey' => 'asset_num',
              'dependent' => true ));

Home:

var $hasOne = array('HighlightImage' => array(
    'className'     => 'Asset',
    'conditions'    => 'highlight_image = TRUE',
    'order'         => '',
    'foreignKey'    => 'home_num',
    'dependent'     => false,
    'exclusive'     => false,
    'finderQuery'   => ''));

var $hasMany = array('Assets' => array(
    'className'     => 'Asset',
    'conditions'    => '',
    'order'         => '(Assets.title + 0 ) ASC',
    'limit'         => '200',
    'foreignKey'    => 'home_num',
    'dependent'     => false,
    'exclusive'     => false,
    'finderQuery'   => ''));

Landmark:

var $belongsTo = array(
    'Assets' => array(
        'className' => 'Asset',
        'foreignKey' => 'asset_num'),
'Regions' =>array(
        'className' => 'Region',
        'foreignKey' => 'region_num'));

Region:

var $hasMany = array('Landmarks' => array(
     'className' => 'Landmark',
     'foreignKey' => 'region_num'));

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

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

发布评论

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

评论(1

撕心裂肺的伤痛 2024-10-17 10:27:28

听起来你们之间有所谓的 ARC 关系。这意味着您拥有一个具有多种关系的资产表。正如您上面所描述的,现在资产表似乎将包含属于 3 个不同表的数据:房屋、房地产经纪人或地区。根据我的经验,cake 不能很好地处理 ARC 关系。

我的建议是创建多个资产表:

home_assets
realtor_assets
region_assets

乍一看,您可能认为这太过分了。然而,就简单性而言,这是最好的方法。

您可以使用单个表,但这需要更多的工作。如果您确实想使用单个资产表,则需要添加额外的列(类型)(如果尚未添加)。然后,您需要在每次保存之前将类型添加到资产,具体取决于保存数据的内容:家庭、房地产经纪人或地区。

如果您提供您正在使用的代码示例(针对家庭、房地产经纪人和地区),因为它适用于资产,那么诊断会更容易。考虑使用pastebin.com,因为我认为代码集可能相当大。

It sounds like you have what is know as an ARC relationship. This means you have one asset table that has multiple relationships. As you describe above, it seems that now the Assets table will contain data that can belong to 3 different tables: Homes, Realtors, or Regions. In my experience, cake does not handle ARC relationships very well.

My advice is to create multiple asset tables:

home_assets
realtor_assets
region_assets

At first glance, you may think this is over kill. However, in terms of simplicity, this is the best way to do it.

You can use a single table, but it requires a little more work. If you really want to use a single asset table, you will need to add an additional column (type) if you haven't already. Then you will need to add the type to the asset before each save, depending on what is saving the data: home, realtor, or region.

If you provide a sample of the code you are using (for home, realtor, and region) as it applies to assets, it would be easier to diagnose. Consider using pastebin.com as I assume the code set may be rather large.

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