Quantcast

Shouldn't saveAll remove associated records as well?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Shouldn't saveAll remove associated records as well?

jelmer-3

Hi all,

I use saveAll to save a 'wordset' and it's associated words. In fact,
when reading the record (recursively), I create an extra field in the
wordSet model that contains the associated words, seperated by a
newline, for easy editing. When saving the wordSet, I recreate the
[Word] array for the associated words by looking them up (to make sure
they keep their old ids) and putting them in the $data['Word'] array.

Then I call saveAll, using atomic = true and validate = first. All
works fine. New words are added, existing words are saved under the
same id (some other stuff can change at that time). But... words that
are *removed* from the wordset are not deleted in the Word table.

I think that there should be an option in saveAll that would do just
this: remove any associated records in the associated hasMany table
that (1) have the same foreign key value as the edited main record and
(2) do not exist in the array for the associated sub records.

I even think that if the relation is defined as 'exclusive' AND
'dependent', this should be done automatically, to avoid orphan
records...

My relationships are defined as follows:

Class WordSet extends AppModel {
        var $transactional = true;
        var $cacheQueries = true;
        var $order = 'WordSet.modified desc';

        var $hasMany = array(
                'Word' => array(
                        'exclusive' => true,
                        'dependent' => true)
                );
...
}

class Word extends AppModel {
        var $cacheQueries = true;
        var $belongsTo = 'WordSet';
        var $order = 'Word.word_set_id asc, Word.woord asc';
...
}


Any thoughts?

Jelmer

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Shouldn't saveAll remove associated records as well?

Erik Kristensen

As far as I know not in the setup you have going ... if you where
using a HABTM with unique => true, then yes that is what would happen.

-Erik

On Jan 26, 6:10 am, Jelmer <[hidden email]> wrote:

> Hi all,
>
> I use saveAll to save a 'wordset' and it's associated words. In fact,
> when reading the record (recursively), I create an extra field in the
> wordSet model that contains the associated words, seperated by a
> newline, for easy editing. When saving the wordSet, I recreate the
> [Word] array for the associated words by looking them up (to make sure
> they keep their old ids) and putting them in the $data['Word'] array.
>
> Then I call saveAll, using atomic = true and validate = first. All
> works fine. New words are added, existing words are saved under the
> same id (some other stuff can change at that time). But... words that
> are *removed* from the wordset are not deleted in the Word table.
>
> I think that there should be an option in saveAll that would do just
> this: remove any associated records in the associated hasMany table
> that (1) have the same foreign key value as the edited main record and
> (2) do not exist in the array for the associated sub records.
>
> I even think that if the relation is defined as 'exclusive' AND
> 'dependent', this should be done automatically, to avoid orphan
> records...
>
> My relationships are defined as follows:
>
> Class WordSet extends AppModel {
>         var $transactional = true;
>         var $cacheQueries = true;
>         var $order = 'WordSet.modified desc';
>
>         var $hasMany = array(
>                 'Word' => array(
>                         'exclusive' => true,
>                         'dependent' => true)
>                 );
> ...
>
> }
>
> class Word extends AppModel {
>         var $cacheQueries = true;
>         var $belongsTo = 'WordSet';
>         var $order = 'Word.word_set_id asc, Word.woord asc';
> ...
>
> }
>
> Any thoughts?
>
> Jelmer
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Shouldn't saveAll remove associated records as well?

AD7six
In reply to this post by jelmer-3



On Jan 26, 12:10 pm, Jelmer <[hidden email]> wrote:

> Hi all,
>
> I use saveAll to save a 'wordset' and it's associated words. In fact,
> when reading the record (recursively), I create an extra field in the
> wordSet model that contains the associated words, seperated by a
> newline, for easy editing. When saving the wordSet, I recreate the
> [Word] array for the associated words by looking them up (to make sure
> they keep their old ids) and putting them in the $data['Word'] array.
>
> Then I call saveAll, using atomic = true and validate = first. All
> works fine. New words are added, existing words are saved under the
> same id (some other stuff can change at that time). But... words that
> are *removed* from the wordset are not deleted in the Word table.
>
> I think that there should be an option in saveAll that would do just
> this: remove any associated records in the associated hasMany table
> that (1) have the same foreign key value as the edited main record and
> (2) do not exist in the array for the associated sub records.
>
> I even think that if the relation is defined as 'exclusive' AND
> 'dependent', this should be done automatically, to avoid orphan
> records...
>
> My relationships are defined as follows:
>
> Class WordSet extends AppModel {
>         var $transactional = true;
>         var $cacheQueries = true;
>         var $order = 'WordSet.modified desc';
>
>         var $hasMany = array(
>                 'Word' => array(
>                         'exclusive' => true,
>                         'dependent' => true)
>                 );
> ...
>
> }
>
> class Word extends AppModel {
>         var $cacheQueries = true;
>         var $belongsTo = 'WordSet';
>         var $order = 'Word.word_set_id asc, Word.woord asc';
> ...
>
> }
>
> Any thoughts?

Isn't an alarm bell ringing that you're calling a method called
saveAll and wanting it to delete?

AD
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Shouldn't saveAll remove associated records as well?

jelmer-3



> Isn't an alarm bell ringing that you're calling a method called
> saveAll and wanting it to delete?

Well yes and no. When you are saving say an article, you use saveAll
to save the article with all its tags, comments and all. These are not
HABTM, but simple 1:n hasMany relationships. So when a user has
deleted a tag with the article, saving of the changes actually means
deleting the tag (that is, the record that says that tag X belongs to
articly Y).

This is what the normal forms imply. The tag-to-article table in this
example is fully dependent on the article record. When I save the
article together with all its tags (saveAll), there should be no tags
in the database that are not in this save, but still connected to the
article.

Maybe a better name for the function should be "replaceAll" or
"saveFull" or something, but it really is what one expects to happen.
Right?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Shouldn't saveAll remove associated records as well?

Erik Kristensen

Negative, once again you are not using cake model as it is intended,
if you really truely want to have this behavior ... you need to have
the following setup ...

tables
-----------
articles
tags
articles_tags

associations
--------------------
article HABTM tag
tags HABTM article

set the unique variable to true in the association

now when you do a saveAll, any current tags associated with the
article will be deleted first, then the ones you have selected before
the saveAll will be inserted into the articles_tags table, thus
effectively deleting any tags you odn't want anymore ..

-Erik


On Jan 27, 6:22 am, Jelmer <[hidden email]> wrote:

> > Isn't an alarm bell ringing that you're calling a method called
> > saveAll and wanting it to delete?
>
> Well yes and no. When you are saving say an article, you use saveAll
> to save the article with all its tags, comments and all. These are not
> HABTM, but simple 1:n hasMany relationships. So when a user has
> deleted a tag with the article, saving of the changes actually means
> deleting the tag (that is, the record that says that tag X belongs to
> articly Y).
>
> This is what the normal forms imply. The tag-to-article table in this
> example is fully dependent on the article record. When I save the
> article together with all its tags (saveAll), there should be no tags
> in the database that are not in this save, but still connected to the
> article.
>
> Maybe a better name for the function should be "replaceAll" or
> "saveFull" or something, but it really is what one expects to happen.
> Right?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Shouldn't saveAll remove associated records as well?

jelmer-3

Hi There,

Thanks for the discussion. I understand the HABTM situation, using a
join table. But in my original setup, there is this WordSet and a
Words table. No join, repeating words are just repeated. So this is a

WordSet hasmany Word

WordSet: id, name etc.
Word: id, word_set_id, word etc.

setup.

In the usual blog example, this would be an article with a comment

article hasMany comment
comment belongsTo article.

I created this page for my Wordsets where all words are joined in a
big textarea. When the user submits the form, I check which words are
gone, which are still there and which are new. So I recreate the $this-
>data->['Word'] array.
Then I do a $this->WordSet->saveAll($this->data). Why would the Word
records with word_set_id = (the one that is saved), that are not in
$this->data be kept in the database.

I understand this is a special case. It is far more usual to have
HABTM-associations added to a 'main' form. One usualy uses subforms,
ajax etc for hasMany associations. But not in my case, in which speed
of input is important, as well as atomic saves.

Back to the blog example, this would be a setup where one could add
and remove comments on the page containing the (editable) article as
well. It is not uncommon using dhtml or prototype to add or remove a
textarea to change the comment collection. On submit, one would check
which of the original comments are gone, and change the $data
accordingly, then call saveAll on the article.

I fixed it now figuring out myself which records should go, and this
within a transaction. But because of the relational structure I really
think when the user says saveAll (i.e. save my record and all the
associations), the result should be relationally correct.

b.t.w I think I am using the cake model as it is intended: this is a
hasMany case. Sorry for the confusion started by me adding 'tags' to
the example. Tags carry the habtm tag..

Jelmer


On 27 jan, 16:13, Esoteric <[hidden email]> wrote:
> Negative, once again you are not using cake model as it is intended,
> if you really truely want to have this behavior ... you need to have
> the following setup ...
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Shouldn't saveAll remove associated records as well?

mark_story
In reply to this post by jelmer-3

On Jan 26, 6:10 am, Jelmer <[hidden email]> wrote:

> Hi all,
>
> I use saveAll to save a 'wordset' and it's associated words. In fact,
> when reading the record (recursively), I create an extra field in the
> wordSet model that contains the associated words, seperated by a
> newline, for easy editing. When saving the wordSet, I recreate the
> [Word] array for the associated words by looking them up (to make sure
> they keep their old ids) and putting them in the $data['Word'] array.
>
> Then I call saveAll, using atomic = true and validate = first. All
> works fine. New words are added, existing words are saved under the
> same id (some other stuff can change at that time). But... words that
> are *removed* from the wordset are not deleted in the Word table.

Yes because its saveAll() not makeIdenticalToPostedData()  If you want
to delete everything first.  Use deleteAll() inside a transaction.

> I think that there should be an option in saveAll that would do just
> this: remove any associated records in the associated hasMany table
> that (1) have the same foreign key value as the edited main record and
> (2) do not exist in the array for the associated sub records.

Patches are always welcomed :) Write a patch and some test cases and
it has a much better chance of  being  added. But personally I don't
have a need for this so I'm not going to build it.

-Mark

> I even think that if the relation is defined as 'exclusive' AND
> 'dependent', this should be done automatically, to avoid orphan
> records...
>
> My relationships are defined as follows:
>
> Class WordSet extends AppModel {
>         var $transactional = true;
>         var $cacheQueries = true;
>         var $order = 'WordSet.modified desc';
>
>         var $hasMany = array(
>                 'Word' => array(
>                         'exclusive' => true,
>                         'dependent' => true)
>                 );
> ...
>
> }
>
> class Word extends AppModel {
>         var $cacheQueries = true;
>         var $belongsTo = 'WordSet';
>         var $order = 'Word.word_set_id asc, Word.woord asc';
> ...
>
> }
>
> Any thoughts?
>
> Jelmer
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Loading...