Propel使用简介

抽空整理了一下Propel的使用步骤,假设应用程序所在目录:/app


1,在/app下创建build.properties,内容:
# Database driver
propel.database = mysql
propel.database.url = mysql:host=localhost;dbname=test
propel.database.user = root
propel.database.password =


# Project name
propel.project = test


2,在/app下创建runtime-conf.xml,内容如:

<?xml version="1.0" encoding="UTF-8"?>
<config>
<!--
<log>
<type>syslog</type>
<name>propel.log</name>
<ident>propel-bookstore</ident>
<level>7</level>
</log>
-->

<propel>
<datasources default="bookstore">
<datasource id="bookstore">
<adapter>mysql</adapter> <!-- sqlite, mysql, mssql, oracle, or pgsql -->
<connection>
<dsn>mysql:host=localhost;dbname=test</dsn>
<user>root</user>
<password></password>
</connection>
</datasource>
</datasources>
</propel>
</config>


3,在/app下创建schema.xml,内容如:
<?xml version="1.0" encoding="UTF-8"?>
<database name="bookstore" defaultIdMethod="native">
<table name="book" phpName="Book">
<column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true"/>
<column name="title" type="varchar" size="255" required="true" />
<column name="isbn" type="varchar" size="24" required="true" phpName="ISBN"/>
<column name="publisher_id" type="integer" required="true"/>
<column name="author_id" type="integer" required="true"/>
<foreign-key foreignTable="publisher" phpName="Publisher" refPhpName="Book">
<reference local="publisher_id" foreign="id"/>
</foreign-key>
<foreign-key foreignTable="author">
<reference local="author_id" foreign="id"/>
</foreign-key>

<validator column="isbn">
<rule name="notMatch" value="/[^\d-]+/" message="Please enter a valid ISBN" />
</validator>
</table>
<table name="author" phpName="Author">
<column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true"/>
<column name="first_name" type="varchar" size="128" required="true"/>
<column name="last_name" type="varchar" size="128" required="true"/>
<column name="email" type="varchar" size="128" required="true"/>

<validator column="first_name">
<rule name="minLength" value="4" message="first_name must be at least ${value} characters !" />
<rule name="type" value="string" message="Username must be a string" />
</validator>
<validator column="email">
<rule name="match" value="/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/" message="Please enter a valid email address." />
</validator>
</table>
<table name="publisher" phpName="Publisher">
<column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true" />
<column name="name" type="varchar" size="128" required="true" />
</table>
</database>


使用xml描述应用程序的数据结构

4,在/app下执行命令:
/path/to/Propel/generator/bin/propel-gen
根据上面的schema.xml会在/app/build/下面生成php配置文件,数据库sql,及各个表的Model类文件,1-3步的配置文件修改后需要重新执行第4步,清不要手动修改/app/build/classes/appname/om和mp下面的类文件,每次执行propel-gen命令会重新生成这些文件,可以把自己的逻辑写到/app/build/classes/appname/下面的类文件中,这些文件不会重新生成。

5,执行命令:
/path/to/Propel/generator/bin/propel-gen insert-sql
创建数据库结构

6,使用数据库模型:
include "/path/to/Propel/runtime/lib/Propel.php";
Propel::init("/app/build/conf/test-conf.php");

// Add the generated 'classes' directory to the include path
set_include_path("/app/build/classes" . PATH_SEPARATOR . get_include_path());

//增加一个author
$author = new Author();
$author->setFirstName('Jane');
$author->setLastName('Austen');
$author->setEmail('Austen@aa.com');
if($author->validate()){
$author->save();
}
else{
foreach ($author->getValidationFailures() as $failure) {
echo $failure->getMessage() . "<BR>";
}
}

//按主键查询author
$q = new AuthorQuery();
$firstAuthor = $q->findPK(1);
print_rr($firstAuthor->toArray());

//按主键查询多个author
$selectedAuthors = AuthorQuery::create()->findPKs(array(1,2,3));
print_rr($selectedAuthors->toArray());


更多方法可以查看官方文档
http://www.propelorm.org/documentation/03-basic-crud.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值