- I have a
person
table that has a primary keyid
. This is a base table. - I have an
address
table that has a foreign keyperson_id
. This is NOT a base table (has no primary key) - The address table contains a
person
's address, but not everyperson
has anaddress
.
When describing these two tables to views, how do I set it up so that when the person
table is used as a base table, I can optionally add a relationship to address
to join in the information?
I know I can add an implicit join on the address
definition like so:
$data['address']['table']['join'] = array(
'person' => array(
'field' => 'person_id',
'left_field' => 'id',
),
);
This allows me to add any address
fields automatically in the Views UI. Views would by default use a LEFT JOIN
to bring in the address
data. I can change this to an INNER JOIN
in the above definition, but I want to allow the user to chose, which is exactly what using a relationship handler does (has the "require this relationship checkbox", which controls INNER
vs LEFT
).
However, in my analysis of how these handlers work, I don't think I can define the handler in a way that this will work. It seems the relationship handler should be added to the foreign key on the base table. My base table is person
, which does not have the foreign key.
For instance, I tried this:
$data['address']['person_id']['relationship'] = array(
'base' => 'person',
'base field' => 'id',
'handler' => 'views_handler_relationship',
'label' => t('Address'),
'help' => t("Has basic information on a person's address.")
);
But this doesn't work, since address
is not used as a base table in my View.
How can I do this?
P.S. part of my confusion/trouble stem from my inability to define a table neither as a base table nor an implicit join table, which is what I want to do for address
.
UPDATE 1: For a real-world example, look at the table accesslog
and how it relates to users
. When working with a view w/ users
base table, you cannot explicitly add the accesslog
relationship, but you can add all the accesslog
fields since statistics.views.inc
creates the implicit relationship. My schema is similar, but I want to use explicit relationships instead.