duanchui1279 2017-04-18 20:38
浏览 25
已采纳

在symfony的查询中组合两个变量

I'm having some trouble with what may be a simple question.

I need to be able to query two separate variables as a key using doctrine, but I'm not sure if I need to update the entity schema (I'd rather not, and it seems overkill) to be able to do this.

I'm currently needing to query a number of different items in a single DQL query in one field. I've been doing something like this:

        $qb->select('r')->from('Bundle:Reviews\Review', 'r')
        ->leftJoin('Bundle:Bibliographies\PersonalName', 'p', 'WITH', 'p.biblio = r.id')
        ->leftJoin('r.biblio', 'bib')
        ->where( 'bib.author LIKE ?1 OR p.surname LIKE ?1' );

so far so good. The problem I have is that unlike author in the Review entity, which includes both surname and first name, stored automatically like surname, givenName, on the PersonalName table (which exists as a one-to-many against Bibliography), there's no single field for both. I have surname, but what I really need is something like this:

   ->where( 'bib.author LIKE ?1 OR "p.surname", "p.givenName" LIKE ?1' );

which of course wont work.

To make this simpler, is there a way to do something like create a variable in the Repository that is a composite, define it as a parameter (e.g. $params[3] and then do something like this>

    ->where( 'bib.author LIKE ?1 OR ?3 LIKE ?1' );

Otherwise, I might be fine with doing it in the entity, but I'm not entirely sure how to do it.

The PersonalName entity has something like this:

    /**
 * @var string
 *
 * @ORM\Column(name="given_name", type="string", length=255, nullable=true)
 */
private $givenName;

/**
 * @var string
 *
 * @ORM\Column(name="surname", type="string", length=255, nullable=true)
 */
private $surname;

Is there an easy way of simply creating a new attribute that is a combination of surname and givenName without having to update the entity? I've been trying to find things in the manual but, alas no dice.

Any help appreciated.

Edit for clarification:

All I really need is in my where clause to give me something like this:

->where( 'bib.author LIKE ?1 OR (CONCAT(p.surname,CONCAT(", ",p.givenName)) LIKE ?1)' )

Unfortunately, where I was getting perfectly good results earlier searching on surname, or a substring of author, I am now getting zero results at all, which makes little sense to me, as there is a clear OR. I'm not sure where this is breaking the query.

  • 写回答

2条回答 默认 最新

  • duanshang3230 2017-04-19 18:06
    关注

    Okay so the advice I got above from @ccKep sent me in the right direction. However, apparently for some reason DQL does not like double quotes inside single quotes.

    This is the solution that worked for me:

    ->where( "bib.author LIKE ?1 OR CONCAT(p.surname,CONCAT(', ',p.givenName)) LIKE ?1" )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同