DBIx-Class

 view release on metacpan or  search on metacpan

lib/DBIx/Class/Manual/FAQ.pod  view on Meta::CPAN

sure you write the database name as part of the
L<table|DBIx::Class::ResultSourceProxy::Table/table> call. Eg:

  __PACKAGE__->table('mydb.mytablename');

And load all the Result classes for both / all databases by calling
L<DBIx::Class::Schema/load_namespaces>.

=item .. use DBIx::Class across PostgreSQL/DB2/Oracle schemas?

Add the name of the schema to the table name, when invoking
L<table|DBIx::Class::ResultSourceProxy::Table/table>, and make sure the user
you are about to connect as has permissions to read/write all the
schemas/tables as necessary.

=back

=head2 Relationships

=over 4

=item .. tell DBIx::Class about relationships between my tables?

There are a variety of relationship types that come pre-defined for
you to use.  These are all listed in L<DBIx::Class::Relationship>. If
you need a non-standard type, or more information, look in
L<DBIx::Class::Relationship::Base>.

=item .. define a one-to-many relationship?

This is called a C<has_many> relationship on the one side, and a
C<belongs_to> relationship on the many side. Currently these need to
be set up individually on each side. See L<DBIx::Class::Relationship>
for details.

=item .. define a relationship where this table contains another table's primary key? (foreign key)

Create a C<belongs_to> relationship for the field containing the
foreign key.  See L<DBIx::Class::Relationship/belongs_to>.

=item .. define a foreign key relationship where the key field may contain NULL?

Just create a C<belongs_to> relationship, as above. If the column is
NULL then the inflation to the foreign object will not happen. This
has a side effect of not always fetching all the relevant data, if you
use a nullable foreign-key relationship in a JOIN, then you probably
want to set the C<join_type> to C<left>.

=item .. define a relationship where the key consists of more than one column?

Instead of supplying a single column name, all relationship types also
allow you to supply a hashref containing the condition across which
the tables are to be joined. The condition may contain as many fields
as you like. See L<DBIx::Class::Relationship::Base>.

=item .. define a relationship bridge across an intermediate table? (many-to-many)

The term 'relationship' is used loosely with many_to_many as it is not considered a
relationship in the fullest sense.  For more info, read the documentation on L<DBIx::Class::Relationship/many_to_many>.

=item .. stop DBIx::Class from attempting to cascade deletes on my has_many and might_have relationships?

By default, DBIx::Class cascades deletes and updates across
C<has_many> and C<might_have> relationships. You can disable this
behaviour on a per-relationship basis by supplying
C<< cascade_delete => 0 >> in the relationship attributes.

The cascaded operations are performed after the requested delete or
update, so if your database has a constraint on the relationship, it
will have deleted/updated the related records or raised an exception
before DBIx::Class gets to perform the cascaded operation.

See L<DBIx::Class::Relationship>.

=item .. use a relationship?

Use its name. An accessor is created using the name. See examples in
L<DBIx::Class::Manual::Cookbook/USING RELATIONSHIPS>.

=back

=head2 Searching

=over 4

=item .. search for data?

Create a C<$schema> object, as mentioned above in L</.. connect to my
database?>. Find the
L<ResultSet|DBIx::Class::Manual::Glossary/ResultSet> that you want to
search in, by calling C<< $schema->resultset('MySource') >> and call
C<search> on it. See L<DBIx::Class::ResultSet/search>.

=item .. search using database functions?

Supplying something like:

 ->search({'mydatefield' => 'now()'})

to search, will probably not do what you expect. It will quote the
text "now()", instead of trying to call the function. To provide
literal, unquoted text you need to pass in a scalar reference, like
so:

 ->search({'mydatefield' => \'now()'})

=item .. sort the results of my search?

Supply a list of columns you want to sort by to the C<order_by>
attribute. See L<DBIx::Class::ResultSet/order_by>.

=item .. sort my results based on fields I've aliased using C<as>?

You didn't alias anything, since L<as|DBIx::Class::ResultSet/as>
B<has nothing to do> with the produced SQL. See
L<DBIx::Class::ResultSet/select> for details.

=item .. group the results of my search?

Supply a list of columns you want to group on, to the C<group_by>
attribute, see L<DBIx::Class::ResultSet/group_by>.

=item .. group my results based on fields I've aliased using C<as>?

You don't. See the explanation on ordering by an alias above.

=item .. filter the results of my search?

The first argument to C<search> is a hashref of accessor names and
values to filter them by, for example:



( run in 1.425 second using v1.01-cache-2.11-cpan-d8267643d1d )