「Symfony」カテゴリーアーカイブ

Using Constants in YAML Configuration Files

symfony PHP5 framework Chapter 5 - Configuring Symfony

Using Constants in YAML Configuration Files

Some configuration settings rely on the value of other settings. To avoid setting the same value twice, symfony supports constants in YAML files. On encountering a setting name (one that can be accessed by sfConfig::get()) in capital letters enclosed in % signs, the configuration handlers replace them with their current value. See Listing 5-20 for an example.

Listing 5-20 - Using Constants in YAML Files, Example from autoload.yml

autoload:
  symfony:
    name:           symfony
    path:           %SF_SYMFONY_LIB_DIR%
    recursive:      on
    exclude:        [vendor]

The path parameter will take the value returned by sfConfig::get('sf_symfony_lib_dir'). If you want one configuration file to rely on another, you need to make sure that the file you rely on is already parsed (look in the symfony source to find out the order in which the configuration files are parsed). app.yml is one of the last files parsed, so you may rely on others in it.

request parameterからの更新

基本パターン

if ($this->hasRequestParameter('xxx'))
{
  $this->model->setXxx($this->getRequestParameter('xxx'));
}

または

if ($this->getRequestParameter('xxx') !== null)
{
  $this->model->setXxx($this->getRequestParameter('xxx'));
}

※チェックボックスの場合は、チェックボックスを含むフォームを処理するアクションで、以下のようにデフォルトをセットする必要がある。

if (!$this->hasRequestParameter('xxx'))
{
  $this->getRequest()->setParameter('xxx', false);
}

または

if ($this->getRequestParameter('xxx') == null)
{
  $this->getRequest()->setParameter('xxx', false);
}

日付
(input_date_tag, select_day_tag+select_month_tag+select_year_tag)

if ($this->hasRequestParameter('date'))
// または if ($this->getRequestParameter('date') !== null)
{
  if ($this->getRequestParameter('date'))
  {
    try
    {
      $dateFormat = new sfDateFormat($this->getUser()->getCulture());
      if (!is_array($this->getRequestParameter('date')))
      {
        $value = $dateFormat->format($this->getRequestParameter('date'), 'i', $dateFormat->getInputPattern('d'));
      }
      else
      {
        $value_array = $this->getRequestParameter('date');
        if (is_numeric($value_array['year']) && is_numeric($value_array['month']) && is_numeric($value_array['day']))
        {
          $value = $value_array['year'].'-'.$value_array['month'].'-'.$value_array['day'].(isset($value_array['hour']) ? ' '.$value_array['hour'].':'.$value_array['minute'].(isset($value_array['second']) ? ':'.$value_array['second'] : '') : '');
        }
        else
        {
          $value = null;
        }
      }
      $this->model->setDate($value);
    }
    catch (sfException $e)
    {
      // not a date
    }
  }
  else
  {
    $this->model->setDate(null);
  }
}

日付時刻
日付とほぼ同じだが、値をテキストで受け取った場合(rich=true)のフォーマット引数が異なる。

$value = $dateFormat->format($this->getRequestParameter('date'), 'i', $dateFormat->getInputPattern('d'));

日付時刻

$value = $dateFormat->format($this->getRequestParameter('date'), 'I', $dateFormat->getInputPattern('g'));

Retrieve information about the current route

symfony PHP5 framework

Retrieve information about the current route
If you need to retrieve information about the current route, for instance to prepare a future 'back to page xxx' link, you should use the methods of the sfRouting object. For instance, if your routing.yml defines:

<code>
my_rule:
  url:   /call_my_rule
  param: { module: mymodule, action: myaction }
</code>

Use the following calls in the action:

<code>
// if you require an URL like
http://myapp.example.com/call_my_rule/param1/xxx/param2/yyy
 
$uri = sfRouting::getInstance()->getCurrentInternalUri();
// will return 'mymodule/myaction?param1=xxx&param2=yyy'
 
$uri = sfRouting::getInstance()->getCurrentInternalUri(true);
// will return '@myrule?param1=xxx&param2=yyy'
 
$route = sfRouting::getInstance()->getCurrentRouteName();
// will return 'myrule'
</code>

The URIs returned by the ->getCurrentInternalUri() method can be used in a call to a link_to() helper.
In addition, you might want to get the first or the last action called in a template. The following variables are automatically updated at each request and are available to templates:
$sf_first_action
$sf_first_module
$sf_last_action
$sf_last_module

Complement complex SQL in SYMFONY

Code Snippets
Complement complex SQL in SYMFONY
http://propel.phpdb.org/trac/ticket/57
集計関数を使用する例

SELECT MIN(reserve.MINUTES_PER_UNIT), reserve.NUMBER_PER_UNIT
FROM reserve
WHERE reserve.TYPE='time' AND reserve.RESERVE_GROUP_ID=1
GROUP BY reserve.TYPE,reserve.RESERVE_GROUP_ID
$c = new Criteria();
$c->add(ReservePeer::TYPE, $reserve_type);
$c->add(ReservePeer::RESERVE_GROUP_ID, $reserve_group_id);
$c->addSelectColumn('MIN('.ReservePeer::MINUTES_PER_UNIT.')');
$c->addGroupByColumn(ReservePeer::TYPE);
$c->addGroupByColumn(ReservePeer::RESERVE_GROUP_ID);
$rs = ReservePeer::doSelectRS($c);
while ($rs->next())
{
  $minutes_per_unit = $rs->getInt(1);
}

File structure explained

http://www.symfony-project.com/book/trunk/file_structure
symfonyのディレクトリパスは、sfConfigで取得できる。


// root directory structure
'sf_cache_dir_name'   => 'cache',
'sf_log_dir_name'     => 'log',
'sf_lib_dir_name'     => 'lib',
'sf_model_dir_name'   => 'model',
'sf_web_dir_name'     => 'web',
'sf_data_dir_name'    => 'data',
'sf_config_dir_name'  => 'config',
'sf_apps_dir_name'    => 'apps',
 
// global directory structure
'sf_app_dir'        => $sf_root_dir.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.$sf_app,
'sf_model_dir'      => $sf_root_dir.DIRECTORY_SEPARATOR.'model',
'sf_lib_dir'        => $sf_root_dir.DIRECTORY_SEPARATOR.'lib',
'sf_web_dir'        => $sf_root_dir.DIRECTORY_SEPARATOR.'web',
'sf_upload_dir'     => $sf_root_dir.DIRECTORY_SEPARATOR.'web'.DIRECTORY_SEPARATOR.'uploads',
'sf_base_cache_dir' => $sf_root_dir.DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR.$sf_app,
'sf_cache_dir'      => $sf_root_dir.DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR.$sf_app.DIRECTORY_SEPARATOR.$sf_environment,
'sf_log_dir'        => $sf_root_dir.DIRECTORY_SEPARATOR.'log',
'sf_data_dir'       => $sf_root_dir.DIRECTORY_SEPARATOR.'data',
'sf_config_dir'     => $sf_root_dir.DIRECTORY_SEPARATOR.'config',