Symfony 框架基础知识点集锦一

 

日期间天数的计算

$a=date("y-m-d",time());  
$b=$resultarray['date'];  
$c=strtotime($a)-strtotime($b);  
$c=ceil($c/(60*60*24));

----------------------------- 2009-07-27 17:33:04 ------------------------------

一条记录,Copy为另一条记录

$mNewObj    = new DbMccAccount();
$mObj    = $mccAccount['mobj'];
$mObj->setId(null);
$mObj->setSemUserId($uid);
$mObj->copyInto($mNewObj);
$mNewObj->save();
$mid    = $mNewObj->getId();
$aNewObj    = new DbAccount();
$aObj    = $mccAccount['aobj'];
$aObj->setId('');
$aObj->setSemMccAccountId($mid);
$aObj->copyInto($aNewObj);
$aNewObj->save();

--------------------------------------------------------------------------------
同一个字段的两个条件书写方法:

$c1= $criteria_task->getNewCriterion(SpotTaskPeer::SPOT_PLAN_DATE, $start_date, Criteria::GREATER_EQUAL);
$c2= $criteria_task->getNewCriterion(SpotTaskPeer::SPOT_PLAN_DATE, $end_date, Criteria::LESS_EQUAL);
$c1->addAnd($c2);

----------------------------- 2009-07-15 09:10:14 ------------------------------

$c = new Criteria();
$c->add(AuthorPeer::LAST_NAME, array("Tolstoy", "Dostoevsky", "Bakhtin"), Criteria::IN);

$authors = AuthorPeer::doSelect($c);

----------------------------- 2009-07-14 16:30:37 ------------------------------

// Find all authors with first name "Leo" OR last name of
// "Tolstoy", "Dostoevsky", or "Bakhtin"
$c = new Criteria();
$cton1 = $c->getNewCriterion(AuthorPeer::FIRST_NAME, "Leo");
$cton2 = $c->getNewCriterion(AuthorPeer::LAST_NAME,
                      array("Tolstoy", "Dostoevsky", "Bakhtin"), Criteria::IN);
// combine them
$cton1->addOr($cton2);
// add to Criteria
$c->add($cton1);

----------------------------- 2009-07-09 11:44:52 ------------------------------
模板从配置文件中读取值:


getActiveJobs(sfConfig::get('app_max_jobs_on_homepage')) as $i => $job): ?>

----------------------------- 2009-07-07 10:08:10 ------------------------------
模版中 设置 获得 当前用户的语言

setCulture('en_US') ?>
getCulture() ?>
你无需明确地将culture值传递给辅助函数。辅助函数会在当前的会话对象中
自动找到culture值。例13-4中列出的辅助函数在输出数据时都会考虑用户
的culture值。

----------------------------- 2009-07-06 16:23:24 ------------------------------
删除记录
Example 1: Delete using primary key

BookPeer::doDelete(1);
Example 2: Delete using instantiated object

$book = BookPeer::retrieveByPK(1);
BookPeer::doDelete($book);

$c    = new Criteria();
$c->add(JobeetJobPeer::TYPE, 'part-time');
$jobeet_jobs    = JobeetJobPeer::doDelete($c);

----------------------------- 2009-07-06 10:45:37 ------------------------------
select 列表书写方式:
1.select_tag  +   objects_for_select

2. select_tag + options_for_select

----------------------------- 2009-07-06 10:15:23 ------------------------------

表单动作,位于同一个动作脚本中:
// mymodule/actions/actions.class.php
public function executeEditAuthor()
{
  if ($this->getRequest()->getMethod() != sfRequest::POST)
  {
    // 显示表单
    return sfView::SUCCESS;
  }
  else
  {
    // 对提交的表单加以处理
    $name = $this->getRequestParameter('name');
    ...
    $this->redirect('mymodule/anotheraction');
  }
}
这段代码要能正常工作,表单处理和表单显示必须在同一个动作中。
// mymodule/templates/editAuthorSuccess.php

----------------------------- 2009-06-25 10:33:41 ------------------------------
不显示到模版中的动作
//    return sfView::NONE;

----------------------------- 2009-06-25 09:56:29 ------------------------------
Vim中u是撤销当前操作,U是恢复刚才撤销的操作。

----------------------------- 2009-06-25 09:44:55 ------------------------------
PHP
各个方法中通用的变量需要在Class中预定义。
引用的时候使用$this指针。

----------------------------- 2009-06-25 09:44:25 ------------------------------
General DB API Changes ?

Of course, any code in your application that was using the Creole API (e.g. returned from Propel::getConnection()) will now need to use the PDO API.

See http://www.php.net/pdo for more information on the PDO API. Generally, the PDO API is quite similar to Creole's, so updates are generally quite easy.

For example:

Propel 1.2 + Creole code:

 

$con = Propel::getConnection(SomeTablePeer::DATABASE_NAME);
$stmt = $con->prepareStatement("SELECT * FROM some_table WHERE name = ?");
$stmt->setString(1, $name);
$rs = $stmt->executeQuery();
while($rs->next()) {
   print "Name: " . $rs->getString("name") . "/n";
}

完整版
        $con = Propel::getConnection();
        $query = 'select %s.*, %s.action_name from %s, %s where %s=%s and %s=?';
        $query = sprintf($query,
          DbAuthorityGradeRelActionPeer::TABLE_NAME,
          DbAuthorityActionPeer::TABLE_NAME,
          DbAuthorityGradeRelActionPeer::TABLE_NAME,
          DbAuthorityActionPeer::TABLE_NAME,
          DbAuthorityGradeRelActionPeer::SEM_AUTHORITY_ACTION_ID,
          DbAuthorityActionPeer::ID,
          DbAuthorityGradeRelActionPeer::SEM_AUTHORITY_GRADE_ID
        );

        $stmt = $con->prepareStatement($query);
        $stmt->setInt(1,$aid);
        $rs = $stmt->executeQuery();
        return $rs;

Propel 1.3 + PDO code:

 

$con = Propel::getConnection(SomeTablePeer::DATABASE_NAME);
$stmt = $con->prepare("SELECT * FROM some_table WHERE name = ?");
$stmt->bindValue(1, $name);
$stmt->execute();
while($row = $stmt->fetch()) {
   print "Name: " . $row['name'] . "/n";
}

http://propel.phpdb.org/trac/wiki/Users/Documentation/1.3/Upgrading

example:
        $con    = Propel::getConnection();
        $query    = "select %s.*,%s.* from %s left join %s on %s=%s ";
        $query    .= "where %s=? and %s>? and %s        $query    = sprintf($query,
          MediaPlanPeer::TABLE_NAME,
          AdvertisersPeer::TABLE_NAME,
          MediaPlanPeer::TABLE_NAME,
          AdvertisersPeer::TABLE_NAME,
          MediaPlanPeer::ADVERTISER_ID,
          AdvertisersPeer::ID
          );
        $stmt    = $con->prepare($query);
        $stmt->execute();
        $hell    = $stmt;
//        $this->media_plannings    = $stmt->fetch();

        $this->media_plannings    = MediaPlanPeer::populateObjects($stmt);

while($row = $hell->fetch()) {
   print "Name: " . $row['media_plan_name'] . "
";
}

----------------------------- 2009-06-24 09:07:48 ------------------------------
i18n下面的文件名,不能带有下划线。

----------------------------- 2009-06-01 09:16:20 ------------------------------

表单辅助函数

form_tag()
------------------------------------------------

 
    =>

 

 
    =>

 

-------------------------------------------------

标准的表单元素

input_tag('name', 'default value', 'maxlength=20')

----------------------------- 2009-05-31 16:16:19 ------------------------------

模版快捷变量

在模版里,有一些Symfony变量可以直接使用。通过这些快捷变量可以从Symfony的对象中取得一些最常用的模版信息:
$sf_context:  完整的环境对象(context object), sfContext类的实例
$sf_request:  请求对象,sfRequest类的实例
$sf_params:   请求的参数
$sf_user:     当前的用户session对象,sfUser类的实例

$sf_request 请求对象的实例:

| 模版中获得请求的参数(http://www.a.cn/showpage.php?id=1,id就是请求showpage.php的参数)
| getParameter('id'); ?>
| 简版:
| get('id'); ?>
|
| 在动作(action)里面,相当于如下代码:
| echo $this->getRequestParameter('id')

--------------------------------------------------------------------------------

----------------------------- 2009-05-31 16:16:06 ------------------------------

模版中使用 Helper 时,先使用 use_helper() 函数载入:

TextHelper.php

truncate_text()        字符串截取函数
highlight_text()    高亮显示函数
excerpt_text()        截取摘要函数
wrap_text()        自定义行宽函数
simple_format_text()    格式化文本
auto_link_text()    自动设置文本的的链接
strip_links_text()    去除文本中的链接
_auto_link_urls()    转换文本中的URL为可点击的URL
_auto_link_email_addresses()    转换文本中Email为可点击的Email

----------------------------- 2009-05-31 15:19:05 ------------------------------

为项目设置虚拟主机

NameVirtualHost *:80

  ServerName weblog.loc
  DocumentRoot "D:/workspace/weblog/web"
 
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
    Order allow,deny
    Allow from all

  Alias /sf "D:/xampp/php5/pear/data/symfony/web/sf"
 
    AllowOverride All
    Allow from All

本机解析项目域名
打开文件:C:/WINDOWS/system32/drivers/etc/hosts
增加相应域名解析:
127.0.0.1       weblog.loc
127.0.0.1       uii.loc
127.0.0.1       jobeet.loc
127.0.0.1       askeet.loc
127.0.0.1       mysql.loc
127.0.0.1       localhost

----------------------------- 2009-05-31 11:59:27 ------------------------------

Symfony 建立项目流程

创建项目:
D:/workspace/weblog> symfony init-project weblog

创建模型:
// Create a schema.yml file in ProjecName/config/
D:/workspace/weblog> symfony propel-build-model

创建数据库:
D:/workspace/weblog>symfony propel-build-sql
D:/workspace/weblog>symfony propel-insert-sql

创建应用程序:
D:/workspace/weblog>symfony init-app frontend

创建模块:
D:/workspace/weblog>symfony init-module frontend post
D:/workspace/weblog>symfony init-module frontend comment
(创建脚手架:
D:/workspace/weblog>symfony propel-generate-crud frontend post Post
D:/workspace/weblog>symfony propel-generate-crud frontend comment Comment
)

----------------------------- 2009-05-31 10:50:10 ------------------------------

表单验证
------------------------

        methods:
          get:            [password1, password2]
          post:           [password1, password2]
        names:
          password1:
            required:     Yes
            required_msg: Please enter a password
          password2:
            required:     Yes
            required_msg: Please retype the password
            validators:   passwordValidator
        passwordValidator:
          class:          sfCompareValidator
          param:
            check:        password1
            compare_error: The passwords you entered do not match. Please try again.
/^/w+([-+.]/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*$/i

emailValidator:
  class:                  sfRegexValidator
    param:
      match:            Yes
      match_error:      Email 格式不正确
      pattern:              /^/w+([-+.]/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*$/i
--------------------------------------------------------

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值