美文网首页
symfony2学习几天的总结

symfony2学习几天的总结

作者: 沉睡的小精灵 | 来源:发表于2016-11-21 15:31 被阅读0次

request

//获取request    原有的方法$this->getRequest()已经废弃;
$request = $this->container->get('request_stack')->getCurrentRequest();

//获取get参数用(方法参数中注入了参数Request $request时)
$request->query->get();

//获取post参数用
$request->request->get();

创建Bundle

php app/console generate:bundle --namespace=Acme/StoreBundle

从数据库中生成orm.entity实体

//1.生成了orm.yml
app/console doctrine:mapping:import --force WebBundle yml  

//2.生成了entities
php app/console doctrine:mapping:convert annotation ./src

//3.生成了get set方法
app/console doctrine:generate:entities WebBundle --no-backup

# notice 如果要生成EntityRepository.php,需要更改Entity\User.php中
@ORM\Entity(repositoryClass="WebBundle\Entity\UserRepository")

数据库具体操作

查询

1.使用Doctrine’s Query Builder查询对象
$repository = $this->getDoctrine()
    ->getRepository('AppBundle:Product');
 


# more 更多查询语法
//通过主键查询(一般为"id")
$product=$repository->find($id);

//动态方法名基于列值查找
$product=$repository->findOneById($id);
$product=$repository->findOneByProductName('chocolate');

//查询所有产品
$products=$repository->findAall();
//基于任意列值查找一组产品
$products = $repository->findByPrice(19.99);

//按照名字和价格来获取一个匹配的对象(查询条件数组传入)
$product=$repository->findOneBy(array('name'=>'foo','price'=>19.99));

//查询匹配名字的所有产品并按照价格排序
$products = $repository->findBy(
        array('name'=> 'foo'),
        array('price'=>'ASC')
);

$query = $repository->createQueryBuilder('p')
    ->where('p.price > :price')
    ->setParameter('price', '19.99')
    ->orderBy('p.price', 'ASC')
    ->getQuery();
 
$products = $query->getResult();
//该getResult()方法返回一个结果数组
2.使用DQL查询对象
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery(
    'SELECT p
    FROM AppBundle:Product p
    WHERE p.price > :price
    ORDER BY p.price ASC'
)->setParameter('price', '19.99');
 
$products = $query->getResult();

新增

$category = new Category();
$category->setName('Main Products');

$product = new Product();
$product->setName('Foo');
$product->setPrice(19.99);
$product->setDescription('Lorem ipsum dolor');
// relate this product to the category
$product->setCategory($category);

$em = $this->getDoctrine()->getManager();
$em->persist($category);
$em->persist($product);
$em->flush();

更新

$em = $this->getDoctrine()->getManager();
$product = $em->getRepository('AppBundle:Product')->find($id);
if (!$product) {
    throw $this->createNotFoundException(
        'No product found for id '.$id
    );
}

$product->setName('New product name!');
$em->flush();

删除

$em = $this->getDoctrine()->getManager();
$product = $em->getRepository('AppBundle:Product')->find($id);
$em->remove($product);
$em->flush();

相关文章

  • symfony2学习几天的总结

    request 创建Bundle 从数据库中生成orm.entity实体 数据库具体操作 查询 1.使用Doctr...

  • Composer错误:exceeded the timeout

    Symfony2在使用Composer安装propel的时候遇到下面的错误 [Symfony\Component\...

  • 要做就做第一名

    前几天刚学会了一个学习的公式:体验→感受→分享→总结。 生活要学会总结,总结是最好的学习,可以说没有总结的学习不叫...

  • php extension ts和nts 版本选择

    在安装symfony2时, 系统推荐安装apc, 但是下载下来的apc extension有ts和nts两个版本....

  • 又是几天的总结

    跑了几天的宣讲会,到底收获的是什么呢?可能是选择比能力更重要,也可能是失败的经验也可以说是一种成功。诸如此类的,归...

  • 这几天的总结

    9月19日 今天的任务 总结 今天早上的任务没有完成,晚上的网页2个只完成一个需要明天到公司收尾 通过上课得到的启...

  • 这几天的总结

    这几天的总结 我和平台的老师说,感觉通过在平台的不断学习,我自己的沟通能力表达能力,以及为人处世方面等方面的综合能...

  • 这几天的总结

    文/田静 1.前几天和一个学长聊了很久,也聊了很多,不过大部分都是学长在给我普及知识盲区,他真的蛮厉害的,接触到的...

  • 这几天的总结

    1.股票 A股这两天大跌,我买的股票又接连亏损,庆幸的是,在跌之前把其中两支出手止盈,虽然没赚多少钱,总算是落袋为...

  • symfony2 请求篇

    要与用户进行交互,请求与响应是必要的,也是必须的。这篇主要是介绍symfony如何接受用户的请求。 获取reque...

网友评论

      本文标题:symfony2学习几天的总结

      本文链接:https://www.haomeiwen.com/subject/gmyupttx.html