symfony日记17-上传文件

On 2010/10/01, in PHP, by lyndon

upload file via symfon.

step1: set widget

$this->setWidget(‘pic’,new sfWidgetFormInputFile());

or

$this->setWidget(‘pic’, new sfWidgetFormInputFileEditable(array(

‘file_src’    => sfConfig::get(‘sf_upload_dir’).’/’.$this->getObject()->filenamefield,

‘edit_mode’   => !$this->isNew(),

‘is_image’    => true,

‘with_delete’ => false,

)));

step2: set validatorschema

$this->validatorSchema['pic'] = new sfValidatorFile(array(
‘required’   => false,    //是否必填
‘path’       => sfConfig::get(‘sf_upload_dir’).’/pic’,    //上传路径
‘mime_types’ => ‘web_images’,    //文件类型
));
step3: save
//当文件保持时,会自动生成唯一文件名,类似6cc3e597929aaa8251abe913fc73241df548b8a3.jpg,并自动赋值给form的pic,然后只需保存form即可。
$form->save();
Tagged with:  

symfony生成后台和前台后,默认的session_name是同名的,所以当前台或后台登陆任一个的时候再打开另一个应用程序状态自然是登陆状态(getAuthenticated为true),只要改变各自的session_name就可以了。

在各自的factories.yml文件中添加:
storage:
class: sfSessionStorage
param:
session_name: yunfei_backend

Tagged with:  

在一些主机上,比如godaddy,php是以cgi模式运行,这时服务器的path_info变量会出现问题,导致symfony的路由获取的信息不对,在访问

index.php/app/action这样的链接时出现错误。

这时可以修改setting.yml文件加入

path_info_key:          REDIRECT_URL

并修改php.ini,加入

cgi.fix_pathinfo = 1

doc_root=

即可

Tagged with:  

symfony日记14-给doctrine添加缓存

On 2010/09/16, in PHP, by lyndon

doctrine可以使用的缓存类型很多有apc,memcache,xcache,array,db等,但是没有eaccelerator。于是自己写了一个。
Continue reading »

Tagged with:  

symfony日记13–在form中获得user信息

On 2010/09/16, in PHP, by lyndon

sfContext::getInstance()->getUser()->getAttribute(…)

Tagged with:  

symfony日记12–读取yml中的内容

On 2010/09/16, in PHP, by lyndon

$var = sfYaml::load(sfConfig::get(‘sf_config_dir’) . ‘/app.yml’);

Tagged with:  

Form控件的错误信息只是进行表单校验时代错误,比如为空,或者超过长度,如果需要将一些业务的错误信息也显示在其中,可以用下面的方法:

Action代码:

//创建form

$this->form = new BackendLoginForm();

//获取对应控件的validator对象

$userID_custom_error=$this->form->getValidator(‘userId’);

//添加错误信息文字

$userID_custom_error ->addMessage(‘other_msg’, ‘This is a custom error msg: %msg%’);

//加入此信息选项

$userID_custom_error ->addOption(‘other_msg’);

//添加新错误

$this->form->addCustomError(new sfValidatorError($userID_custom_error,’other_msg’,array(‘msg’=>’ERROR!’)), ‘userId’);

在backendloginform.class.php中加入addCustomError方法

public function addCustomError($error,$name=null)

{

// errorSchema为保护对象,只能从类中访问

$this->errorSchema->addError($error,$name);

}

Tagged with:  

在doctrine中直接使用原生的sql语句

//Note: If you want to select a specific connection first, then call:
//Doctrine_Manager::getInstance()->setCurrentConnection(’your_connection_name_1′);
// Get Doctrine_Connection object

$con = Doctrine_Manager::getInstance()->connection();

// Execute SQL query, receive Doctrine_Connection_Statement

$st = $con->execute(“your sql”);

// Fetch query result

$result = $st->fetchAll();

Tagged with:  

当database.yml中存在多个链接时,doctrine有时无法使用正确的链接,即使在schema.yml中指定了链接也可能发生这种情况。
这时需要在action中指定链接。
class youractions extends sfActions

{
public function __construct($context=null, $moduleName=null, $actionName=null)
{
Doctrine_Manager::getInstance()->setCurrentConnection(‘链接名字’);
parent::__construct($context, $moduleName, $actionName);
}
//other code
}

Tagged with:  

变量可在action中定义为$this->var,然后在template中直接使用$var即可使用。
但是数组中action中定义后,在template中就变成了sfOutputEscaperArrayDecorator对象,使用的方法也随之改变。
详见http://www.symfony-project.org/api/1_4/sfOutputEscaperArrayDecorator

或者使用$sf_data->getRaw(‘var’)

Tagged with: