NAME

DBIx::Class::Manual::FAQ - Frequently Asked Questions (in theory)

DESCRIPTION

This document is intended as an anti-map of the documentation. If you know what you want to do, but not how to do it in DBIx::Class, then look here. It does not contain any code or examples, it just gives explanations and pointers to the correct pieces of documentation to read.

FAQs

How Do I:

Getting started

.. create a database to use?

First, choose a database. For testing/experimenting, we reccommend DBD::SQLite, which is a self-contained small database. (i.e. all you need to do is to install the DBD from CPAN, and it's usable).

Next, spend some time defining which data you need to store, and how it relates to the other data you have. For some help on normalisation, go to http://b62.tripod.com/doc/dbbase.htm or http://209.197.234.36/db/simple.html.

Now, decide whether you want to have the database itself be the definitive source of information about the data layout, or your DBIx::Class schema. If it's the former, look up the documentation for your database, eg. http://sqlite.org/lang_createtable.html, on how to create tables, and start creating them. For a nice universal interface to your database, you can try DBI::Shell. If you decided on the latter choice, read the FAQ on setting up your classes manually, and the one on creating tables from your schema.

.. use DBIx::Class with Catalyst?

Install Catalyst::Model::DBIC::Schema from CPAN. See it's documentation, or below, for further details.

.. set up my DBIx::Class classes automatically from my database?

Install DBIx::Class::Schema::Loader from CPAN, and read it's documentation.

.. set up my DBIx::Class classes manually?

Look at the DBIx::Class::Manual::Example, come back here if you get lost.

.. create my database tables from my DBIx::Class schema?

Create your classes manually, as above. Write a script that calls "deploy" in DBIx::Class::Schema. See there for details, or the DBIx::Class::Manual::Cookbook.

.. connect to my database?

Once you have created all the appropriate table/source classes, and an overall DBIx::Class::Schema class, you can start using them in an application. To do this, you need to create a central Schema object, which is used to access all the data in the various tables. See "connect" in DBIx::Class::Schema for details. The actual connection does not happen until you actually request data, so don't be alarmed if the error from incorrect connection details happens a lot later.

Relationships

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

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

.. define a one-to-many relationship?

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

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

Create a belongs_to relationship for the field containing the foreign key. "belongs_to" in DBIx::Class::Relationship.

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

Just create a 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 join_type to 'left'.

.. 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 DBIx::Class::Relationship::Base.

.. define a relatiopnship across an intermediate table? (many-to-many)

Read the documentation on "many_to_many" in DBIx::Class::Relationship.

.. stop DBIx::Class from attempting to cascade deletes on my has_many relationships?

By default, DBIx::Class cascades deletes and updates across has_many relationships. If your database already does this (and probably better), turn it off by supplying cascade_delete => 0 in the relationship attributes. See DBIx::Class::Relationship::Base.

.. use a relationship?

Use it's name. An accessor is created using the name. See examples in "Using relationships" in DBIx::Class::Manual::Cookbook.

Searching

.. search for data?

Create a $schema object, as mentioned above in ".. connect to my database". Find the DBIx::Class::Manual::Glossary/ResultSet that you want to search in, and call search on it. See "search" in DBIx::Class::ResultSet.

.. 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()'})
.. sort the results of my search?

Supply a list of columns you want to sort by, to the order_by attribute, see "order_by" in DBIx::Class::ResultSet.

.. sort my results based on fields I've aliased using as?

You don't. You'll need to supply the same functions/expressions to order_by, as you did to select.

.. group the results of my search?

Supply a list of columns you want to group on, to the group_by attribute, see "group_by" in DBIx::Class::ResultSet.

.. group my results based on fields I've aliased using as?

You don't. You'll need to supply the same functions/expressions to group_by, as you did to select.

.. filter the results of my search?
.. search in several tables simultaneously?
.. create joins with conditions other than column equality?
.. search using greater-than or less-than and database functions? NOT my $interval = "now() - interval '12 hours'"; last_attempt => { '<' => \$interval }, BUT 08:44 <@castaway> my $interval = "< now() - interval '12 hours'"; .. last_attempt => \$interval ,
.. find more help on constructing searches?

Behind the scenes, DBIx::Class uses SQL::Abstract to help construct it's SQL searches. So if you fail to find help in the DBIx::Class::Manual::Cookbook, try looking in the SQL::Abstract documentation.

Fetching data

.. fetch as much data as possible in as few select calls as possible? (prefetch)

See the prefetch examples in the DBIx::Class::Manual::Cookbook.

Inserting and updating data

.. insert many rows of data efficiently?
.. update a collection of rows at the same time?
.. use database functions when updating rows?
.. update a column using data from another column?

Misc

How do I store my own (non-db) data in my DBIx::Class objects?
How do I use DBIx::Class objects my TT templates?
See the SQL statements my code is producing?
Why didn't my search run any SQL?

1 POD Error

The following errors were encountered while parsing the POD:

Around line 128:

alternative text 'DBIx::Class::Manual::Glossary/ResultSet' contains non-escaped | or /