I am trying to pull a specific set of records from a table called Faculty, by retrieving a specific parameter from another table. I am getting the id from the sfGuard User, then retrieving the record from the ID. From there I would like to pull out the department title from that table and display only records from Faculty with that department title.
This is what I have in my actions.class:
$userId = $this->getUser()->getGuardUser()->getId();
$userRecord = Doctrine_Query::create()
->from ('sfGuardUser s')
->where("s.id = '$userId'")
->limit('1')
->execute();
$userDept = $userRecord['department_title'];
$this->facultyAdminFind = Doctrine_Query::create()
->from ("Faculty a")
->where("a.notables LIKE ?", "%$userDept%")
->execute();
After doing some troubleshooting, I know I am pulling the correct ID and the correct record from sfGuardUser table. I just can’t seem to pull the department title from the sfGuardUser record.
The loop is coming up with nothing.
If I change $userDept = $userRecord[‘department_title’]; to $userDept = “Engineering”;
Update to questions below:
I am getting the userID, then asking for the department_title of that ID. I can get the id using getGuardUser(), but not ‘department_title’ right? If so, what would that syntax look like?
I am sure there is a department title and there are no misspellings. I actually worked from this backwrds to verify all variables that were being pulled
First I did this:
$this->facultyAdminFind = Doctrine_Query::create()
->from ('sfGuardUser s')
->where("s.id = '$userId'")
->limit('1')
->execute();
which returned the correct record in the loop and I was able to use $item[‘department_title”] in the template to call the department title correctly. So I know I am getting the correct userID and calling the correct record into $userRecord.
Also, inside Faculty::notables there are strings with Engineering. That is why I did this:
$userDept = "Engineering";
And I was able to get a result of all records with Engineering in notables.
This is what I found that works:
Per ProdigitalSon I was able to look at the model and figure out how to pull the dept title from the user table. This is working.
$userId = $this->getUser()->getGuardUser()->getDepartmentTitle();
$this->facultyAdminFind = Doctrine_Query::create()
->from ("Faculty a")
->where("a.notables LIKE ?", "%$userId%")
->execute();
UPDATE:
I learned you should not modify the sfGuard schema because if you update the plugin, you would lose the specialized schema. I have added a new class in the main schema, and have a working association between sf_guard_user and department_title.
The issue is I am back to square one with trying to get the department_title from the array.
Here is the added schema:
MyUserProfile:
columns:
id: { type: integer, primary: true, autoincrement: true }
sf_guard_user_id: { type:integer }
department_title: { type: string(255) }
# ... other column definitions
relations:
User:
class: sfGuardUser
foreignType: one
foreignAlias: Profile
local: sf_guard_user_id
onDelete: CASCADE
And this is the query that should work (in my head).
//pulls department limited records for faculty admin
$userId = $this->getUser()->getGuardUser()->getId();
$usergetDept = Doctrine_Query::create()
->from('MyUserProfile d')
->where("d.sf_guard_user_id = '$userId'")
->execute();
$userDept = $usergetDept['department_title'];
$this->facultyAdminFind = Doctrine_Query::create()
->from ("Faculty a")
->where("a.notables LIKE ?", "%$userDept%")
->execute();
This is pulling every record, not just the ones with the department_title. Could I do this easier with a multiple join maybe?
Firs thing that stands out is why are you querying for the user when you already have the user. You dont need to query
sfGuardUseragain you already have that record from callingmyUser::getGuardUser().So the next thing is are you sure the record youre working with has a
department_titleproperty set? Are you sure you didnt mispell the column name either in the schema or in the snippet code? And are us sure that the value in thesfGuardUser::department_titleyoure working with does in fact match some records inFaculty::notables?