At the moment it's hardcoded into PartyQuery that the party table is unaliased and called 'party', for example:

  //This Will Work
  $query = db_select('party')
    ->extend('PartyQuery')
    ->fieldCondition(...)
    .
    .
    ->execute();

  // This won't work
  $query = db_select('party', 'p')
    ->extend('PartyQuery')
    ->fieldCondition(...)
    .
    .
    ->execute();

Is it possible to sniff out the Alias in the query Extender constructor?

Comments

andrewbelcher’s picture

FYI, there is a helper method for building party queries:

$query = party_query()
  ->fieldCondition(...)
  .
  .
  ->execute();

This is what party_query() does:

/**
 * Returns a new SelectQuery extended by PartyQuery for the active database.
 *
 * @param $options
 *   An array of options to control how the query operates.
 *
 * @return SelectQuery
 *   A new SelectQuery object for this connection.
 *
 * @see db_select()
 */
function party_query($options = array()) {
  return db_select('party', 'party', $options)
    ->extend('PartyQuery');
}

But yes, it would be good to be able to make the extender handle different aliases for the party table. Not sure if this can be magically sniffed or if we need to be able to pass an option to the constructor (or both). I'll look into it later.

joachim’s picture

You can get a list of tables (and their aliases I think) from the query object.

andrewbelcher’s picture

Yes, so it will be easy to sniff out in a simpe query. The more complicated issue is that it will need to still work if you have a query where you join to the party table twice, we wont know which one to use.

I think probably what we want to do it make it attempt to sniff it out automatically and just select the first one. Then we need to provide a method to explicitly set which table we're working from. This then means we can deal with the situation where you're doing something more complicated. We then need to namespace our joins so that we don't get two different parties confused.

I think all of that will be relatively simple, so when I get some time I'll give it a go!

andrewbelcher’s picture

Status: Active » Needs review
StatusFileSize
new2.63 KB

Here is a possible patch.

It attempts to sniff out the table in the constructor and provides a method to get/set the base alias.

Status: Needs review » Needs work

The last submitted patch, 1790252-4-allow_any_base_alias.patch, failed testing.

andrewbelcher’s picture

Status: Needs work » Needs review
StatusFileSize
new2.63 KB

Needed re-basing after some other changes...

rlmumford’s picture

Status: Needs review » Fixed

Thanks for all your work on this andrewbelcher, it's an excellent feature.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

Updated issue summary.