1、建立entities 存储的 string 类型 使用 datetime控件编辑
//transformer string to date
$builder->add('startTime','datetime');
$builder->get('startTime')->addModelTransformer
(new CallbackTransformer
(
function($datatimestr)
{
if(strcmp($datatimestr,'0') == 0)
{
$datatimestr = date("Y/m/d H:i:s",time());
}
//transform the string to datatime must \DateTime
$datatime = new \DateTime($datatimestr);
return $datatime;
},
function(\DateTime $datetime)
{
return $datetime->format("Y/m/d H:i:s");
}
));
必须使用/DateTime 命名空间
2、在symfony2中如何加入第三方库
symfony2的第三方文档中介绍了如何添加第三方插件
地址:官网文档
首先从packageist.org中找到需要的库 这里以jpgraph/jpgraph 为例
看下 composer的下载名称是
composer require jpgraph/jpgraph:dev-master
在工程根目录下 输入上面命名 工程会自动更新插件的配置
然后就可以使用
use JpGraph\JpGraph;
然后JpGraph有load() 和loadModule($modleName);静态方法 需要在使用 如 \Graph (必须使用\命名空间) 调用
贴上使用的例子代码
public function chartAction(StatisticResult $statisticResult)
{
JpGraph::load();
JpGraph::module('line');
//$this->getResponse()->setContent('image/jpeg');
// Some data
$ydata = array(11,3,8,12,5,1,9,13,5,7);
// Create the graph. These two calls are always required
$graph = new \Graph(350,250);
$graph->SetScale('textlin');
// Create the linear plot
$lineplot=new \LinePlot($ydata);
$lineplot->SetColor('blue');
// Add the plot to the graph
$graph->Add($lineplot);
// Display the graph
$graph->Stroke();
return sfView::NONE;
}
3.symfony2 从dev模式发布为prod模式 是 出现 500 internal server error
然而开发模式下都是好的
首先查看 app\logs 下的prod.log日志 看看报的错误是否有相关头绪
如果检查后发现以前正常
删除工程目录下cache/prod 目录然后在打开试试
4.symfony2 如何传递变量
首先要修改路由表
例如:(举两个栗子)
原始:
statisticuserdetail_index:
path: /
defaults: { _controller: "SymfonyUserInfoBundle:StatisticUserDetail:index" }
methods: GET
statisticuserdetail_show:
path: /{id}/show
defaults: { _controller: "SymfonyUserInfoBundle:StatisticUserDetail:show" }
methods: GET
修改后 需要传递 变量$uid参数的
statisticuserdetail_index:
path: /{uid}
defaults: { _controller: "SymfonyUserInfoBundle:StatisticUserDetail:index" }
methods: GET
statisticuserdetail_show:
path: /{id}/show/{uid}
defaults: { _controller: "SymfonyUserInfoBundle:StatisticUserDetail:show" }
methods: GET
修改好了
还需要修改显示模板文件
列举出片段代码
红色为修改的部分
<ul>
<li>
<a href="{{ path('statisticuserdetail_index',{ 'uid': userInfo.uid }) }}">详情</a>
</li>
</ul>
<ul>
<li>
<a href="{{ path('statisticuserdetail_show', {'id': statisticUserDetail.id ,'uid':uid }) }}">详情</a>
</li>
</ul>
接着需要修改对应控制器的接口
第一种
public function indexAction(Request $request)
{
....
$uid = $request->get('uid');
}
public function showAction(Request $request)
{
....
$uid = $request->get('id');
$uid = $request->get('uid');
}
第二种
public function indexAction($uid)
{
....
}
public function showAction($id,$uid)
{
....
}
5.关于form表单的一篇文章
https://www.sitepoint.com/building-processing-forms-in-symfony-2/
获取表单提交的变量的方法要修改下
$post = Request::createFromGlobals();
if ($post->query->has('submit'))
{
$name = $post->query->get('name');
}
else
{
..
}
6.遇到out of memory 问题 修改了 php.ini 的limit_memory 无用
在操作数据库需要大量内存时 调用 gc_collect_cycles()
gc内存
7.常用操作记录
windows config
0.php.exe add to system path
1.opn open_ssl
2.php.ini plus
[curl]
curl.cainfo="E:/phpStudy/php55n/ext/ssl/cacert.pem"
composer create-project symfony/framework-standard-edition my_project_name "2.8.*"
创建 bundle
php app/console generate:bundle
Bundle namespace: Symfony/Bundle/SampleBundle
Bundle name [SymfonySampleBundle]:
Target directory [/home/saharabear/workspace/symfony-sample/src]:
Configuration format (yml, xml, php, or annotation): yml
Do you want to generate the whole directory structure [no]? yes
Do you confirm generation [yes]? yes
Generating the bundle code: OK
Checking that the bundle is autoloaded: OK
Confirm automatic update of your Kernel [yes]? yes
Enabling the bundle inside the Kernel: OK
Confirm automatic update of the Routing [yes]? yes
创建实体
php app/console generate:doctrine:entity
Welcome to the Doctrine2 entity generator
This command helps you generate Doctrine2 entities.
First, you need to give the entity name you want to generate.
You must use the shortcut notation like AcmeBlogBundle:Post.
The Entity shortcut name: SymfonySampleBundle:Article
Determine the format to use for the mapping information.
Configuration format (yml, xml, php, or annotation) [annotation]:yml
Instead of starting with a blank entity, you can add some fields now.
Note that the primary key will be added automatically (named id).
Available types: array, simple_array, json_array, object,
boolean, integer, smallint, bigint, string, text, datetime, datetimetz,
date, time, decimal, float, blob, guid.
New field name (press to stop adding fields): title
Field type [string]:
Field length [255]: 200
New field name (press to stop adding fields): content
Field type [string]: text
New field name (press to stop adding fields): author
Field type [string]:
Field length [255]: 20
New field name (press to stop adding fields):
Do you want to generate an empty repository class [no]? yes
Summary before generation
You are going to generate a "SymfonySampleBundle:Article" Doctrine2 entity
using the "yml" format.
Do you confirm generation [yes]? yes
Entity generation
Generating the entity code: OK
You can now start using the generated code!
更新数据库
php app/console doctrine:schema:update --force
生成关联数据库操作Action
php app/console generate:doctrine:crud
Welcome to the Doctrine2 CRUD generator
This command helps you generate CRUD controllers and templates.
First, you need to give the entity for which you want to generate a CRUD.
You can give an entity that does not exist yet and the wizard will help
you defining it.
You must use the shortcut notation like AcmeBlogBundle:Post.
The Entity shortcut name: SymfonySampleBundle:Article
By default, the generator creates two actions: list and show.
You can also ask it to generate "write" actions: new, update, and delete.
Do you want to generate the "write" actions [no]? yes
Determine the format to use for the generated CRUD.
Configuration format (yml, xml, php, or annotation) [annotation]: yml
Determine the routes prefix (all the routes will be "mounted" under this
prefix: /prefix/, /prefix/new, ...).
Routes prefix [/article]: /article
Summary before generation
You are going to generate a CRUD controller for "SymfonySampleBundle:Article"
using the "yml" format.
Do you confirm generation [yes]? yes
CRUD generation
Generating the CRUD code: OK
Generating the Form code: OK
You can now start using the generated code!
发布和调试环境切换
################################
php ./app/console --env=dev cache:clear
php ./app/console --env=dev cache:warm
################################
php ./app/console --env=prod cache:clear
php ./app/console --env=prod cache:warm