获取数据库中的相关信息(Doctrine)

# 获取单个对象形式结果,用法1
use DBBundle\Entity\User;

$object = $this
    ->getDoctrine()
    ->getManager('default')
    ->getRepository(User::class)
    ->findOneBy(['user_id' => 1], ["user_id" => "ASC"]);
    //->find($user_id);

# 获取单个对象形式结果,用法2
$object = $this
    ->getDoctrine()
    ->getManager('default')
    ->getRepository('DBBundle:User')
    ->findOneBy(['user_id' => 1], ["user_id" => "ASC"]);
    //->find($user_id);

# 通过方法获取数组形式结果
$orm = $this
    ->getDoctrine()
    ->getManager('default');
$query = $orm
    ->getRepository('DBBundle:User')
    ->createQueryBuilder('p')
    ->select('p.userId,p.name')
    ->where('p.userId = :user_id')
    ->setParameter('user_id', 1)
    //)->setParameters(['user_id'=>1])
    ->orderBy('p.userId', 'ASC')
    ->setFirstResult(0)
    ->setMaxResults(10)
    ->getQuery()
    ->getArrayResult();

# 通过语句获取数组形式结果
$orm = $this
    ->getDoctrine()
    ->getManager('default');
$query = $orm
    ->createQuery(
        'SELECT
            p.userId, p.name
        FROM
            DBBundle:User p
        WHERE
            p.userId = :user_id
        ORDER BY
            p.userId
        ASC'
    )->setParameter('user_id', 1)
    //)->setParameters(['user_id'=>1])
    ->orderBy('p.userId', 'ASC')
    ->setFirstResult(0)
    ->setMaxResults(10)
    ->getArrayResult();

# 通过语句连表查询获取数组结果
$orm = $this
    ->getDoctrine()->getManager('default');
$query = $orm
    ->createQuery(
        'SELECT
            p.userId, p.name
        AS
            id
        FROM
            DBBundle:User1 p
        INNER JOIN
            DBBundle:User2 q
        WHERE
            p.userId = q.userId
        AND
            p.userId = :user_id
        AND
            q.time > :time
        ORDER BY
            p.userId
        ASC'
    )->setParameter('user_id', 1)
    ->setParameter('time', $time)
    //)->setParameters(['user_id' => 1, 'time' => $time])
    ->getArrayResult();

查询数据库中相关信息(PDO)

# 注意:如果用'fetch'代替'fetchAll'则返回的是一维数组,且是第一条结果
# 最简单的方式执行
$this->conn = $this->get('database_connection');
$sql = <<<SQL
SELECT 
  *
FROM
  `user`
WHERE
  `user`.id = 1
SQL;
$result = $this->conn->fetchAll($sql);
unset($this->conn);
# 查询1条结果
$result = $this->conn->fetch($sql);
unset($this->conn);

# 按序绑定参数执行
$this->conn = $this->get('database_connection');
$sql = <<<SQL
SELECT 
  *
FROM
  `user`
WHERE
  `user`.id IN (?,?,?,?,?)
SQL;
$do_exec = $this->conn->prepare($sql);
$do_exec->bindValue(1, 1);
$do_exec->bindValue(2, 2);
$do_exec->bindValue(3, 3);
$do_exec->bindValue(4, 4);
$do_exec->bindValue(5, 5);
$do_exec->execute();
$res = $do_exec->fetchAll();
unset($do_exec);
unset($this->conn);
# 查询1条结果
$res = $do_exec->fetch();
unset($do_exec);
unset($this->conn);

# 按key绑定参数执行
$this->conn = $this->get('database_connection');
$sql = <<<SQL
SELECT 
  *
FROM
  `user`
WHERE
  `user`.id = :id
SQL;
$id = 10000;
$do_exec = $this->conn->prepare($sql);
$do_exec->bindParam('id', $id);
$do_exec->execute();
$res = $do_exec->fetchAll();
unset($do_exec);
unset($this->conn);
# 查询1条结果
$res = $do_exec->fetch();
unset($do_exec);
unset($this->conn);
@耿志环 2012-∞ 冀ICP备17033181号, powered by Gitbook修订: 2018-05-23 16:25:59

results matching ""

    No results matching ""