Basset

 view release on metacpan or  search on metacpan

lib/Basset/Object/Persistent.pm  view on Meta::CPAN

	if ($self->tied_to_parent && ! $self->should_be_deleted) {
		return $self;
	};

	$self->deleting(1);

	my $table	= $self->primary_table or return $self->error("Cannot delete with no table", "BOP-01");

	$self->begin() or return;

	my $query = $table->attach_to_query(
		$table->delete_query(),
		{
			'where' => join(' and ', map {"$_ = ?"} $table->primary_cols)
		}
	) or return $self->error($table->errvals);

	my @values = map {$self->$_()} $table->alias_column($table->delete_bindables) or return;

	$self->arbitrary_sql(
		'query'	=> $query,
		'vars'	=> \@values,
		'table'	=> $table,
		'cols'	=> [$table->delete_bindables]
	) or return;

	$self->delete_relationships or return;

	$self->end or return;

	$self->deleting(0);

	$self->deleted(1);

	return $self;

};


=pod

=item load_where

Simple wrapper around load_all. Takes key/value pairs.

 my $users = Some::Class->load_where(
 	'user' 		=> 3,
 	'location'	=> 'mountains',
 	'weather'	=> 'sunny'
 );

This is exactly equivalent to:

 my $users = Some::Class->load_all(
 	{
 		'where' => 'user = ? and location = ? and weather = ?'
 	},
 	3, 'mountains', 'sunny'
 );

It just looks prettier and hides more of the SQL.

Even better, you can also stick in an array for multiple value loads.

 my $users = Some::Class->load_where(
 	'state'		=> 'PA',
 	'last_name' => [qw(Smith Jones Johnson)]
 );

Is exactly the same as:

 my $users = Some::Class->load_all(
 	{
 		'where' => 'last_name in (?,?,?) and state = ?'
 	},
 	qw(Smith Jones Johnson), 'PA',
 );

There is an alternative syntax, you may pass in one arrayref and one hashref. The arrayref becomes your
where clause, the second contains additional loader args (such as 'order by', 'limit', etc.)

 my $users = Some::Class->load_where(
 	#where array
 	[
 		'state'		=> 'PA',
 		'last_name' => [qw(Smith Jones Johnson)]
 	],
 	#extra loader hash
 	{
 		'order by' => 'state desc',
 	},
 );

Is exactly the same as:

 my $users = Some::Class->load_all(
 	{
 		'where' => 'last_name in (?,?,?) and state = ?',
 		'order by' => state desc',
 	},
 	qw(Smith Jones Johnson), 'PA',
 );


=cut

sub load_where {
	my $class = shift;

	my @clauses = @_ or return $class->error("Cannot load_where w/o clauses", "BOP-68");

	my $additional_clauses = {};

	if (ref $clauses[0] eq 'HASH') {
		$class->notify('warnings', 'load_where with a hashref argument is deprecated. Please load with an array instead.');
		$clauses[0] = [%{$clauses[0]}];
	};

	if (ref $clauses[0] eq 'ARRAY') {
		$additional_clauses = @clauses == 2 ? pop @clauses : {};	#last one is additional clauses
		@clauses = @{$clauses[0]};



( run in 0.738 second using v1.01-cache-2.11-cpan-ff9377addf4 )