diff --git a/README.md b/README.md index 6347f223ed5c3329205a78cc724a1b5bdac007e0..dd73d31620921dac5f879bbb06c2949ef08eae37 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,11 @@ ## 环境变量 XDEBUG_CONFIG="idekey=VSCODE" XDEBUG=vscode +## 快捷键 +ctrl/command + shift + k //删除当前行 ## cli php think run - +php think make:model composer create-project topthink/think=~6.0 demotp6 composer create-project topthink/think=~8.0 demotp8 composer create-project topthink/think=~5 demotp5 diff --git a/demotp6/app/model/Article.php b/demotp6/app/model/Article.php index 4384e8e1ad7be39b099ccfcae7c8accda758ad51..d223ccfed0cfaddd675c9e4bcbc869b6aea20e33 100644 --- a/demotp6/app/model/Article.php +++ b/demotp6/app/model/Article.php @@ -7,8 +7,17 @@ use think\Model; /** * @mixin \think\Model + * @property string $title */ class Article extends Model { - // + public const Content = 'content'; + public function content() + { + return $this->hasOne(ArticleContent::class); + } + public function user() + { + return $this->belongsTo(User::class); + } } diff --git a/demotp6/app/model/ArticleContent.php b/demotp6/app/model/ArticleContent.php new file mode 100644 index 0000000000000000000000000000000000000000..8406cd79e8bd433257454ea00e2e3bade59c6dbe --- /dev/null +++ b/demotp6/app/model/ArticleContent.php @@ -0,0 +1,19 @@ +belongsTo(Article::class)->bind(["aaa"=>'content']); + } +} diff --git a/demotp6/app/model/User.php b/demotp6/app/model/User.php index 0ffa858bf99fefb59e77539245ce6c0484ae86a2..da102adf160214520affc4d4e3839b9827bec374 100644 --- a/demotp6/app/model/User.php +++ b/demotp6/app/model/User.php @@ -24,5 +24,10 @@ enum UserState:int{ */ final class User extends Model { + public const Articles = 'articles'; + public function articles() + { + return $this->hasMany(Article::class); + } } diff --git a/demotp6/tests/ModelTest.php b/demotp6/tests/ModelTest.php index 837dc117c0b8ec1f6d79445fca9e342195ed25fc..95c9c11c047e66f2ba58ea5e50a1582fbd26adcd 100644 --- a/demotp6/tests/ModelTest.php +++ b/demotp6/tests/ModelTest.php @@ -1,5 +1,7 @@ article; + } + public function testhasMany() + { + // $uu = User::find(13); + // foreach ($uu->articles()->where("title","aaaaa")->select() as $item) { + // echo $item->title,PHP_EOL; + // } + + // print $uu->articles; + + // $user=new User(); + // $user->username='aaaa'; + // $user->save(); + // $aa=new Article(); + // $aa->title="aaaaa"; + // $bb=new Article(); + // $bb->title='bbbbb'; + // $user->articles()->saveAll([$aa,$bb]); + $wh=Article::where('user_id','>',0); + $uu = User::with([User::Articles])->hasWhere(User::Articles,$wh)->select(); + + print $uu; + + + } + public function testArticleContent() + { + $aa = Article::find(3); + $cc = new ArticleContent(); + $cc->content = 'aaaaa'; + // $aa->content->content="cccccc"; + // $aa->content->save(); + // $aa->save(); + // $cc->save(); + $aa->content()->save($cc); + // $aa->save(); + } + public function testArticle() + { + // $aaa=Article::withJoin("content")->select(); + // foreach ($aaa as $item) { + // print $item; + + // } + // $aa=new Article(); + // $aa->title="bbb"; + // $aa->save(); + + $aa = Article::with("content")->find(3); + $aa->content->delete(); + $aa->delete(); + // print $aa->content->content; + // $aa->content->content = 'cccc'; + // $aa->content->save(); + // echo $aa->aaa,"???"; + // print $aa->content; + } + public function testCreateArticle() + { + $aa = new Article(); + $aa->title = "ccc"; + $cc=new ArticleContent(); + $cc->content='vvvvvv'; + $aa->content=$cc; + $aa->together(['content'])->save(); + // $aa->save(); + } public function testByName() { - $user=User::getByUsername('aaa'); + $user = User::getByUsername('aaa'); print $user; } function testValueColumn() { // $res=User::where("id",">",0)->value("email"); - $res=User::where("id",">",0)->column("username"); + $res = User::where("id", ">", 0)->column("username"); print $res; } function testCreate() @@ -38,7 +110,6 @@ class ModelTest extends TestCase $user->password = 'wqsqwsqsw'; $user->status = UserState::Inactive->value; $user->save(); - } function testSelect() {