[CDBI] find_or_create() usage or feature request

Matt S Trout dbix-class at trout.me.uk
Thu May 24 22:56:58 BST 2007


On Thu, May 24, 2007 at 02:01:57PM -0700, Carlos Ramirez wrote:
> I like the functionality of find_or_create() however, i think it could
> be better if it allowed you specify the search criteria and the actual
> record data as seperate parameters like:
> 
>   ## find_or_create(\%search_criteria [,\%new_record]);
>   Module->find_or_create({user_name => 'einstein'}, 
>      { user_name => 'einstein', field => 'physics', fname => 'Albert'});
> 
> For instance, if I want to find or create a new record where I only want
> to check that the user_name field doesn't already exist, before I insert
> the record. The current implementation requires that you specify all the
> new fields of the new record which are used as conditions for a search
> ... right?

DBIx::Class handles this by using either the PK or a specified unique
constraint for the 'find' part, so you'd do e.g.

$rs->find_or_create({ user_name => 'einstein', field => 'physics', fname => 'Albert'});

# would only search user_name assuming that's the PK

or

$rs->find_or_create({ user_name => 'einstein', email_address => 'einstein at example.com' }, { key => 'email' });

# would search by email_address assuming you've added a unique constraint
# called email containing only that column

Personally, I prefer that style to what you're proposing, but both work well
enough so I guess it's a matter of taste.

-- 
      Matt S Trout       Need help with your Catalyst or DBIx::Class project?
   Technical Director    Want a managed development or deployment platform?
 Shadowcat Systems Ltd.  Contact mst (at) shadowcatsystems.co.uk for a quote
http://chainsawblues.vox.com/             http://www.shadowcatsystems.co.uk/ 



More information about the ClassDBI mailing list