In the modules actions, what is the best way to select records based on an index other than the primary key id?
$this->city = Doctrine::getTable('City')->find(array($request->getParameter('city')));
This always returns a query with WHERE City.id= instead of WHERE City.city=
Do I have to do something like
$q = Doctrine_Query::create()
->from('City j')
->where('j.city = ?', $request->getParameter('city'));
$this->city=$q->execute();
find() method only finds a record by a primary key.
You are able to find records by other fields with findBy*/findOneBy* methods just like @phidah mentioned (so it’s findOneByCity in your case).
However, you shouldn’t use finder methods in your final code. From doctrine’s documenation:
Read more about magic finders here: http://www.doctrine-project.org/documentation/manual/1_2/nl/dql-doctrine-query-language:magic-finders
I’d rather put a short call to a model method in your action.
Action:
Model:
As long as you give appropriate name to your method, which shows its intent, your code is far more readable.