i'm trying to write a views field handler but i'm lost.

i have a table "flag_note" and a field "flag_note.uid". i want to retrieve the value of flag_note.uid in the handler.

there are two cases:

  1. the view query already contains another table with a field called "uid" field because of a join with ie. node/users/flag_content
    through a relationship
  2. the view query doesn't contain any table with an "uid" field yet because there are no relationships, so i have to retrieve the uid
    through the flag_note table directly

because i don't know in which of the above ones i will fall, what i do in the handler is:

class views_handler_field_flag_note_link_delete extends views_handler_field {
  function construct() {
    parent::construct();
    $this->additional_fields['uid'] = array('table' => 'flag_note', 'field' => 'uid');
  }
  function query() {
    $this->ensure_my_table();
    $this->add_additional_fields();
....

the problem is that the additional SELECT field flag_note.uid is not aliased.

when the query is executed it tries to select flag_note.uid as "uid" but that causes "uid is ambiguos" and all break lose:


user warning: Column 'uid' in field list is ambiguous query: SELECT node.nid AS nid, node.title AS node_title, flag_note_node.note AS flag_note_node_note, flag_note_node.timestamp AS flag_note_node_timestamp, flag_content.content_id AS flag_content_content_id, uid FROM node node LEFT JOIN flag_content flag_content_node ON node.nid = flag_content_node.content_id AND flag_content_node.fid = 1 LEFT JOIN flag_note flag_note_node ON node.nid = flag_note_node.content_id LEFT JOIN flag_content flag_content ON node.nid = flag_content.content_id AND flag_content.fid = 1

moreover as the flag_note table is already in the query (because of the relationship), ensure_table() returns NULL, so I cannot get the
aliased name of my flag_note table and i can neither use query->add_field() instead of additional_fields().

how to retrieve my uid field ?
thank you for the answer if one :]

Comments

merlinofchaos’s picture

Your problem may not what you think. It's a little difficult for me to be sure, because I don't quite know exactly how things are set up.

I can tell from the query that flag_note is added via a relationship. This might mean that simply adding flag_note.uid as an additional field may not work unless you've got that on the right table. This is because ensure_table won't automatically find tables added through relationships. It only finds implied relationships and I'm guessing there isn't one in this case.

It's possible that stray 'uid' is coming in because of the failing ensure_table, above.

So the question is: What table is this field attached to?

gunzip’s picture

Status: Active » Closed (fixed)

thank you for the fast answer merlinofchaos ! i don't know what's changed in the meantime but while debugging it started work magically and now the table has got the right aliases (maybe because i practically recreated the view from scratch ?) (to answer anyway: the field "uid" belongs to the flag_note table which usually is joined to users/nodes through view relationships).

gunzip’s picture

just a quick note. i discovered WHEN this happens. it happens if I add (through views gui) some fields with "one shot" (i mean, selecting multiple checkboxes at once instead of adding a field and then re-clicking the plus sign to add another and so on). if i re-edit the single fields and the re-save them, the problem goas away. i also receive an error when i click update in the field form:

Fatal error</b>views_db_object::get_item(): The script tried to execute a method or access a property of an incomplete object.
   Please ensure that the class definition &amp;quot;views_plugin_display_default&amp;quot; of the object you are trying to operate on wa
  s loaded _before_ unserialize() gets called or provide a __autoload() function to load the class definition  in <b>/home/danilo/public_
  html/drupal/sites/all/modules/views/includes/view.inc</b> on line <b>1837

(only when adding multiple fields at once)

frosty29’s picture

I am getting just the same as this (#3) when adding multiple Content Profile fields to a view. Crashes out when saving configuration options on first field, and gives error as above until each newly-added field is individually clicked and saved, then behaves itself.
D6.22 / Views 6.x-2.12 / Content Profile 6.x-1.0