2009-02-27

symfony数据表关联详解(for propel)

类归于: symfony — 标签:, , , , maker @ 16:52

上周又接到一个研究性质的任务,最近比较忙碌,所以这两天才开始分析代码,由于数据表相关的内容会比较多,所以本文将分成以下几部分。

  1. 商店的数据关系
  2. 一对多(hasMany)关系的处理
  3. 从属(belongsTo)关系的处理
  4. 多对多(manyToMany)关系的处理
  5. 一对一(oneToOne)关系的处理
  6. 总结

本文使用的测试环境如下:

  • php 5.2.6
  • apache 2.2.8
  • mysql 5.0.67
  • symfony 1.2.4-DEV

一. 商店的数据关系

我们用一个例子来对symfony中的表关联进行分析,我们假设商店有如下的数据关系。

图1.1

2009-02-26-104551_294x299_scrot

这里涉及到4类对象实体,分别是City(城市),Shop(商店),Shopinfo(商店信息), Tag(标签)。其中又涉及到了三种数据关系,一个城市可以有多家商店(一对多),一个商店拥有一个商店信息(一对一),一个商店可以拥有很多特点(多对多),这里我们用Tag来表示特点(特性)。

首先我们设计数据库结构config/schema.yml

图1.2

2009-02-26-100743_401x689_scrot

然后我们自动生成相关的库文件

$ ./symfony propel:build-all

首先我们来分析City和Shop之间的一对多/从属关系.

二.一对多(hasMany)关系的处理

我们先来看propel自动生成的BaseCity对象

图1.3

2009-02-25-103053_396x688_scrot

其他和关联没有关系的表这里不做过多描述, 我们重点介绍下面几个方法.

clearShops();

清空当前已经获得的关联商店, 实际上这个清空操作并没有真正意义上的进行清空或者删除,只是清空了当前取得的关联对象集合.

示例2.1(通过测试):

$mc = new Shop;
$mc->setName('MC in Beijing');
$city->addShop($mc);
$city->initShops();
$wc = new Shop;
$wc->setName('WC in Beijing');
$city->addShop($wc);
$city->save();

initShops();

清空当前已经获得的关联商店, 实际上这个清空操作并没有真正意义上的进行清空或者删除,只是清空了当前取得的关联对象集合. 该方法与clearShops()类似, 但该方法将Shop集合初始化为一个空的数组而clearShops()将Shop集合设置为null.

示例2.2(通过测试):

$mc = new Shop;
$mc->setName('MC in Beijing');
$city->addShop($mc);
$city->initShops();
$wc = new Shop;
$wc->setName('WC in Beijing');
$city->addShop($wc);
$city->save();

getShops($criteria, $con);

根据条件$criteria返回city下相关联的shop对象集合.

示例2.3(通过测试):

//我们用下面代码获得某城市下全部的KFC(肯德基)店铺
$c = new Criteria;
$c->add(ShopPeer::NAME, '%KFC%', Criteris::LIKE);
foreach($city->getShops($c) as $shop){
echo $shop->getId();
}

countShops($criteria, $con);

countShops()方法用来返回满组织定条件的关联Shop个数

示例2.4(通过测试):

//我们用下面代码获得某城市下全部的KFC(肯德基)的个数
$c = new Criteria;
$c->add(ShopPeer::NAME, '%KFC%', Criteris::LIKE);
echo $city->getName() . ' has ' . $city->countShops($c) . ' KFC shops.';

addShop($shop);

给city添加一个关联的shop, 参数$shop是一个shop对象

示例2.5(通过测试):

//我们用下面代码在某城市新加一个KFC店铺
$shop = new Shop;
$shop->setName('KFC 人民大街店');
$city->addShop($shop);
$city->save()

clearAllReferences($deep);

清空当前已经获得的关联对象, 实际上这个清空操作并没有真正意义上的进行清空或者删除,只是清空了当前已经取得的关联对象集合. 由于city只和shop进行了关联, 所以对于city对象来说其实该方法的效果和clearShops()是相同的.

示例2.6:

$mc = new Shop;
$mc->setName('MC in Beijing');
$city->addShop($mc);
$city->clearAllReferences();
$wc = new Shop;
$wc->setName('WC in Beijing');
$city->addShop($wc);
$city->save();

三.从属(belongsTo)关系的处理

下图为BaseShop对象的结构

图3.1 BaseShop

2009-02-26-123737_401x843_scrot

前面很多方法和BaseCity都是一样的, 而与BaseCity不同并与city相关的只有以下方法.

setCity(City $city)

指定所属city, 参数$city是city对象

示例3.1(通过测试):

//添加Changchun和KFC并同时创建关联
$city = new City;
$city->setName('Changchun');
$shop->new Shop;
$shop->setName('KFC in Changchun');
$shop->setCity($city);
$shop->save();

getCity($con)

返回所属城市,返回值为city对象.

示例3.2(通过测试):

// 输出店铺所在城市名
echo $shop->getCity()->getName();

四. 多对多(manyToMany)关系的处理

我们继续看例子, Shop对象和Tag对象通过Shop_Tag对象进行多对多的关联, 下面我们来看一下这三个对象都生成了哪些方法.

图4.1

2009-02-25-172720_395x647_scrot

图4.2

2009-02-25-172752_403x704_scrot

在symfony中多对多关系相对要复杂一些, 整个过程涉及到3种对象, 其中涉及到的方法也要多一些.

其中Shop对象的相关方法有

clearShopTags();

参考示例2.1

initShopTags();

参考示例2.2

getShopTags($criteria)

返回所有满足条件的相关联的ShopTag对象

示例4.1(通过测试):

foreach($shop->getShopTags() as $shopTag){
echo $shopTag->getTag()->getName();
}

countShopTags($criteria);

参考示例2.4

addShopTag($shoptag);

添加一个ShopTag关联, 参数$shoptag是ShopTag对象

我并没有想明白这个方法是用来作什么的, 按照正常的逻辑创建一个多对多关联应该是示例化一个新的ShopTag而不应该在Shop中addShopTag().

示例4.2(通过测试):

$shopTag = new ShopTag;
$shopTag->setTagId(2);
$shop->addShopTag($shopTag);
$shop->save();

getShopTagsJoinTag($criteria);

返回所有符合查询条件的ShopTag关联对象并同时取出关联的Tag对象.

在使用getShopTagsJoinTags()的时候你会发现用法其实和getShopTags()是一样的, 但使用getShopTagJoinTags()的之后再使用shopTag的getTag()方法实际上就不进行数据库查询了. 下面的例子要比getShopTags()的例子少很多次查询.

示例4.3(通过测试):

foreach($shop->getShopTagsJoinTag() as $shopTag){
echo $shopTag->getTag()->getName();
}

clearAllReferences($deep);

参考示例2.6

Tag对象的相关方法有

clearShopTags();

参考示例2.1

initShopTags();

参考示例2.2

getShopTags($criteria)

参考示例4.1

countShopTags($criteria);

参考示例2.4

addShopTag($shoptag);

参考示例4.2

getShopTagsJoinShop($criteria);

参考示例4.3

clearAllReferences($deep);

参考示例2.6

ShopTag对象的相关方法有

getShop();

返回关联的Shop对象

示例4.4(通过测试):

// 输出与tag关联的全部shop的name
foreach($tag->getShopTags() as $shopTag){
echo $shopTag->getShop()->getName();
}

setShop($shop);

设置关联Shop, $shop参数是Shop对象

getTag();

返回关联的Tag对象

示例4.5(通过测试):

// 输出与shop关联的全部tag的name
foreach($shop->getShopTags() as $shopTag){
echo $shopTag->getTag()->getName();
}

setTag($tag);

设置关联Tag, $tag参数是Tag对象

clearAllReferences($deep);

参考示例2.6

五.一对一(oneToOne)关系的处理

首先, 我们来看一下propel自动生成的Shopinfo对象, Shop对象在图3.1中已经有介绍.

图5.1 BaseShopinfo

2009-02-26-123139_398x617_scrot

经过几天的探索才成功的找到了进行一对一关联的方法.我们必须要将shopinfo中的外键设定为shopinfo的主键才可以被propel处理.

下面这几种都是错误的关联设计.

shop:
..id:
..shopinfo_id:
..name:
shopinfo:
..id:
..add:
..tel:

shop:
..id:
..name:
shopinfo:
..id:
..shop_id:
..add:
..tel:

shop:
..id:
..shopinfo_id:
..name:
shopinfo:
..id:
..shop_id:
..add:
..tel:

正确的一对一关联设计应该是

shop:
..id:
..name:
shopinfo:
..shop_id:
..add:
..tel:

根据图3.1图5.1, Shop对象拥有方法setShopinfo()和getShopinfo()方法来操作唯一的关联店铺信息, 而Shopinfo对象也拥有setShop()和getShop()方法来操作唯一的关联店铺.

示例5.1:

//给商店添加店铺信息
$shop->setShopinfo(new Shopinfo);
$shop->getShopinfo()->setAddress('a Place');
$shop->getShopinfo()->setTel('010-12345678');
$shop->save();

示例5.2:

// 修改店铺的店铺信息
$shop->getShopinfo()->setTel('010-87654321');
$shop->save();

六.总结

到这里为止我们就把symfony中propel对数据关联的操作介绍的差不多了, 可能还有一些方面没有详细的介绍也只能考各位自己钻研了. 介于本人能力有限, 本文难免会有错误和遗漏, 希望大家可以指出, 感慨一下, 这篇文章从策划到结束一共用了4天.

附件是本文进行测试的项目文件, 内无symfony库, 请将symfony/lib目录cp到lib目录下并重命名为symfony.

附件: references.tgz

(本文完)

2009-02-25

在layout中应用action定义的变量

类归于: symfonyzhuozi @ 13:21

在action中定义变量
// in the action
class ...Actions extends sfActions
{
public function execute...()
{
$this->title = 'foo';
}
}

在template取得变量,再传给layout
// in the template
<?php slot('title', $title) ?>

在layout中,显示变量
// in the layout
<?php echo get_slot('title') ?>

Symfony中组件槽(Component Slot)的应用

类归于: PHP, symfonyzhuozi @ 13:01

在某种程度上,组件槽有点类似与局部模板,只是运用的作用上有所不同,组件槽可以用于面包屑型的导航、上下文相关的导航,还有各种动态插入。作为组件,它们可以用在全局模板、普通模板,甚至是在其他的组件里。
如何载入组件槽?
<?php include_component_slot('sidebar') ?>

如何在myapp/config/view.yml里定义sidebar组件槽的默认组件
default:
components:
sidebar: [bar, default]

这个方法会显示位于modules/bar/templates/目录下的_default.php局部模板文件。
现在我们拿实际的例子,来说明一下,具体应该怎么应用组件槽。
在myapp/modules/user/config/view.yml里给sidebar组件槽指定不同于默认值的值
all:
components:
sidebar: [bar, user]

sidebar槽使用的组件,该文件位于:modules/bar/actions/components.class.php
class barComponents extends sfComponents
{
public function executeDefault()
{
}
public function executeUser()
{
$test = 'Hello World';
//这里你可以获得一些数据,或者其他,便于在组件槽中的显示。例如
$user = UserPeer::getUser();
$this->user = $user;
}
}

在sidebar组件槽的局部模板,该文件位于: modules/bar/templates
// _user.php
<p>Name: <?php echo $user->getName(); ?></p>

这里整个组件槽的应用的过程就是这样了。
如果你在某个模块里想暂停使用一个组件,只要再声明一个空的模块/组件定义就可以了。
例如在view.yml里取消一个组件槽
all:
components:
sidebar: []

参考官网symfony

2009-02-24

symfony命令行详解

类归于: PHP, symfony — 标签:, maker @ 13:05

./symfony list

语法:
symfony [选项] 任务名 [参数]

选项:
–dry-run -n Do a dry run without executing actions.
–help -H 显示帮助信息
–quiet -q Do not log messages to standard output.
–trace -t Turn on invoke/execute tracing, enable full backtrace.
–version -V 显示程序版本
可用的任务:
help 显示任务的帮助信息 (简写h)

list 列出任务

app:routes 显示一个应用程序当前的路由信息

cache:clear 清空缓存(简写cc, clear-cache)

configure:author 设置项目作者
configure:database 设置数据库 DSN

generate:app 初始化一个应用程序 (简写init-app)
generate:module 初始化一个模块 (简写init-module)
generate:project 初始化一个项目 (简写init-proj)
generate:task Creates a skeleton class for a new task

i18n:extract Extracts i18n strings from php files
i18n:find Finds non “i18n ready” strings in an application

log:clear 清空日志 (log-purge)
log:rotate Rotates an application log files (log-rotate)

plugin:add-channel 添加一个新的PEAR频道
plugin:install 安装一个插件 (简写plugin-install)
plugin:list 列出已安装的插件 (简写plugin-list)
plugin:publish-assets 将全部插件的web目录发布到项目的web目录下
plugin:uninstall 卸载一个插件 (plugin-uninstall)
plugin:upgrade 升级一个插件(plugin-upgrade)

project:clear-controllers Clears all non production environment controllers (clear-controllers)
project:deploy 发布一个项目到另外一台服务器 (sync)
project:disable 在指定环境下屏蔽一个应用程序 (disable)
project:enable 在指定环境下打开一个应用程序 (disable)
project:freeze 冻结 symfony 库文件 (简写freeze)
project:permissions 修改symfony项目目录的权限 (permissions, fix-perms)
project:unfreeze 解冻 symfony 库文件 (简写unfreeze)
project:upgrade1.1 将一个symfony项目升级到1.1
project:upgrade1.2 将一个symonfy项目从1.1升级到1.2

propel:build-all 生成model,form,生成sql语句并创建数据库 (propel-build-all)
propel:build-all-load 生成model,form,生成sql语句,创建数据库并插入初始数据 (propel-build-all-load)
propel:build-filters 根据当前的模型创建过滤器
propel:build-forms 根据当前的模型创建表单
propel:build-model 根据数据库结构创建模型 (propel-build-model)
propel:build-schema 根据数据库结构创建schema (propel-build-schema)
propel:build-sql 根据当前模型创建一个SQL (简写propel-build-sql)
propel:data-dump 导出数据到 fixtures directory (propel-dump-data)
propel:data-load 从fixtures directory载入数据 (propel-load-data)
propel:generate-admin 自动生成一个module的后台
propel:generate-module Generates a Propel module (propel-generate-crud, propel:generate-crud)
propel:generate-module-for-route Generates a Propel module for a route definition
propel:graphviz Generates a graphviz chart of current object model
propel:init-admin 初始化一个module后台(propel-init-admin)
propel:insert-sql Inserts SQL for current model (propel-insert-sql)
propel:schema-to-xml 根据schema.yml生成schema.xml (propel-convert-yml-schema)
propel:schema-to-yml 根据schema.xml生成schema.yml (propel-convert-xml-schema)

test:all 运行全部测试 (简写test-all)
test:coverage Outputs test code coverage
test:functional 运行全部功能测试 (简写test-functional)
test:unit 运行单元测试 (test-unit)

./symfony help app:routes

语法:
symfony app:routes application [name]

参数:
application The application name
name A route name

描述:
The app:routes displays the current routes for a given application:

./symfony app:routes frontend

./symfony help cc

语法:
symfony cache:clear [--app[="..."]] [--env[="..."]] [--type[="..."]]

别名: cc, clear-cache

选项:
–app The application name
–env The environment
–type The type (default: all)

描述:
The cache:clear task clears the symfony cache.

By default, it removes the cache for all available types, all applications,
and all environments.

You can restrict by type, application, or environment:

For example, to clear the frontend application cache:

./symfony cache:clear –app=frontend

To clear the cache for the prod environment for the frontend application:

./symfony cache:clear –app=frontend –env=prod

To clear the cache for all prod environments:

./symfony cache:clear –env=prod

To clear the config cache for all prod environments:

./symfony cache:clear –type=config –env=prod

The built-in types are: config, i18n, routing, module
and template.

./symfony help configure:author

语法:
symfony configure:author author

参数:
author The project author

描述:
The configure:author task configures the author for a project:

./symfony configure:author “Fabien Potencier <fabien.potencier@symfony-project.com>”

The author is used by the generates to pre-configure the PHPDoc header for each generated file.

The value is stored in [config/properties.ini].

./symfony help configure:database

语法:
symfony configure:database [--env[="..."]] [--name[="..."]] [--class[="..."]] [--app[="..."]] dsn [username] [password]

参数:
dsn The database dsn
username The database username (default: root)
password The database password

选项:
–env The environment (default: all)
–name The connection name (default: propel)
–class The database class name (default: sfPropelDatabase)
–app The application name

描述:
The configure:database task configures the database DSN
for a project:

./symfony configure:database mysql:host=localhost;dbname=example root mYsEcret

By default, the task change the configuration for all environment. If you want
to change the dsn for a specific environment, use the env option:

./symfony configure:database –env=dev mysql:host=localhost;dbname=example_dev root mYsEcret

To change the configuration for a specific application, use the app option:

./symfony configure:database –app=frontend mysql:host=localhost;dbname=example root mYsEcret

You can also specify the connection name and the database class name:

./symfony configure:database –name=main –class=sfDoctrineDatabase mysql:host=localhost;dbname=example root

WARNING: The propel.ini file is also updated when you use a Propel database
and configure for all environments with no app.

./symfony help generate:app

语法:
symfony generate:app [--escaping-strategy="..."] [--csrf-secret="..."] application

别名: init-app

参数:
application The application name

选项:
–escaping-strategy Output escaping strategy (default: )
–csrf-secret Secret to use for CSRF protection (default: )

描述:
The generate:app task creates the basic directory structure
for a new application in the current project:

./symfony generate:app frontend

This task also creates two front controller scripts in the
web/ directory:

web/%application%.php for the production environment
web/%application%_dev.php for the development environment

For the first application, the production environment script is named
index.php.

If an application with the same name already exists,
it throws a sfCommandException.

You can enable output escaping (to prevent XSS) by using the escaping-strategy option:

./symfony generate:app frontend –escaping-strategy=on

You can enable session token in forms (to prevent CSRF) by defining
a secret with the csrf-secret option:

./symfony generate:app frontend –csrf-secret=UniqueSecret

./symfony help generate:module

语法:
symfony generate:module application module

别名: init-module

参数:
application The application name
module The module name

描述:
The generate:module task creates the basic directory structure
for a new module in an existing application:

./symfony generate:module frontend article

The task can also change the author name found in the actions.class.php
if you have configure it in config/properties.ini:

[symfony]
name=blog
author=Fabien Potencier <fabien.potencier@sensio.com>

You can customize the default skeleton used by the task by creating a
%sf_data_dir%/skeleton/module directory.

The task also creates a functional test stub named
%sf_test_dir%/functional/%application%/%module%ActionsTest.class.php
that does not pass by default.

If a module with the same name already exists in the application,
it throws a sfCommandException.

./symfony help generate:project

用方法:
symfony generate:project name

别名: init-project

参数:
name The project name

描述:
The generate:project task creates the basic directory structure
for a new project in the current directory:

./symfony generate:project blog

If the current directory already contains a symfony project,
it throws a sfCommandException.

./symfony help generate:task

语法:
symfony generate:task [--dir="..."] [--use-database="..."] [--brief-description="..."] task_name

参数:
task_name The task name (can contain namespace)

选项:
–dir The directory to create the task in (default: lib/task)
–use-database Whether the task needs model initialization to access database (default: propel)
–brief-description A brief task description (appears in task list)

描述:
The generate:task creates a new sfTask class based on the name passed as
argument:

./symfony generate:task namespace:name

The namespaceNameTask.class.php skeleton task is created under the lib/task/
directory. Note that the namespace is optional.

If you want to create the file in another directory (relative to the project
root folder), pass it in the –dir option. This directory will be created
if it does not already exist.

./symfony generate:task namespace:name –dir=plugins/myPlugin/lib/task

If you want the task to default to a connection other than propel, provide
the name of this connection with the –use-database option:

./symfony generate:task namespace:name –use-database=main

The –use-database option can also be used to disable database
initialization in the generated task:

./symfony generate:task namespace:name –use-database=false

You can also specify a description:

./symfony generate:task namespace:name –brief-description=”Does interesting things”

./symfony help i18n:extract

语法:
symfony i18n:extract [--display-new] [--display-old] [--auto-save] [--auto-delete] application culture

参数:
application The application name
culture The target culture

选项:
–display-new Output all new found strings
–display-old Output all old strings
–auto-save Save the new strings
–auto-delete Delete old strings

描述:
The i18n:extract task extracts i18n strings from your project files
for the given application and target culture:

./symfony i18n:extract frontend fr

By default, the task only displays the number of new and old strings
it found in the current project.

If you want to display the new strings, use the –display-new option:

./symfony i18n:extract –display-new frontend fr

To save them in the i18n message catalogue, use the –auto-save option:

./symfony i18n:extract –auto-save frontend fr

If you want to display strings that are present in the i18n messages
catalogue but are not found in the application, use the
–display-old option:

./symfony i18n:extract –display-old frontend fr

To automatically delete old strings, use the –auto-delete but
be careful, especially if you have translations for plugins as they will
appear as old strings but they are not:

./symfony i18n:extract –auto-delete frontend fr

./symfony help i18n:find

语法:
symfony i18n:find [--env="..."] application

参数:
application The application name

选项:
–env The environment (default: dev)

描述:
The i18n:find task finds non internationalized strings embedded in templates:

./symfony i18n:find frontend

This task is able to find non internationalized strings in pure HTML and in PHP code:

Non i18n text

As the task returns all strings embedded in PHP, you can have some false positive (especially
if you use the string syntax for helper arguments).

./symfony help log:clear

语法:
symfony log:clear

别名: log-purge

描述:
The log:clear task clears all symfony log files:

./symfony log:clear

./symfony help log:rotate

语法:
symfony log:rotate [--history="..."] [--period="..."] application env

别名: log-rotate

参数:
application The application name
env The environment name

选项:
–history The maximum number of old log files to keep (default: 10)
–period The period in days (default: 7)

描述:
The log:rotate task rotates application log files for a given
environment:

./symfony log:rotate frontend dev

You can specify a period or a history option:

./symfony –history=10 –period=7 log:rotate frontend dev

./symfony help plugin:add-channel

语法:
symfony plugin:add-channel name

参数:
name The channel name

描述:
The plugin:add-channel task adds a new PEAR channel:

./symfony plugin:add-channel symfony.plugins.pear.example.com

./symfony help plugin:install

语法:
symfony plugin:install [--stability|-s="..."] [--release|-r="..."] [--channel|-c="..."] [--install_deps|-d] [--force-license] name

别名: plugin-install

参数:
name The plugin name

选项:
–stability (-s) The preferred stability (stable, beta, alpha)
–release (-r) The preferred version
–channel (-c) The PEAR channel name
–install_deps (-d) Whether to force installation of required dependencies
–force-license Whether to force installation even if the license is not MIT like

描述:
The plugin:install task installs a plugin:

./symfony plugin:install sfGuardPlugin

By default, it installs the latest stable release.

If you want to install a plugin that is not stable yet,
use the stability option:

./symfony plugin:install –stability=beta sfGuardPlugin
./symfony plugin:install -s beta sfGuardPlugin

You can also force the installation of a specific version:

./symfony plugin:install –release=1.0.0 sfGuardPlugin
./symfony plugin:install -r 1.0.0 sfGuardPlugin

To force installation of all required dependencies, use the install_deps flag:

./symfony plugin:install –install-deps sfGuardPlugin
./symfony plugin:install -d sfGuardPlugin

By default, the PEAR channel used is symfony-plugins
(plugins.symfony-project.org).

You can specify another channel with the channel option:

./symfony plugin:install –channel=mypearchannel sfGuardPlugin
./symfony plugin:install -c mypearchannel sfGuardPlugin

Or you can use the channel/package notation:

./symfony plugin:install mypearchannel/sfGuardPlugin

You can also install PEAR packages hosted on a website:

./symfony plugin:install http://somewhere.example.com/sfGuardPlugin-1.0.0.tgz

Or local PEAR packages:

./symfony plugin:install /home/fabien/plugins/sfGuardPlugin-1.0.0.tgz

If the plugin contains some web content (images, stylesheets or javascripts),
the task creates a %name% symbolic link for those assets under web/.
On Windows, the task copy all the files to the web/%name% directory.

./symfony help plugin:list

语法:
symfony plugin:list

别名: plugin-list

描述:
The plugin:list task lists all installed plugins:

./symfony plugin:list

It also gives the channel and version for each plugin.

./symfony help plugin:publish-assets

语法:
symfony plugin:publish-assets [--core-only] [--symfony-lib-dir="..."]

选项:
–core-only If set only core plugins will publish their assets
–symfony-lib-dir The symfony lib dir

描述:
The plugin:publish-assets task will publish web assets from all plugins.

./symfony plugin:publish-assets

In fact this will send the plugin.post_install event to each plugin.

./symfony help plugin:uninstall

语法:
symfony plugin:uninstall [--channel|-c="..."] [--install_deps|-d] name

别名: plugin-uninstall

参数:
name The plugin name

选项:
–channel (-c) The PEAR channel name
–install_deps (-d) Whether to force installation of dependencies

描述:
The plugin:uninstall task uninstalls a plugin:

./symfony plugin:uninstall sfGuardPlugin

The default channel is symfony.

You can also uninstall a plugin which has a different channel:

./symfony plugin:uninstall –channel=mypearchannel sfGuardPlugin

./symfony plugin:uninstall -c mypearchannel sfGuardPlugin

Or you can use the channel/package notation:

./symfony plugin:uninstall mypearchannel/sfGuardPlugin

You can get the PEAR channel name of a plugin by launching the
plugin:list] task.

If the plugin contains some web content (images, stylesheets or javascripts),
the task also removes the [web/%name% symbolic link (on *nix)
or directory (on Windows).

./symfony help plugin:upgrade

语法:
symfony plugin:upgrade [--stability|-s="..."] [--release|-r="..."] [--channel|-c="..."] name

别名: plugin-upgrade

参数:
name The plugin name

选项:
–stability (-s) The preferred stability (stable, beta, alpha)
–release (-r) The preferred version
–channel (-c) The PEAR channel name

描述:
The plugin:upgrade task tries to upgrade a plugin:

./symfony plugin:upgrade sfGuardPlugin

The default channel is symfony.

If the plugin contains some web content (images, stylesheets or javascripts),
the task also updates the web/%name% directory content on Windows.

See plugin:install for more information about the format of the plugin name and options.

./symfony help project:clear-controllers

语法:
symfony project:clear-controllers

别名: clear-controllers

描述:
The project:clear-controllers task clears all non production environment
controllers:

./symfony project:clear-controllers

You can use this task on a production server to remove all front
controller scripts except the production ones.

If you have two applications named frontend and backend,
you have four default controller scripts in web/:

index.php
frontend_dev.php
backend.php
backend_dev.php

After executing the project:clear-controllers task, two front
controller scripts are left in web/:

index.php
backend.php

Those two controllers are safe because debug mode and the web debug
toolbar are disabled.

./symfony help project:deploy

语法:
symfony project:deploy [--go] [--rsync-dir="..."] [--rsync-options[="..."]] server

别名: sync

参数:
server The server name

选项:
–go Do the deployment
–rsync-dir The directory where to look for rsync*.txt files (default: config)
–rsync-options To options to pass to the rsync executable (default: -azC –force –delete)

描述:
The project:deploy task deploys a project on a server:

./symfony project:deploy production

The server must be configured in config/properties.ini:

[production]
host=www.example.com
port=22
user=fabien
dir=/var/www/sfblog/
type=rsync

To automate the deployment, the task uses rsync over SSH.
You must configure SSH access with a key or configure the password
in config/properties.ini.

By default, the task is in dry-mode. To do a real deployment, you
must pass the –go option:

./symfony project:deploy –go production

Files and directories configured in config/rsync_exclude.txt are
not deployed:

.svn
/web/uploads/*
/cache/*
/log/*

You can also create a rsync.txt and rsync_include.txt files.

If you need to customize the rsync*.txt files based on the server,
you can pass a rsync-dir option:

./symfony project:deploy –go –rsync-dir=config/production production

Last, you can specify the options passed to the rsync executable, using the
rsync-options option (defaults are -azC):

./symfony project:deploy –go –rsync-options=avz

./symfony help project:disable

语法:
symfony project:disable application env

别名: disable

参数:
application The application name
env The environment name

描述:
The project:disable task disables an application for a specific environment:

./symfony project:disable frontend prod

./symfony help project:enable

语法:
symfony project:enable application env

别名: enable

参数:
application The application name
env The environment name

描述:
The project:enable task enables an application for a specific environment:

./symfony project:enable frontend prod

./symfony help project:freeze

语法:
symfony project:freeze symfony_data_dir

别名: freeze

参数:
symfony_data_dir The symfony data directory

描述:
The project:freeze task copies all the symfony core files to
the current project:

./symfony project:freeze /path/to/symfony/data/directory

The task takes a mandatory argument of the path to the symfony data
directory.

The task also changes config/config.php to switch to the
embedded symfony files.

./symfony help project:permissions

语法:
symfony project:permissions

别名: permissions, fix-perms

描述:
The project:permissions task fixes directory permissions:

./symfony project:permissions

./symfony help project:unfreeze

语法:
symfony project:unfreeze

别名: unfreeze

描述:
The project:unfreeze task removes all the symfony core files from
the current project:

./symfony project:unfreeze

The task also changes config/config.php to switch to the
old symfony files used before the project:freeze command was used.

./symfony help project:upgrade1.1

语法:
symfony project:upgrade1.1

描述:
The project:upgrade1.1 task upgrades a symfony project
based the 1.0 release to the 1.1 symfony release.

./symfony project:upgrade1.1

Please read the UPGRADE_TO_1_1 file to have information on what does this task.

./symfony help project:upgrade1.2

语法:
symfony project:upgrade1.2

描述:
The project:upgrade1.2 task upgrades a symfony project
based on the 1.1 release to the 1.2 symfony release.

./symfony project:upgrade1.2

Please read the UPGRADE_TO_1_2 file to have information on what does this task.

./symfony help propel:build-all

语法:
symfony propel:build-all [--application[="..."]] [--env="..."] [--connection="..."] [--no-confirmation] [--skip-forms|-F] [--classes-only|-C] [--phing-arg="..."]

别名: propel-build-all

选项:
–application The application name (default: 1)
–env The environment (default: dev)
–connection The connection name (default: propel)
–no-confirmation Do not ask for confirmation
–skip-forms (-F) Skip generating forms
–classes-only (-C) Do not initialize the database
–phing-arg Arbitrary phing argument (multiple values allowed)

描述:
The propel:build-all task is a shortcut for five other tasks:

./symfony propel:build-all

The task is equivalent to:

./symfony propel:build-model
./symfony propel:build-forms
./symfony propel:build-filters
./symfony propel:build-sql
./symfony propel:insert-sql

See those tasks’ help pages for more information.

To bypass confirmation prompts, you can pass the no-confirmation option:

./symfony propel:buil-all –no-confirmation

To build all classes but skip initializing the database, use the classes-only
option:

./symfony propel:build-all –classes-only

./symfony help propel:build-all-load

语法:
symfony propel:build-all-load [--application[="..."]] [--env="..."] [--connection="..."] [--no-confirmation] [--skip-forms|-F] [--classes-only|-C] [--phing-arg="..."] [--append] [--dir="..."]

别名: propel-build-all-load

选项:
–application The application name (default: 1)
–env The environment (default: dev)
–connection The connection name (default: propel)
–no-confirmation Do not ask for confirmation
–skip-forms (-F) Skip generating forms
–classes-only (-C) Do not initialize the database
–phing-arg Arbitrary phing argument (multiple values allowed)
–append Don’t delete current data in the database
–dir The directories to look for fixtures (multiple values allowed)

描述:
The propel:build-all-load task is a shortcut for two other tasks:

./symfony propel:build-all-load

The task is equivalent to:

./symfony propel:build-all
./symfony propel:data-load

See those tasks’ help pages for more information.

To bypass the confirmation, you can pass the no-confirmation
option:

./symfony propel:buil-all-load –no-confirmation

./symfony help propel:build-filters

语法:
symfony propel:build-filters [--connection="..."] [--model-dir-name="..."] [--filter-dir-name="..."] [--application[="..."]]

选项:
–connection The connection name (default: propel)
–model-dir-name The model dir name (default: model)
–filter-dir-name The filter form dir name (default: filter)
–application The application name (default: 1)

描述:
The propel:build-filters task creates filter form classes from the schema:

./symfony propel:build-filters

The task read the schema information in config/*schema.xml and/or
config/*schema.yml from the project and all installed plugins.

The task use the propel connection as defined in config/databases.yml.
You can use another connection by using the –connection option:

./symfony propel:build-filters –connection=”name”

The model filter form classes files are created in lib/filter.

This task never overrides custom classes in lib/filter.
It only replaces base classes generated in lib/filter/base.

./symfony help propel:build-forms

语法:
symfony propel:build-forms [--connection="..."] [--model-dir-name="..."] [--form-dir-name="..."] [--application[="..."]]

选项:
–connection The connection name (default: propel)
–model-dir-name The model dir name (default: model)
–form-dir-name The form dir name (default: form)
–application The application name (default: 1)

描述:
The propel:build-forms task creates form classes from the schema:

./symfony propel:build-forms

The task read the schema information in config/*schema.xml and/or
config/*schema.yml from the project and all installed plugins.

The task use the propel connection as defined in config/databases.yml.
You can use another connection by using the –connection option:

./symfony propel:build-forms –connection=”name”

The model form classes files are created in lib/form.

This task never overrides custom classes in lib/form.
It only replaces base classes generated in lib/form/base.

./symfony help propel:build-model

语法:
symfony propel:build-model [--phing-arg="..."]

别名: propel-build-model

选项:
–phing-arg Arbitrary phing argument (multiple values allowed)

描述:
The propel:build-model task creates model classes from the schema:

./symfony propel:build-model

The task read the schema information in config/*schema.xml and/or
config/*schema.yml from the project and all installed plugins.

You mix and match YML and XML schema files. The task will convert
YML ones to XML before calling the Propel task.

The model classes files are created in lib/model.

This task never overrides custom classes in lib/model.
It only replaces files in lib/model/om and lib/model/map.

./symfony help propel:build-schema

语法:
symfony propel:build-schema [--application[="..."]] [--env="..."] [--connection="..."] [--xml] [--phing-arg="..."]

别名: propel-build-schema

选项:
–application The application name (default: 1)
–env The environment (default: cli)
–connection The connection name
–xml Creates an XML schema instead of a YML one
–phing-arg Arbitrary phing argument (multiple values allowed)

描述:
The propel:build-schema task introspects a database to create a schema:

./symfony propel:build-schema

By default, the task creates a YML file, but you can also create a XML file:

./symfony –xml propel:build-schema

The XML format contains more information than the YML one.

./symfony help propel:build-sql

语法:
symfony propel:build-sql [--phing-arg="..."]

别名: propel-build-sql

选项:
–phing-arg Arbitrary phing argument (multiple values allowed)

描述:
The propel:build-sql task creates SQL statements for table creation:

./symfony propel:build-sql

The generated SQL is optimized for the database configured in config/propel.ini:

propel.database = mysql

./symfony help propel:data-dump

语法:
symfony propel:data-dump [--application[="..."]] [--env="..."] [--connection="..."] [--classes="..."] [target]

别名: propel-dump-data

参数:
target The target filename

选项:
–application The application name (default: 1)
–env The environement (default: cli)
–connection The connection name (default: propel)
–classes The class names to dump (separated by a colon)

描述:
The propel:data-dump task dumps database data:

./symfony propel:data-dump > data/fixtures/dump.yml

By default, the task outputs the data to the standard output,
but you can also pass a filename as a second argument:

./symfony propel:data-dump dump.yml

The task will dump data in data/fixtures/%target%
(data/fixtures/dump.yml in the example).

The dump file is in the YML format and can be re-imported by using
the propel:data-dump task.

By default, the task use the propel connection as defined in config/databases.yml.
You can use another connection by using the connection option:

./symfony propel:data-dump –connection=”name”

If you only want to dump some classes, use the classes option:

./symfony propel:data-dump –classes=”Article,Category”

If you want to use a specific database configuration from an application, you can use
the application option:

./symfony propel:data-dump –application=frontend

./symfony help propel:data-load

语法:
symfony propel:data-load [--application[="..."]] [--env="..."] [--append] [--connection="..."] [--dir="..."]

别名: propel-load-data

选项:
–application The application name (default: 1)
–env The environment (default: cli)
–append Don’t delete current data in the database
–connection The connection name (default: propel)
–dir The directories to look for fixtures (multiple values allowed)

描述:
The propel:data-load task loads data fixtures into the database:

./symfony propel:data-load

The task loads data from all the files found in data/fixtures/.

If you want to load data from other directories, you can use
the –dir option:

./symfony propel:data-load –dir=”data/fixtures” –dir=”data/data”

The task use the propel connection as defined in config/databases.yml.
You can use another connection by using the –connection option:

./symfony propel:data-load –connection=”name”

If you don’t want the task to remove existing data in the database,
use the –append option:

./symfony propel:data-load –append

If you want to use a specific database configuration from an application, you can use
the application option:

./symfony propel:data-load –application=frontend

./symfony help propel:generate-admin

语法:
symfony propel:generate-admin [--module="..."] [--theme="..."] [--singular="..."] [--plural="..."] [--env="..."] application route_or_model

参数:
application The application name
route_or_model The route name or the model class

选项:
–module The module name
–theme The theme name (default: admin)
–singular The singular name
–plural The plural name
–env The environment (default: dev)

描述:
The propel:generate-admin task generates a Propel admin module:

./symfony propel:generate-admin frontend Article

The task creates a module in the %frontend% application for the
%Article% model.

The task creates a route for you in the application routing.yml.

You can also generate a Propel admin module by passing a route name:

./symfony propel:generate-admin frontend article

The task creates a module in the %frontend% application for the
%article% route definition found in routing.yml.

For the filters and batch actions to work properly, you need to add
the wildcard option to the route:

article:
class: sfPropelRouteCollection
options:
model: Article
with_wildcard_routes: true

./symfony help propel:generate-module-for-route

语法:
symfony propel:generate-module-for-route [--theme="..."] [--non-verbose-templates] [--singular="..."] [--plural="..."] [--env="..."] application route

参数:
application The application name
route The route name

选项:
–theme The theme name (default: default)
–non-verbose-templates Generate non verbose templates
–singular The singular name
–plural The plural name
–env The environment (default: dev)

描述:
The propel:generate-module-for-route task generates a Propel module for a route definition:

./symfony propel:generate-module-for-route frontend article

The task creates a module in the %frontend% application for the
%article% route definition found in routing.yml.

./symfony help propel:graphviz

语法:
symfony propel:graphviz [--phing-arg="..."]

选项:
–phing-arg Arbitrary phing argument (multiple values allowed)

描述:
The propel:graphviz task creates a graphviz DOT
visualization for automatic graph drawing of object model:

./symfony propel:graphviz

./symfony help propel:init-admin

语法:
symfony propel:init-admin [--theme="..."] application module model

别名: propel-init-admin

参数:
application The application name
module The module name
model The model class name

选项:
–theme The theme name (default: default)

描述:
The propel:init-admin task generates a Propel admin module:

./symfony propel:init-admin frontend article Article

The task creates a %module% module in the %application% application
for the model class %model%.

The created module is an empty one that inherit its actions and templates from
a runtime generated module in %sf_app_cache_dir%/modules/auto%module%.

The generator can use a customized theme by using the –theme option:

./symfony propel:init-admin –theme=”custom” frontend article Article

./symfony help propel:insert-sql

语法:
symfony propel:insert-sql [--application[="..."]] [--env="..."] [--connection="..."] [--no-confirmation] [--phing-arg="..."]

别名: propel-insert-sql

选项:
–application The application name (default: 1)
–env The environment (default: cli)
–connection The connection name
–no-confirmation Do not ask for confirmation
–phing-arg Arbitrary phing argument (multiple values allowed)

描述:
The propel:insert-sql task creates database tables:

./symfony propel:insert-sql

The task connects to the database and executes all SQL statements
found in config/sql/*schema.sql files.

Before execution, the task will ask you to confirm the execution
as it deletes all data in your database.

To bypass the confirmation, you can pass the –no-confirmation
option:

./symfony propel:insert-sql –no-confirmation

The task read the database configuration from `databases.yml`.
You can use a specific application/environment by passing
an –application or –env option.

You can also use the –connection option if you want to
only load SQL statements for a given connection.

./symfony help propel:schema-to-xml

语法:
symfony propel:schema-to-xml

别名: propel-convert-yml-schema

描述:
The propel:schema-to-xml task converts YML schemas to XML:

./symfony propel:schema-to-xml

./symfony help propel:schema-to-yml

语法:
symfony propel:schema-to-yml

别名: propel-convert-xml-schema

描述:
The propel:schema-to-yml task converts XML schemas to YML:

./symfony propel:schema-to-yml

./symfony help test:all

语法:
symfony test:all

别名: test-all

描述:
The test:all task launches all unit and functional tests:

./symfony test:all

The task launches all tests found in test/.

If one or more test fail, you can try to fix the problem by launching
them by hand or with the test:unit and test:functional task.

./symfony help test:coverage

语法:
symfony test:coverage [--detailed] test_name lib_name

参数:
test_name A test file name or a test directory
lib_name A lib file name or a lib directory for wich you want to know the coverage

选项:
–detailed Output detailed information

描述:
The test:coverage task outputs the code coverage
given a test file or test directory
and a lib file or lib directory for which you want code
coverage:

./symfony test:coverage test/unit/model lib/model

To output the lines not covered, pass the –detailed option:

./symfony test:coverage –detailed test/unit/model lib/model

./symfony help test:functional

语法:
symfony test:functional application [controller1] … [nameN]

别名: test-functional

参数:
application The application name
controller The controller name

描述:
The test:functional task launches functional tests for a
given application:

./symfony test:functional frontend

The task launches all tests found in test/functional/%application%.

You can launch all functional tests for a specific controller by
giving a controller name:

./symfony test:functional frontend article

You can also launch all functional tests for several controllers:

./symfony test:functional frontend article comment ./symfony help test:unit

使用方法:
symfony test:unit [name1] … [nameN]

别名: test-unit

参数:
name The test name

描述:
The test:unit task launches unit tests:

./symfony test:unit

The task launches all tests found in test/unit.

You can launch unit tests for a specific name:

./symfony test:unit strtolower

You can also launch unit tests for several names:

./symfony test:unit strtolower strtoupper

./symfony -V

symfony version 1.2.4-DEV (/home/maker/www/symfony/lib)

2009-02-11

在symfony中使用FCKeditor上传图片附件

类归于: symfony — 标签:, , maker @ 13:31

在项目中我们经常需要使用富文本编辑器(什么是富文本?), 很多时候我们需要在我们编辑的内容中插入媒体文件, 比如图片,视频等等, 这就要涉及到富文本编辑器的上传功能, 本文主要讲解如何在symfony中使用FCKeditor和如何配置上传文件. 富文本编辑器我是更喜欢tinyMCE的, 但由于tinyMCE对上传的支持并不好, ImageManager等插件都是收费使用的, 所以不得不转向了FCKeditor. 首先我们进入FCKeditor的官方主页www.fckeditor.net下载源码包(http://www.fckeditor.net/download), 当前最新版本是2.6.4. 下载完源之后我们将压缩包解压到项目的web/js目录下, 修改要使用FCKeditory的app的config/settings.yml, 添加代码如下: all:
  .settings:
    rich_text_fck_js_dir: js/fckeditor
如果不使用上传功能, 那么现在FCKeditory就可以使用了. // app/editor/modules/test/templates/indexSuccess.php
<?php use_helper('Form');?>
<?php echo textarea_tag('name', 'default content', array ('rich'=>'fck','tool'=>'Basic','size' => '30x30')) ?>
访问http://test/editor_dev.php/test/index 效果如下 2009-02-11-124911_350x517_scrot 接下来开始配置上传图片附件, 这个功能是FCKeditor的功能, 所以要在FCKeditor进行配置. 首先编辑web/js/fckeditor/fckconfig.js var _FileBrowserLanguage    = 'php' ;    // asp | aspx | cfm | lasso | perl | php | py
var _QuickUploadLanguage    = 'php' ;    // asp | aspx | cfm | lasso | php
然后编辑 web/js/fckeditor/editor/filemanager/connectors/php/config.php $Config['Enabled'] = true;
$Config['UserFilesPath'] = '/uploads/';
web/js/fckeditor/editor/filemanager/connectors/php/config.php中还可以配置上传的其他相关参数, 比如文件类型等等. 现在上传功能就设置好了. // app/editor/modules/test/templates/indexSuccess.php
<?php use_helper('Form');?>
<?php echo textarea_tag('name', 'default content', array ('rich'=>'fck','tool'=>'Default','size' => '30x30')) ?>
访问页面打开插入图片功能, 画面如图. 2009-02-11-130238_622x608_scrot进入上传面板, 画面如图 2009-02-11-130249_627x619_scrot我们在本地选择图片, 进行上传, 如果使用的是symfony默认的配置, 应该会报错如下. 2009-02-11-130954_591x406_scrot 错误内容, Error creating folder “redirect:/index.php” (Can’t create redirect: directory) 经过一番研究, 最终发现这个错误是由apache的mod_rewrite造成的, 最终发现是web/.htaccess中的这一行导致的 RewriteRule ^([^.]+)$ $1.html      [QSA] 由于本人道行还不够, 最终也没有参透.htaccess中的代码都是什么作用, 但经过测试发现将这行去掉程序是可以正常运行的, 不过既然存在就一定有存在的意义, 所以使用了另一个方法来解决这个问题. 我们在web/uploads目录下再建立一个.htaccess, 内容如下 <IfModule mod_rewrite.c>
   RewriteEngine Off
</IfModule>
这样我们可以正常上传图片了. 2009-02-11-125927_617x749_scrot 在generator.yml中使用富文本编辑器 这里看一个例子, 出自《symfony权威指南》 generator:
  class: sfPropelAdminGenerator
  param:
    model_class: Comment
    theme: default
    edit:
      fields:
        ## 不显示表单控件, 只显示文本
        id: {type:plain}
        ## 表单控件不可编辑
        author:  {disabled=true}
        ## 富文本编辑器 (object_textarea_tag)
        content: {type: textarea_tag, params: rich=true css=user.css tinymce_options=width:330}
        ## 下拉列表 (object_select_tag)
        article_id: {params: include_custom=Choose an article}
如何在form中使用富文本编辑器? 《How to use FCKeditor》里提供了一个widget, 我没有用过, 但听同事说可能不好用, 所以自己写了一个. //lib/widget/sfWidgetFormFCK.class.php
<?
class sfWidgetFormFCK extends sfWidgetFormTextarea
{
   public function render($name, $value = null, $attributes = array(), $errors = array())
   {
     sfContext::getInstance()->getConfiguration()->loadHelpers('Form');
     return textarea_tag($name, $value, array_merge($attributes, array('rich' => 'fck', 'tool' => 'Default' )));
   }
}
//lib/form/ShopForm.class.php
<?php
class ShopForm extends BaseShopForm
{
   public function configure()
   {
     $this->setWidget('sid', new sfWidgetFormFCK());
   }
}
参考: 《Symfony TinyMCE & FCKeditor》 《How to use FCKeditor》 (本文完)

早前文章 »

WordPress 所驱动