Jump to content

Recommended Posts

Hi,

 

I have a belongsTo relationship with a counterCache set correctly. The problem being that this relationship is not always there.

 

For example Users don't always have a link to an address so the DB allows nulls.

 

But the counterCache is always trying to update, even when the foreign key is NULL.

 

E.g.

 

UPDATE [addresses] SET [user_count] = 0 WHERE [id] IS N''

 

Is there something I can put into the counterScope to stop this error from happening on NULL id's??

 

Thanks in advance.

 

Link to comment
https://forums.phpfreaks.com/topic/278764-cakephp-countercache/
Share on other sites

Hi,

 

No, it is the counterCache which is trying to do the UPDATE.

 

I have 3 functions in my UsersController:

 

add which does a $this->User->save()

edit which does a $this->User->save()

delete which does a $this->User->delete()

 

when I use the delete function, it deletes the User. But the counterCache tries to UPDATE the address_count field on the Users table with an id of NULL.

 

The save()'s on the add / edit do not produce this error and run correctly.

 

Hope this explains it better.

Sorry got myself all in a muddle, it should of read...

 

"No, it is the counterCache which is trying to do the UPDATE.

 

I have 3 functions in my UsersController:

 

add which does a $this->User->save()

edit which does a $this->User->save()

delete which does a $this->User->delete()

 

when I use the delete function, it deletes the User. But the counterCache tries to UPDATE the user_count field on the Addresses table with an id of NULL.

 

The save()'s on the add / edit do not produce this error and run correctly.

 

Hope this explains it better."

 

Sorry.

Yes as I wanted to make the DB relational so I split the tables so an Address hasMany Users.

 

This is for a building management issue logging system. So we may have 40 people that work out of one building at one address that need to log building issues.

 

For example: My light bulb needs replacing.

 

Thanks,

We have no idea how your database is laid-out and we have no idea what $this->User->delete() actually does, so we have no idea how to help.

 

In a relational design with the requirements you describe, I would have the following three tables:

 

Users: ID (Primary Key), detail user data

Addresses: ID (Primary Key), detail address data

UserAddresses: UserID (Foreign Key), AddressID (Foreign Key)

 

Then the delete process would be:

1) Delete from UserAddress Where UserID = <the user being deleted>

2) Delete from Users Where ID = <the user being deleted>

 

You would then have to deal with Addresses that are left with no Users assigned. Depending on your requirements, you might leave them, or use some process to "clean them out".

 

I don't know why or where you have an "UserCount" column. In the design above, to find out how many Users are associated with any given address, you do a Select COUNT() query with a Join. You should not try to maintain a count field.

If you'd explained some of this up front it would have gone a lot faster.

It probably has to do with the configuration of your relationships, which you haven't shown.

I suggest you read the links in my signature on how to get help.

 

David: counterCache is supposed to handle that count automatically - hence why it's trying to update the userCount automatically when the users table is updated. It's a tool for CakePHP.

Hi,

 

Addresses -> $hasMany -> Users

Users -> $belongsTo -> Addresses

 

I do not need a UserAddresses table as that would suggest a HABTM relationship which is not needed here.

 

All I want to know is why the counterCache is trying to update when I use the $this->Model->delete() and not when I use $this->Model->save

when the foreign_key is NULL???

Edited by 244863
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.