I am trying to query the department name from one table, using the id submitted from a form submission to another table by matching the id from the form submission to the id in the Department table.
$deptQuery = $form->getValue('department_id');
$q = Doctrine_Query::create()
->select('d.name')
->from('Department d')
->where('id = ?', $deptQuery)
->execute();
;
echo $q;
This is my output:
<pre>
Doctrine_Collection
data : Array(
0 : Object(Department)
)
</pre>
I can get the department_id variable to display like this:
$deptQuery = $form->getValue('department_id');
echo $deptquery;
It will show the correct id:
5 (for example)
How can I write the query so it takes the submitted form department_id and matches it to the id in the Department table and outputs the department name?
ie rather than 5, it outputs the photography dept.
This has been addressed and resolved here. Sorry for my amateur posting.
I just needed to cut the array to one return and pull the first.
$dept = $q->fetchOne();
$dept = $q->execute()->getFirst();
There is a method call find() which does exactly what you want … use it like this :
The
$idparameter must be a primary key – which in your case it is (im assuming). This will return aDepartmentobject matching your primary key – to get the name you can just use$dept->getName()