[Dbix-class] Easy update for has_many?

Jason Kohles email at jasonkohles.com
Tue Aug 28 21:03:15 GMT 2007


On Aug 25, 2007, at 9:39 AM, Jess Robinson wrote:

>
> On Wed, 22 Aug 2007, Paul Makepeace wrote:
>
>> I have a standard has_many linking multiple groups profiles to a
>> particular user,
>>
>>  My::Schema::User->has_many(user_profile => 'My:Schema::UserProfile',
>> 	{ 'foreign.user_uid' => 'self.uid', } );
>>
>> UserProfile is the link class ("join") table: user_uid, profile_uid
>>
>> I'd like to be able to set a bunch of profile_uids and have the ORM
>> sort out which need deletion and creation. Is this possible?
>
> I'm not missing something am I? My code does the equivalent of this:
>
> $user->user_profiles->delete;
> $user->create_realted('user_profiles', {profile_id => $_}) for  
> @profiles;
>
That's initially how I did it too, then I ran into problems when  
updating a big list where most of the profiles were already there,  
and deleting then recreating them all fired a bunch of triggers and  
generally polluted the database with old rows, causing the vacuuming  
to take place a lot more frequently.  That was the point where I  
rewrote it to the version I posted a couple of days ago, which only  
deletes entries that need to be deleted, and saves the database a lot  
of work.

>> I looked in the cookbook and there's examples on building the
>> relationships ({has,many_to}_many) but not much (anything?) on using
>> them.
>
> ::Manual::Joining should have this sorta stuff in, but may not yet.
>
The code I posted a couple of days ago works pretty well for the  
common case, feel free to stuff it into the manual wherever it seems  
to fit...

-- 
Jason Kohles
email at jasonkohles.com
http://www.jasonkohles.com/
"A witty saying proves nothing."  -- Voltaire





More information about the DBIx-Class mailing list