dony39517 2013-11-20 10:25
浏览 50
已采纳

如何使用symfony2和doctrine从数据库中手动查询

I'm trying to query the database in my user repository like this (symfony2):

namespace Doobin\UserBundle\Entity;

use Doctrine\ORM\EntityRepository;

class UserRepository extends EntityRepository
{
public function pageAccess($User=1,$Page='_home')
{
    $query="SELECT `user` FROM `vu001_user_permission` WHERE `user` = '$User' AND `path`='$Page'";
    $conn = Doctrine_Manager::getInstance()->connection();
    $stmt = $conn->prepare($query);
    $stmt->execute();
    return $stmt;
}

But it won't work. It stop working when it arives at the live $conn = Doctrine_Manager::getInstance()->connection();

I copied that from the symfony website tuturial.

I Also tried this too:

$em = $this->getDoctrine()->getEntityManager();
    $connection = $em->getConnection();
    $statement = $connection->prepare("SELECT `user` FROM `vu001_user_permission` WHERE `user` = '$User' AND `path`='$Page'");
    $statement->execute();
    $results = $statement->fetchAll();

It stop working when it arive to line $em = $this->getDoctrine()->getEntityManager();

And tried this too:

namespace Doobin\UserBundle\Entity;

use Doctrine\ORM\EntityRepository;

class UserRepository extends EntityRepository
{
public function pageAccess($User=1,$Page='_home')
{
return $this->getEntityManager()
        ->createQuery(
            'SELECT p FROM UserBundle:User p ORDER BY p.name ASC'
        )
        ->getResult();
}
}

$this->getEntityManager() now working, please help I'm so confiused. I checked everything.

Am I missing anything?

  • 写回答

2条回答 默认 最新

  • dongyi1939 2013-11-20 10:42
    关注

    It kind of looks like you are mixing symfony 1 and 2.

    In symfony2 you would most likely use the NativeQuery methods to achieve this. (see here http://docs.doctrine-project.org/en/latest/reference/native-sql.html)

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

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?