Symfony2 TidyUp代码

时间:2014-08-23 09:44:08

标签: php symfony doctrine

有没有可能让代码变得更聪明?我是Symfony的初学者,这就是为什么我的代码看起来像这样:

如果有想法,如何缩短代码或提高效率,欢迎提出这些想法!

/**
@Route(
 *     path = "/taskmanager/user/{user_id}",
 *     name = "taskmanager"
 * )
 * @Template()
 */
public function taskManagerAction($user_id, Request $request)
{

    /* #### NEW TASK FORM #### */

    $task = new Task();
    $log = new Log();

    $addTaskForm = $this->createForm(new TaskType(), $task);
    $addTaskForm->handleRequest($request);

    if($addTaskForm->isValid()):

        /* User Object of current Users task list */
        $userid = $this->getDoctrine()
            ->getRepository('SeotoolMainBundle:User')
            ->find($user_id);

        /* User ID of current logged in User */
        $editor = $this->get('security.context')->getToken()->getUser();
        $editor_id = $editor->getId();

        /* Editor Object of current logged in User */
        $editorid = $this->getDoctrine()
            ->getRepository('SeotoolMainBundle:User')
            ->find($editor_id);

        $task->setDone(FALSE);
        $task->setUser($userid);
        $task->setDateCreated(new \DateTime());
        $task->setDateDone(NULL);


        $log->setUser($userid);
        $log->setEditor($editorid);
        $log->setLogTitle('Neue Aufgabe hinzugefügt');
        $log->setDate(new \DateTime());

        $em = $this->getDoctrine()->getManager();
        $em->persist($task);
        $em->persist($log);
        $em->flush();

        return $this->redirect($this->generateUrl('taskmanager', array('user_id' => $user_id)));

    endif;

    /* #### TASK LISTS #### */

    $priorities = array(
        0 => array('name' => 'Hoch', 'class' => 'high' ),
        1 => array('name' => 'Normal', 'class' => 'normal' ),
        2 => array('name' => 'Niedrig', 'class' => 'low' )
    );

    $tasks = $this->getDoctrine()
        ->getRepository('SeotoolMainBundle:Task')
        ->findBy(array('User' => $user_id));

    $count_task_done = $this->getDoctrine()
        ->getRepository('SeotoolMainBundle:Task')
        ->findBy(array('User' => $user_id, 'done' => TRUE));

    $count_task_open = $this->getDoctrine()
        ->getRepository('SeotoolMainBundle:Task')
        ->findBy(array('User' => $user_id, 'done' => FALSE));

    /* #### USER LISTS #### */

    $user = $this->getDoctrine()
        ->getRepository('SeotoolMainBundle:User')
        ->findBy(array('isActive' => 1, 'isAdmin' => 0));

    return array(
        'addTaskForm' => $addTaskForm->createView(),
        'message' => '',
        'list_user' => $user,
        'tasks' => $tasks,
        'current_user' => $user_id,
        'count_task_done' => $count_task_done,
        'count_task_open' => $count_task_open,
        'priorities' => $priorities,
    );

}

1 个答案:

答案 0 :(得分:1)

如果您是初学者,我建议您从其食谱中学习Symfony,这非常有启发性。 http://symfony.com/doc/current/cookbook/index.html

  1. 创建表单处理程序以管理表单。
  2. 您还可以使用工厂模式来实例化对象。
  3. 不要犹豫,不能创造服务。
  4. 所有这些建议都在食谱中。

    希望它有用。 祝你有个美好的一天。