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('[email protected]');
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


你可能感兴趣的:(EL)