doukangbin9698 2010-08-14 14:06
浏览 6
已采纳

symfony查询问题

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();
  • 写回答

2条回答 默认 最新

  • duannuochi3549 2010-08-14 16:09
    关注

    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:

    These are very limited magic finders and it is always recommended to expand your queries to be manually written DQL queries. These methods are meant for only quickly accessing single records, no relationships, and are good for prototyping code quickly.

    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:

    $this->city = CityTable::getByName($request->getParameter('city'));
    

    Model:

    public static function getByName($cityName)
    {
        return Doctrine_Core::getTable('City')
          ->createQuery('c')
          ->where('c.city = ?', $cityName)
          ->fetchOne();
    }
    

    As long as you give appropriate name to your method, which shows its intent, your code is far more readable.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端