简体   繁体   English

在学说中如何使用左子句?

[英]How to use left clause in doctrine?

this is my query for find all records in table and sort from the oldest. 这是我的查询,用于查找表中的所有记录并从最早的记录开始排序。 I want to limit one column to only 50 characters. 我想将一列限制为仅50个字符。 I can't find any information how to change this query for use left clause. 我找不到任何信息来更改此查询以使用left子句。

    $posts = $ChocoPosts->findBy(array(), array('id' => 'DESC'));

Additionally i tried use clean sql syntax for that, but it's not show any records. 此外,我尝试为此使用干净的sql语法,但未显示任何记录。

 $conn = $this->getDoctrine()->getManager()->getConnection();
$sql = '
    SELECT id, title, left(content, 45), date_add, img
    FROM choco_posts
    ORDER BY id DESC
    ';
$stmt = $conn->prepare($sql);
$stmt->execute();

$stmt->fetchAll();

I'm using symfony4 ant twig template. 我正在使用symfony4蚂蚁树枝模板。 I display all records in twig for loop. 我将所有记录显示在树枝中以进行循环。

You can simply use SUBSTRING statement, as example: 您可以简单地使用SUBSTRING语句,例如:

$sql = '
    SELECT id, title, SUBSTRING(content, 1, 45), date_add, img
    FROM choco_posts
    ORDER BY id DESC
    ';

By itself there is little difference, however left can potentially utilize an index while substring cannot. 本身没有什么区别,但是left可以潜在地利用索引,而substring则不能。

Hope this help 希望这个帮助

You can: 您可以:

  1. use twig slice filter - https://twig.symfony.com/doc/2.x/filters/slice.html 使用树枝切片过滤器-https: //twig.symfony.com/doc/2.x/filters/slice.html
  2. map results - http://php.net/manual/en/function.array-map.php 地图结果-http://php.net/manual/en/function.array-map.php
  3. or if you want use object create metohd with substring 或者如果您想使用对象创建带有子字符串的metohd

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM