《PHP实例:Laravel框架学习笔记(二)项目实战之模型(Models)》要点:
本文介绍了PHP实例:Laravel框架学习笔记(二)项目实战之模型(Models),希望对您有用。如果有疑问,可以联系我们。
在开发mvc项目时,models都是第一步.PHP实例
下面就从建模开端.PHP实例
1.实体关系图,PHP实例
由于不知道php有什么好的建模对象,这里我用的vs ado.net实体模型数据建模PHP实例
PHP实例
下面开端laravel编码,编码之前首先得配置数据库连接,在app/config/database.php文件PHP实例
'mysql' => array( 'driver' => 'mysql', 'read' => array( 'host' => '127.0.0.1:3306', ), 'write' => array( 'host' => '127.0.0.1:3306' ), 'database' => 'test', 'username' => 'root', 'password' => 'root', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ),
配置好之后,必要用到artisan工具,这是一个php命令工具在laravel目录中PHP实例
首先必要要通过artisan建立一个迁移 migrate ,这点和asp.net mvc几乎是一模一样PHP实例
在laravel目录中 shfit+右键打开命令窗口 输入artisan migrate:make create_XXXX会在app/database/migrations文件下生成一个带光阴戳前缀的迁移文件PHP实例
代码:PHP实例
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTablenameTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
}
}
看到这里有entityframework 迁移经验的根本上发现这是出奇的相似啊.PHP实例
接下来便是创建我们的实体结构,laravel 的结构生成器可以参考http://v4.golaravel.com/docs/4.1/schemaPHP实例
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTablenameTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function(Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id');
$table->string('title');
$table->string('read_more');
$table->text('content');
$table->unsignedInteger('comment_count');
$table->timestamps();
});
Schema::create('comments', function(Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('post_id');
$table->string('commenter');
$table->string('email');
$table->text('comment');
$table->boolean('approved');
$table->timestamps();
});
Schema::table('users', function (Blueprint $table) {
$table->create();
$table->increments('id');
$table->string('username');
$table->string('password');
$table->string('email');
$table->string('remember_token', 100)->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('posts');
Schema::drop('comments');
Schema::drop('users');
}
}
继续在上面的命令窗口输入php artisan migrate 将执行迁移PHP实例
更多迁移相关知识:http://v4.golaravel.com/docs/4.1/migrationsPHP实例
先写到这里来日诰日继续PHP实例
欢迎参与《PHP实例:Laravel框架学习笔记(二)项目实战之模型(Models)》讨论,分享您的想法,维易PHP学院为您提供专业教程。