1

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();
2
  • I don't know symfony but this sounds like a join to me. Commented Feb 8, 2012 at 17:16
  • So lets say I throw in a left join like this: ->leftJoin('d.Users u'), I am still getting the same result. Does the variable ($deptQuery) belong somewhere in the leftJoin tag? Commented Feb 8, 2012 at 19:09

1 Answer 1

1

There is a method call find() which does exactly what you want ... use it like this :

$dept = Doctrine_Core::getTable('Department')->find($id);

The $id parameter must be a primary key - which in your case it is (im assuming). This will return a Department object matching your primary key - to get the name you can just use $dept->getName()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.