0

I have two tables: Person {pID, pName, deptID} and Departments {deptID,deptName} An SQL statement like this gets me the name of a person and the department name:

SELECT pName, departments.deptName FROM people INNER JOIN people.deptID = departments.deptID;

The above works fine. I have a Flex DropDownList that is populated with deptNames. When I click the submit button, I am able to obtain the ID of the selected department without problem:

protected function button_clickHandler(event:MouseEvent):void
   {
    person.pName=pNameTextInput.text;
    person.deptID=pDepartmentDropDownList.selectedItem.deptID; //ID of selected department obtained correctly.
    if (person.pID == 0)
    {
     createPersonResult.token=personService.createPerson(person);
    }
    else
    {
     updatePersonResult.token=personService.updatePerson(person);
    }
  }

I have a createPersonresult Handler that attempts to update the datagrid when a person is added. The datagrid is updated with the new added person, but in the department column, I get the ID of the department chosen from the dropdownlist. I need to reload the flash app to get the datagrid refreshed to display the deptName.

Something is missing in this createPerson result Handler. Google didn't provide much help.

   protected function createPersonResult_resultHandler(event:ResultEvent):void
   {
    currentState="PeopleDetails";
    person.pID=event.result as int;
    peopleDg.dataProvider.addItem(person); //the problem might be here, more below.
    peopleDg.setSelectedIndex(peopleDg.dataProvider.getItemIndex(person));
    peopleDg.ensureCellIsVisible(peopleDg.selectedIndex);
   }

From the comment above, I see that adding person will indeed add the attributes of the person object to the datagrid, except that one attribute is a foreign ID (deptID) from departments.

Shailen
  • 7,909
  • 3
  • 29
  • 37

2 Answers2

0

What I suspect is: Your select statement above is run only when you initialize/run your application for the first time. Anything you add afterwords, fails to call that select statement that brings the department name back using join statement and hence you only see the id until next load/initialization of whole app.

M.D.
  • 1,886
  • 1
  • 13
  • 14
  • I agree. Do you know a way to refresh only one row of the datagrid, by running a SELECT statement with the ID of the newly added item? – Shailen Jul 19 '11 at 16:30
  • I created a function to get the person details by ID. – Shailen Jul 19 '11 at 16:50
  • @shailenTJ I would say you load all the Department collection during app initialization and afterwards you can always find the department name by id without going back to server, hence saving yourself a roundtrip to backend. – M.D. Jul 20 '11 at 09:12
  • I can load the Department collection on appinit(), by calling a the function getAllDepartments() which performs a SELECT * FROM Departments. But then, how will that help in doing a proper update on the datagrid? Basically, the createPerson function does send the ID of the department to the server, but the datagrid should show the deptName in the deptID column. This is a bit confusing actually. In my library/Person.php, I do not define the datatype of deptID. When the datagrid is created, the column deptID shows deptNames, due to the INNER JOIN. – Shailen Jul 20 '11 at 10:35
  • Can I send you my sample FB project? If yes, how can I do so? – Shailen Jul 20 '11 at 10:36
  • In short, I would say: 1) Create an ItemRenderer for Department column. 2) Define a AppUtil.as class with a static function called getDepartmentById(ac:DeparmentColl, deptId:int): String { ..} 3) From your ItemRenderer, call this function to get the department name/label from Id. Hope it helps. – M.D. Jul 20 '11 at 12:24
0

I think you need to set your deptName property since you aren't getting it from the join when it's being added in the interface. So something like this should do the trick I think?

EDIT: for 4.5 dropdown instead of combobox

protected function button_clickHandler(event:MouseEvent):void
{
    person.pName=pNameTextInput.text;
    person.deptID=pDepartmentDropDownList.selectedItem.deptID; //ID of selected department obtained correctly.
    person.deptName = pDepartmentDropDownList.selectedItem.deptName;
    if (person.pID == 0)
    {
        createPersonResult.token=personService.createPerson(person);
    }
    else
    {
        updatePersonResult.token=personService.updatePerson(person);
    }
}
Nate
  • 2,881
  • 1
  • 14
  • 14
  • What version of the Flex SDK are you using? I am using Flash Builder for PHP 4.5 and I do not get .selectedLabel in the Content Assist menu (Ctrl+Space) for the dropDownList component. – Shailen Jul 20 '11 at 10:29
  • Updated for 4.5 dropdown instead of combobox. Give that a shot and let me know. :) – Nate Jul 20 '11 at 14:39
  • I noticed that this 'may' not work, but before commenting, I tried it out. Notice that deptName is not a member of the Person Class. That said, there is no deptName field in the Person table. We only have the deptID field. Also, due to my INNER JOIN statement, I get the names of the departments under the datagrid field deptID. From the SQL statement in my question, I clearly get departments.deptName but the FB datagrid does not see it. It just puts the result under deptID. Weird. Any clues? – Shailen Jul 20 '11 at 17:11
  • Ok, so in the datagrid, you dont have a department name column? I think we're just confused on what the goal is here. Can you pastebin your source that shows what is displayed in the datagrid or an image? – Nate Jul 20 '11 at 17:31
  • Nate! I understand why I did not have deptName in my datagrid when I dragged getPeople() on it. That was because I created the library file Person.php that had the Person Class/Object signature defined. While browsing sample source code, I noticed that someone used PHP's generic stdClass object. In my getPeople() function, I change the line $row = new Person() to $row = new stdClass(); I then had to delete and recreate the PHP service in FB (refresh didn't do the trick) and bingo, I could get person.deptID, person.deptName! Thank you!! StackOverflow is awesome. – Shailen Jul 20 '11 at 22:32
  • To everybody: Having the library file, lib\Person.php forces FB to set the attributes of the Person object according to what is defined in the class definition. – Shailen Jul 20 '11 at 22:34