Home  >  Article  >  Backend Development  >  Detailed explanation of Symfony2 function usage

Detailed explanation of Symfony2 function usage

*文
*文Original
2018-01-05 17:24:212059browse

This article mainly introduces the usage of Symfony2 functions, and analyzes the related skills of calling Symfony functions and database operations in the form of examples. Friends in need can refer to it. I hope it will be helpful to everyone.

1. Call methods of other objects.

Example:

$grobal_func=$this->container->get('global_func');
//'global_func'函数所在的文件名
$lot_data=$global_func->getDataFromFile($parm1);
//getDataFromFile函数名

2. Database preliminary query

$conn = $this->em->getConnection();
$sql_user="select * from lot_user where user_name=:param1 and user_id=:param2 limit 1";
$params = array(
    'param1' => $user,
    'param2' => $uid
);
$ready = $conn->prepare($sql_user);
$ready->execute($params);
$result_user = $ready->fetchAll();

3. Database rollback event

$em->getConnection()->beginTransaction();
try{
   $lotuser = new LotUser();
   $lotuser->setId(0);
   $lotuser->setUserId($user_id);
   $lotuser->setUserName($user_name);
   $lotuser->setPassword($password);
   $lotuser->setUserTele($user_tele);
   $lotuser->setEmail($email);
   $lotuser->setRegDate($reg_date);
   $lotuser->setIdNumber($id_number);
   $lotuser->setRealUsername($real_username);
$em->persist($lotuser);
$em->flush();
$em->getConnection()->commit();
}catch(Exception $e){
$e->getConnection()->rollback();
}

Related recommendations:

Detailed explanation of how Symfony obtains request parameters in templates and behaviors

Detailed explanation of creating projects and setting templates in the Symfony2 framework

Detailed explanation of the usage of Symfony2 controller

The above is the detailed content of Detailed explanation of Symfony2 function usage. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn