2014-01-06 125 views
0

我已經這麼多天了。我的問題是我想獲取出生日期中的數據「選擇」,當我點擊編輯配置文件,我的出生日期輸入是下拉選擇哪個年份,月份和日期。我的數據庫字段是birth_date,它有一個類型日期。在保存數據時,日期以這種格式保存。例如1990-01-20,它會保存,我的問題是,當我點擊編輯配置文件選擇下拉菜單中沒有數據,這意味着它不會被「選中」,我將如何能夠獲得數據被選中?在symfony框架中?使用symfony框架選擇下拉「SELECTED」

這裏是我的前/模塊/家庭行動

<?php 

class editAction extends sfAction{ 
    private function saveData($post, $file=''){ 
    $firstName = ucfirst($post['first_name']); 
    $lastName = ucfirst($post['last_name']); 
    // $Slug = $post['slug']; 
    // $newSlug = explode(' ', $Slug); 
    // $Slug = implode("", $newSlug); 
    $cleanFirstName = preg_replace('/[^A-Za-z0-9\-]/', '', $firstName); 
    $cleanLastName = preg_replace('/[^A-Za-z0-9\-]/', '', $lastName); 

    $year = $post['year']; 
    $month = $post['month']; 
    $day = $post['day']; 

    $res = $year."-".$month."-".$day; 

    $user = Doctrine::getTable('User')->find($post['id']); 
    $user->setFirstName($cleanFirstName); 
    $user->setLastName($cleanLastName); 
    $user->setAbout($post['about']); 
    $user->setSex($post['sex']); 
    $user->setMobile($post['mobile']); 
    $user->setBirthDate($res); 
    $user->setSlug($post['slug']); 
    $user->save(); 

    $imageName = $this->getUser()->getAttribute('homeImage'); 
    if($imageName){ 
     $temp = sfConfig::get('app_GalleryPhoto_dir_150'); 
     $dir = sfConfig::get('app_GalleryPhoto_dir'); 

     if(is_file($temp.$imageName)){ 
     $file = $imageName; 
     $file['ext'] = getFileExtension($file['name']); 

     rename($file.$imageName,$dir.$file); 
     $user->setProfileImage($file); 
     $user->save(); 

     }  
    }else{ 
     if($file && !$file['error']){ 
     $file = $this->saveImage($file, $user->getId()); 
     $user->setProfileImage($file); 
     $user->save(); 
     } 
    } 

    } 

    public function submit($request){ 
    $user = $this->getUser()->getAttribute('home'.$this->id); 
    $imageName = $this->getUser()->getAttribute('homeImage'); 

    if(!$user){ 
     return $this->redirect('home/edit?id='.$this->id); 
    } 
    $this->saveData($user); 
    $this->getUser()->setAttribute('home'.$this->id, NULL); 
    return $this->redirect('home'); 

    } 

    private function saveImage($file, $id=NULL){ 
    if(!$id){ 
     $dir = sfConfig::get('app_company_dir_150'); 
    }else{ 
     $dir = sfConfig::get('app_GalleryPhoto_dir'); 
    } 

    $medium = sfConfig::get('app_GalleryPhoto_medium'); 
    $small = sfConfig::get('app_GalleryPhoto_small'); 
    $dir = sfConfig::get('app_GalleryPhoto_dir_150'); 
    $dir_60 = sfConfig::get('app_GalleryPhoto_dir'); 

    $file['ext'] = getFileExtension($file['name']); 

    if(!$id){ 
     $fileName = uniqid(). '.'.$file['ext']; 
    }else{ 
     $fileName = $id.'.'.$file['ext']; 
    } 
     $imagePath = $dir.$fileName; 

    //saving and cropping image 

    $image_medium = encode('home-'.$id).'.'.$fileName; 
    $image_small = encode('home-'.$id).'_thumb.'.$fileName; 

    $image_medium_path = $dir.$image_medium; 
    $image_small_path = $dir_60.$image_small; 

    rename($file['tmp_name'],$image_medium_path); 

    //cropping image medium 
    $image = new myImageManipulator($image_medium_path); 
    $image->resample2($medium, $medium); 
    $image->crop(0, 0, $medium, $medium); 
    $image->save($image_medium_path); 

    //cropping image small 
    $image = new myImageManipulator($image_medium_path); 
    $image->resample2($small, $small); 
    $image->crop(0, 0, $small, $small); 
    $image->save($image_small_path); 

    if(!$id){ 
     $this->getUser()->setAttribute('homeImage',$image_medium);  
    }else{ 
     return $image_medium;  
    } 

    } 

    private function deleteImageSixty(){ 
    $dir60 = sfConfig::get('app_GalleryPhoto_dir'); 
    $fileName = $this->getUser()->getAttribute('homeImage'); 
    if($fileName){ 
     if(is_file($dir60.$fileName)){ 
     unlink($dir60.$fileName); 
     } 
     $this->getUser()->setAttribute('homeImage', ''); 
    } 

    } 

    private function deleteImage($fileName){ 
    $dir = sfConfig::get('app_GalleryPhoto_dir_150'); 
    $fileName = $this->getUser()->getAttribute('homeImage'); 
    if($fileName){ 
     if(is_file($dir.$fileName)){ 
     unlink($dir.$fileName); 
     } 
     $this->getUser()->setAttribute('homeImage', ''); 
    } 
    } 

    private function saveImageTmp($request){ 
    $id = $request->getParameter('id'); 

    $formName = $this->form->getModelName(); 
    $file = $this->request->getFiles($formName); 
    $file = $file['photo']; 

    if($file && $file['error'] == 0){ 
     #$this->deleteImage(); 
     #$this->deleteImageSixty(); 
     $this->saveImage($file); 
    } 
    } 

    public function execute($request){ 
    $this->id = $request->getParameter('id'); 
    if(!$this->id){ 
     $this->forward404(); 
    } 

    $type = $request->getParameter('type'); 
    if($type == "submit"){ 
     return $this->submit($request); 
    } 

    $this->userLogged = Doctrine_Core::getTable('User')->find($this->id); 
    $this->form = new EditProfileForm($this->userLogged); 

    if($request->isMethod('post')){ 
     $formName = $this->form->getModelName(); 
     $files = $this->request->getFiles($formName); 
     $this->form->bind($request->getParameter($formName),$this->request->getFiles($formName)); 
     $post = $request->getParameter($formName); 

     if($this->form->isValid()){ 
     $this->saveImageTmp($request); 
     $this->getUser()->setAttribute('home'.$this->id, $post); 
     return $this->redirect('home/edit?id=' .$this->id. '&type=submit'); 
     }else{ 
     $this->saveImageTmp($request); 
     } 
    } 

    } 
} 
在私有函數SAVEDATA()

我這是怎麼保存的生日選擇下拉代碼

$year = $post['year']; 
    $month = $post['month']; 
    $day = $post['day']; 


    $res = $year."-".$month."-".$day; 
$user->setBirthDate($res); 

這是我editSuccess代碼

<div class="content" class="wrap"> 
    <div class="cols marginBottom30 clearFix"> 
    <div class="content withIcon icon1" style="margin-left:488px; width:500px;"> 
     <h2>Edit Profile</h2> 
     <form enctype="multipart/form-data" action="<?php echo url_for('home/edit?id=' .$userLogged->getId());?>" method="post"> 
     <?php echo $form->renderHiddenFields(); ?> 
     <table> 
      <tr> 
      <td><?php echo $form['first_name']; ?></td> 
      </tr> 
      <tr> 
      <td> 
       <div class="error_list"> 
       <?php echo $form['first_name']->renderError(); ?> 
       </div> 
      </td> 
      </tr> 
      <tr> 
      <td><?php echo $form['last_name']; ?></td> 
      </tr> 
      <tr> 
      <td> 
       <div class="error_list"> 
       <?php echo $form['last_name']->renderError(); ?> 
       </div> 
      </td> 
      </tr> 
      <tr> 
      <td> 
       <?php if($userLogged->getProfileImage()): ?> 
       <a href="/uploads/images/<?php echo $userLogged->getProfileImage(); ?>" target="_blank"><img alt="" src="/uploads/images/<?php echo $userLogged->getprofileImage(); ?>" /></a> 
       <?php endif; ?> 
      </td> 
      </tr> 
      <tr> 
      <td><?php echo $form['photo'];?></td> 
      </tr> 
      <tr> 
      <td><?php echo $form['about']; ?></td> 
      </tr> 
      <tr> 
      <td> 
       <div class="error_list"> 
       <?php echo $form['about']->renderError(); ?> 
       </div> 
      </td> 
      </tr> 
      <tr> 
      <td><?php echo $form['sex']; ?></td> 
      </tr> 
      <tr> 
      <td> 
       <div class="error_list"> 
       <?php echo $form['sex']->renderError(); ?> 
       </div> 
      </td> 
      </tr> 
      <tr> 
      <td><?php echo $form['mobile']; ?></td> 
      </tr> 
      <tr> 
      <div class="error_list"> 
       <?php echo $form['mobile']->renderError(); ?> 
      </div> 
      </tr> 
      <tr> 
      <td>Year: <?php echo $form['year']; ?>Month: <?php echo $form['month']; ?> Day: <?php echo $form['day']; ?></td> 
      </tr> 
      <tr> 
      <div class="error_list"> 
       <?php echo $form['year']->renderError(); ?> 
       <?php echo $form['month']->renderError(); ?> 
       <?php echo $form['day']->renderError(); ?> 
      </div> 
      </tr> 
      <tr> 
      <td><?php echo $form['slug']; ?></td> 
      </tr> 
      <tr> 
      <div class="error_list"> 
       <?php echo $form['slug']->renderError(); ?> 
       </div> 
      </tr> 
      <tr> 
      <td><input type="submit" value="Edit" /></td> 
      </tr> 
     </table> 
     </form> 
     <br /> 
     <br /> 
     <br /> 
     <a href="<?php echo url_for('home'); ?>" title="HomePage">HomePage</a> 
    </div> 
    </div> 
</div> 

這是我的EditProfileForm.class.php

<?php 

/** 
* Edit Profile form. 
*/ 
class EditProfileForm extends BaseUserForm{ 
    public function getModelName(){ 
     return 'User'; 
    } 

    public function configure(){ 
     $options = UserTable::getGender(); 
     $year = UserTable::getInstance()->getYear(); 
     $month = UserTable::getInstance()->getMonths(); 
     $day = UserTable::getInstance()->getDays(); 

     $this->setWidgets(array(
     'id' => new sfWidgetFormInputHidden(), 
     'first_name' => new sfWidgetFormInputText(array(),array('placeholder'=>'First Name: ')), 
     'last_name' => new sfWidgetFormInputText(array(),array('placeholder'=>'Last Name: ')), 
     'photo' => new sfWidgetFormInputFileEditable(array(
      'file_src' => sfConfig::get('app_GalleryPhoto_dir_150'), 
      'is_image' => true, 
      'edit_mode' => false, 
      'with_delete' => false 
     )), 
     'about' => new sfWidgetFormTextarea(array(), array('placeholder'=>'About Me: ')), 
     'sex' => new sfWidgetFormSelectRadio(
      array('choices' => $options) 
     ), 
     'mobile' => new sfWidgetFormInputText(array(),array('placeholder'=>'Mobile Number: ')), 
     'year' => new sfWidgetFormSelect(
      array('choices'=>$year) 
     ), 
     'month' => new sfWidgetFormSelect(
      array('choices'=>$month) 
     ), 
     'day' => new sfWidgetFormSelect(
      array('choices'=>$day) 
     ), 
     'slug' => new sfWidgetFormInputText(array(),array('placeholder'=>'Slug: ')), 

    )); 

     $this->setValidators(array(
     'id' => new sfValidatorDoctrineChoice(
       array('model' => $this->getModelName(), 
       'column' => 'id', 'required' => false)), 
     'first_name' => new sfValidatorString(
      array('trim' => true,'required' => true, 'max_length' => 20), 
      array('invalid' => 'required', 'required'=>'Please enter first name', 'max_length' => 'Firstname is too long. 20 characters maximum.') 
     ), 
     'last_name' => new sfValidatorString(
      array('trim' => true,'required' => true, 'max_length' => 20), 
      array('invalid' => 'required', 'required'=>'Please enter last name', 'max_length' => 'Lastname is too long. 20 characters maximum.') 
      ), 
      'photo' => new sfValidatorFile(array(
       'required'  => false, 
       'max_size'  => 500000000000000, 
       'mime_types'  => 'web_images', 
       'path'   => sfConfig::get('app_GalleryPhoto_dir_150'), 
     )), 

      'about' => new sfValidatorString(
      array('trim' => true,'required' => true) 
      ), 

      'sex' => new sfValidatorString(
      array('trim' => true,'required' => true), 
      array('required'=>'Please select gender.') 
      ), 
      'mobile' => new sfValidatorString(
       array('trim' => true,'required' => true, 'max_length' => 20), 
       array('invalid' => 'required', 'required'=>'Please enter mobile number', 'max_length' => 'mobile number is too long. 20 characters maximum.') 
      ), 
      'year' => new sfValidatorString(
       array('trim' => true), 
       array('required'=>'Please enter year.') 
      ), 
      'month' => new sfValidatorString(
       array('trim' => true), 
       array('required'=>'Please select month') 
      ), 
      'day' => new sfValidatorString(
       array('trim' => true), 
       array('required'=>'Please select day') 
      ), 
      'slug' => new sfValidatorString(
       array('trim' => true,'required' => true), 
       array('required'=>'Please enter Slug') 
      ), 

    )); 

     $this->widgetSchema->setNameFormat($this->getModelName().'[%s]'); 
     $this->validatorSchema->setPostValidator(new sfValidatorCallback(array('callback'=>array($this, 'validateForm')))); 
    } 

    public function validateForm($validator, $values){ 
     //check slug if special characters has been inputed. 
     /*$values = $values['slug']; 

     if(preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/',$values)){ 
     $error = new sfValidatorError($validator, 'Invalid Slug Characters.'); 
     $error_data['slug'] = $error; 
     }else{ 
     return true; 
     } 
     if(preg_replace('~[^\\pL\d]+~u', '', $values)){ 
     $error = new sfValidatorError($validator, 'Invalid Slug, At Least No Spaces'); 
     $error_data['slug'] = $error; 
     }*/ 

     /*if($values['slug']){ 
     $slug = Doctrine_Core::getTable('User')->findOneBySlug($values['slug']); 
     if($values['slug'] != $slug){ 
      $error = new sfValidatorError($validator, 'Slug Already Exists.'); 
      $error_data['slug'] = $error; 
     } 
     }*/ 

     if($values['slug']){ 
     if(!$values['slug']){ 
      $error = new sfValidatorError($validator, 'Invalid slug.'); 
      $error_data['slug'] = $error; 
     }else{ 
      if(isset($values['id'])){ 
      $user = UserTable::getInstance()->checkSlug($values['slug'],$values['id']); 
      }else{ 
      $user = UserTable::getInstance()->findOneBySlug($values['slug']); 
      } 
      if($user->count()){ 
      $error = new sfValidatorError($validator, 'Slug is already taken.'); 
      $error_data['slug'] = $error; 
      } 
     } 
     } 

     //check strings if there are special characters. 
     if($values['slug']){ 
     if(preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/',$values['slug'])){ 
      $error = new sfValidatorError($validator, 'Invalid Slug Characters.'); 
      $error_data['slug'] = $error; 
     } 
     } 

     //check two strings if it has spaces 
     if(preg_match('/\s/',$values['slug'])){ 
     $error = new sfValidatorError($validator, 'Invalid Slug. At Least No Spaces'); 
     $error_data['slug'] = $error; 
     } 

     if(isset($error_data)){ 
     throw new sfValidatorErrorSchema($validator, $error_data); 
     } 
     return $values; 

    } 
} 

,這是我UserTableClass.php代碼

<?php 
/** 
* UserTable 
* 
* This class has been auto-generated by the Doctrine ORM Framework 
*/ 
class UserTable extends Doctrine_Table 
{ 
    /** 
    * Returns an instance of this class. 
    * 
    * @return object UserTable 
    */ 
    public static function getInstance() 
    { 
     return Doctrine_Core::getTable('User'); 
    } 

    public static function getGender(){ 
     return array(
     1 => 'Male', 
     2 => 'Female' 
    ); 
    } 

    public static function getDays(){ 
     return array(
     '' => '----', 
     1 => '1', 
     2 => '2', 
     3 => '3', 
     4 => '4', 
     5 => '5', 
     6 => '6', 
     7 => '7', 
     8 => '8', 
     9 => '9', 
     10 => '10', 
     11 => '11', 
     12 => '12', 
     13 => '13', 
     14 => '14', 
     15 => '15', 
     16 => '16', 
     17 => '17', 
     18 => '18', 
     19 => '19', 
     20 => '20', 
     21 => '21', 
     22 => '22', 
     23 => '23', 
     24 => '24', 
     25 => '25', 
     26 => '26', 
     27 => '27', 
     28 => '28', 
     29 => '29', 
     30 => '30', 
     31 => '31'  
    ); 
    } 

    public static function getMonths(){ 
     return array(
     '' => '----', 
     1 => 'January', 
     2 => 'February', 
     3 => 'March', 
     4 => 'April', 
     5 => 'May', 
     6 => 'June', 
     7 => 'July', 
     8 => 'August', 
     9 => 'September', 
     10 => 'October', 
     11 => 'November', 
     12 => 'December'  
    ); 
    } 

    public static function getYear(){ 
     return array(
     '' => '----', 
     2014 => '2014', 
     2013 => '2013', 
     2012 => '2012', 
     2011 => '2011', 
     2010 => '2010', 
     2009 => '2009', 
     2008 => '2008', 
     2007 => '2007', 
     2006 => '2006', 
     2005 => '2005', 
     2004 => '2004', 
     2003 => '2003', 
     2002 => '2002', 
     2001 => '2001', 
     2000 => '2000', 
     1999 => '1999', 
     1998 => '1998', 
     1997 => '1997', 
     1996 => '1996', 
     1995 => '1995', 
     1994 => '1994', 
     1993 => '1993', 
     1992 => '1992', 
     1991 => '1991', 
     1990 => '1990', 
     1989 => '1989', 
     1988 => '1988', 
     1987 => '1987', 
     1986 => '1986', 
     1985 => '1985', 
     1984 => '1984', 
     1983 => '1983', 
     1982 => '1982', 
     1981 => '1981', 
     1980 => '1980' 
    ); 
    } 

    public function checkSlug($slug, $id=NULL){ 
     $q = Doctrine_Query::create() 
     ->from('User u') 
     ->where('u.slug = ?',$slug); 

     if($id){ 
     $q->andWhere('u.id <> ?',$id); 
     } 

     $q->limit(1); 
     return $q->execute(); 
    } 

} 

,並在那裏我UserTable.class.php文件我把代碼公共職能getDays()getMonths()得到年()。

現在當我點擊編輯配置文件時,所有的數據都會顯示出來,對於出生日期的execpt將不會被選中。任何人都可以幫我解決這個問題嗎?

回答

0

在你EditProfileForm.php設置的出生日期位作爲默認值3種選擇:

$bd = $this->getObject()->getBirthDate(); 
$bits = explode('-', $bd); 
$this->setDefault('year', $bits[0]); 
$this->setDefault('month', $bits[1]); 
$this->setDefault('day', $bit[2]); 
+0

感謝您的意見..我剛剛得到的解決方案!同樣的解決方案給你的答案對不起後期閱讀... –

+0

沒問題 - 如果答案是有效的,但請標記爲幫助他人或添加自己的答案。 – cyberwombat