Question

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'));
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top