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