语义错误类' Blog'没有定义Symfony 4

时间:2018-04-07 23:07:00

标签: php symfony pagination

我有这个代码:     

proc freq data=sashelp.class;
tables _char_ EXCEPT VAR1 VAR2;
run;

但是当我运行它时它会给我

  

[语义错误]第0行,第22行附近'博客':错误:类'博客'是   没有定义的。   我也试过把

namespace App\Controller;
use Symfony\Component\HttpFoundation\Request;
use App\Entity\Blog;

use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Doctrine\ORM\Tools\Pagination\Paginator;

class BlogController extends Controller
{
    /**
     * @Route("/blog", name="blog")
     */
    public function index()
    { 
        $entityManager=$this->getDoctrine()->getManager();

        $blog = $this->getDoctrine()->getRepository(Blog::class);
        $dql = "SELECT id, name  FROM Blog";
        $query = $entityManager->createQuery($dql)
                           ->setFirstResult(0)
                           ->setMaxResults(100);

        $paginator = new Paginator($query, $fetchJoinCollection = true);

        $c = count($paginator);
        foreach ($paginator as $post) {
            echo $post->getHeadline() . "\n";
        }

        return $this->render('blog/index.html.twig', [
            'controller_name' => 'BlogController',
            'blog' =>$blog,
        ]);
    }
}

但是就这样它给了我

  

[语法错误]第0行,第37行:错误:预期   Doctrine \ ORM \ Query \ Lexer :: T_IDENTIFIER,得到了':Blog'

我不知道该怎么做。请帮帮我

2 个答案:

答案 0 :(得分:2)

正确的DQL查询应如下所示:

SELECT blog.id, blog.name FROM App\Entity\Blog blog

DQL documentation可以帮助你。

答案 1 :(得分:1)

升级到S4并重构代码后,我遇到了同样的问题,但能够找出根本原因。

我的代码看起来像这样

public function loadUserByUsername($username)
{
    $select = "SELECT u";
    $from = " FROM App\Vk\UsrBundle\Entity\vUser u";   // <- Note the fully qualified path to the class definition.
    $join = "";
    $where = " WHERE u.username = '$username'";

    $query_string = $select . $from . $join . $where;
    $query = $this->em->createQuery($query_string);
    dump($query); die();                                // <- Dump $query to check how it looks
    $v_user = $query->getResult()[0];       

    if (isset($v_user))
        return $v_user;

    throw new UsernameNotFoundException(
        sprintf('Username "%s" does not exist.', $username)
    );
}

我的查询看起来不正确,'\ v'被系统视为值。 view dump output

添加另一个反斜杠来屏蔽'\ v'中的那个解决了这个问题。在此之后查询工作,错误消失了。

$from = " FROM App\Vk\UsrBundle\Entity\\vUser u";

不确定这是否与您可能具有的根本原因相同,但这可能有助于调查。