编辑:symfony加上复杂的数据库连接

时间:2012-03-29 16:12:16

标签: mysql symfony1 doctrine pdo

所以,我必须说我是symfony的新手,我现在已经使用了大约4天了,但到目前为止我已经知道了很多...

我最近投入了一个项目来添加一个新功能,所以我通过git检出了repo并创建了新实体和crud以开始处理它...值得注意的是这个系统正在使用PDO MySQL驱动程序。

在我的doctrine配置yml我的实体我有:

Ecs\CrmBundle\Entity\TimeClock:
type: entity
table: null
fields:
    id:
        type: integer
        id: true
        generator:
            strategy: AUTO
    in1:
        type: datetime
    out1:
        type: datetime
    in2:
        type: datetime
    out2:
        type: datetime
    totaltime:
        type: string
    daydate:
        type: datetime
manyToOne:
    noteBy:
        targetEntity:  Ecs\AgentManagerBundle\Entity\User
lifecycleCallbacks: {  }

这是我们员工使用的时间...然后在我的时钟控制器中,在index()功能中,我需要做的是在数据库中搜索这个付费期间的条目但仅针对代理已登录...

因此,举个例子,我已经编写了$start$end的代码,这些代码将以日期时间格式保存日期...就像这样:

分别为

$start = "2012-03-24 00:00:00"$end = "2012-03-31 23:59:59" ..

我目前拥有的代码是拉入登录代理的所有条目:

public function indexAction()
{
    $em = $this->getDoctrine()->getEntityManager();
    $user = $this->get('security.context')->getToken()->getUser();
    // date format: YYYY-MM-DD HH:MM:SS
    $entities = $em->getRepository('EcsCrmBundle:TimeClock')->findBy(array('noteBy' => $user->getid()));

    return $this->render('EcsCrmBundle:TimeClock:index.html.twig', array(
        'entities' => $entities,
    ));
}

那么,如何扩展$entities以包含使用$start$end缩小范围?

[编辑]

我做了一点研究,我已经重新编写了indexAction函数..

public function indexAction()
{
    $em = $this->getDoctrine()->getEntityManager();
    $user = $this->get('security.context')->getToken()->getUser();
    // date format: YYYY-MM-DD HH:MM:SS
    $today = time();
    if (date('l') == "Saturday") {
        $end = date('Y-m-d ')."23:59:59";
    } else {
        $end = date('Y-m-d ', strtotime('next_saturday', $today))."23:59:59";
    }
    $start = date('Y-m-d H:i:s', strtotime('last_sunday', $today));
    $entities = $em->getRepository('EcsCrmBundle:TimeClock');
    echo $user->getid();
    $query = $entities->createQueryBuilder('tc')
        ->where('tc.noteBy = :user')
        ->where('tc.daydate <= :end')
        ->where('tc.daydate >= :start')
        ->setParameter('user', $user->getid())
        ->setParameter('end', $end)
        ->setParameter('start', $start)
        ->getQuery();
    $entities = $query->getResult();

    return $this->render('EcsCrmBundle:TimeClock:index.html.twig', array(
        'entities' => $entities,
    ));
}

但是现在当我运行它时,它给了我这个:

Invalid parameter number: number of bound variables does not match number of tokens但是没有告诉我问题出在哪里...如果我删除2 where(tc.daydate)和相应的setParameter函数,它会返回预期结果减去过滤他们输入的日期..任何想法?

1 个答案:

答案 0 :(得分:0)

我在搜索和搜索之后想出来并且没有提出太多任何事情......这个问题,正如我猜测的那样where命令太多......

 public function indexAction()
{
    $em = $this->getDoctrine()->getEntityManager();
    $user = $this->get('security.context')->getToken()->getUser();
    // date format: YYYY-MM-DD HH:MM:SS
    $today = time();
    if (date('l') == "Saturday") {
        $end = date('Y-m-d ')."23:59:59";
    } else {
        $end = date('Y-m-d ', strtotime('next_saturday', $today))."23:59:59";
    }
    $start = date('Y-m-d H:i:s', strtotime('last_sunday', $today));
    $entities = $em->getRepository('EcsCrmBundle:TimeClock');
    echo $user->getid();
    $query = $entities->createQueryBuilder('tc')
        ->where('tc.noteBy = :user')
        ->andWhere('tc.daydate <= :end')
        ->andWhere('tc.daydate >= :start')
        ->setParameter('user', $user->getid())
        ->setParameter('end', $end)
        ->setParameter('start', $start)
        ->getQuery();
    $entities = $query->getResult();

    return $this->render('EcsCrmBundle:TimeClock:index.html.twig', array(
        'entities' => $entities,
    ));
}

daydate的2更改为andWhere可以完全解决问题..