symfony2笔记
public function indexAction($count, $firstName) { return new Response('It\'s a traaaaaaaap!'); }
public function indexAction($count, $firstName) { return $this->render( 'EventBundle:Default:index.html.twig', array('name' => $firstName) ); }
使用变量{{ name }}
也可以打印变量 <?php echo $name;?>{% for i in 1..count %} Hello <strong>{{ name }}</strong> # {{ i }}!<br/> {% endfor %}
注释:{# Hello comments! #}
过滤:
Hello <strong>{{ name|upper }}</strong>
模板继承:定义一个基础模板,app/Resources/views/base.html.twig
{# app/Resources/views/base.html.twig #} <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>{% block title %}Welcome!{% endblock %}</title> {% block stylesheets %}{% endblock %} <link rel="icon" type="image/x-icon" href="{{ asset('favicon.ico') }}" /> </head> <body> {% block body %}{% endblock %} {% block javascripts %}{% endblock %} </body> </html>
继承上面的模板:
{# src/Yoda/EventBundle/Resources/views/Default/index.html.twig #}
{% extends '::base.html.twig' %}
{% block body %}
{# ... the rest of the template ... #}
{% endblock %}
::base.html.twig 模板命名有讲究
模板文件名由三部分组成:bundle,子文件夹,模板文件
在上面的模板例子中,bundle 和子文件夹是缺失的
看下面的例子:
/bundle/子目录/模板文件
EventBundle:Default:index.html.twig:src/Yoda/EventBundle/Resources/views/Default/index.html.twig
/bundle/模板文件
EventBundle::index.html.twig:src/Yoda/EventBundle/Resources/views/index.html.twig
/模板文件
::base.html.twig:app/Resources/views/index.html.twig
Doctrine2的基本概念
1.实体对象-Entity
字段
关系
2.对象操作集合-Repository
有统一的数据库方法
3.Proxy Class
动态根据关系查询数据库
4.DQL
5.事件监听机制 自动添加时间字段
从无到有:设计,配置,生成数据库
设计数据库关系和主要字段
配置symfony2数据库连接
配置entity
生成get/set/repository
生成真实数据库
/*只返回一条记录*/ $user = $em->getRepository('User')->findOneBy(array('id'=>1)); /**修改id为1的name字段**/ $user->setName('Miyaye'); /*实例化,同步到数据库*/ $em->persist($user); $em->flush(); /*DQL语句*/ $query = $em->createQuery( 'select u from DataBundle:User p where u.name = :name' )->setParameter('name','Alice'); /*使用querybuilder生成dql语句*/ $query2 = $em->getRepository('User')->createQueryBuilder('U') ->where('u.name = :name') ->setParameter('name','Alice') ->getQuery();
创建Entity
php app/console doctrine:generate:entity
The Entity shortcut name:MiyayewebBundle:User
生成User表以及repository get set方法
generate:doctrine:entities
1.通过generate:doctrine:entity 选空repository
生成一个空的entity
2.通过generate:doctrine:entities Bundle
生成entity的方法 和 repository
<?php namespace Miyaye\webBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Table(name="user") * @ORM\Entity(repositoryClass="UserRepository") */ class User { /** * @var integer * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @ORM\Column(type="string") */ protected $username; /** * @ORM\Column(type="string") */ protected $password; /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set username * * @param string $username * @return User */ public function setUsername($username) { $this->username = $username; return $this; } /** * Get username * * @return string */ public function getUsername() { return $this->username; } /** * Set password * * @param string $password * @return User */ public function setPassword($password) { $this->password = $password; return $this; } /** * Get password * * @return string */ public function getPassword() { return $this->password; } }