Get Symfony Container in an EntityRepository

后端 未结 5 1230
无人共我
无人共我 2020-12-15 11:56

I\'ve set a variable in parameters.ini, but now I want to retrieve that variable from an EntityRepository and $this->container is unset so I can\'t do it

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-15 12:46

    If you are trying to access DBAL from EntityRepository class, you can use $this->getEntityManager()->getConnection() to get it.

    Ex:

    class CustomRepository extends EntityRepository
    {
        public function myCustomFunction()
        {
            $conn = $this->getEntityManager()->getConnection();
            $stmt = $conn->query($sql);
            if ($stmt)
            {
                while ($row = $stmt->fetch())
                    var_dump($row);
            }             
        }
    }
    

提交回复
热议问题