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,内容如:
mysql mysql:host=localhost;dbname=test root
3,在/app下创建schema.xml,内容如:
使用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() . "
"; } } //按主键查询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