Model operation of the thinkphp

Source: Internet
Author: User

Several ways to call the model:

Use Think\controller;

Use App\index\model\app;
Use Think\loader;

Class Index extends Controller
{
Public Function Index ()
{
$res = App::get (2); "The first Kind"
$app = new app; "Second Type"
$app = Loader::model (' app '); "The Third Kind"
$res = $app->get (4);
$res = $res->toarray ();
Dump ($res);
}
}

Ways to query data using models
"Get a Record" get method and Find method
Public Function Index ()
{
$res = App::get (function ($query) {
$query->where (' id ', ' eq ', 3)->field (' Name, status ');
});

$res = App::where (' name ', ' eq ', ' ipad ')->field (' Name, key ')->find ();
$res = $res->toarray ();
Dump ($res);

}

"Get more than one data" "All"

Public Function Index ()
{

$res (' =app::all '); "String Form"
$res = App::all ([2,3,4]);//"Array form"
$res = App::all (function ($query) {
$query->where (' id ', ' < ', 5)->field (' Name,key,status ');
});
foreach ($res as $val) {
$res = $val->toarray ();
Dump ($res);
}

}

Where method gets directly

Public Function Index ()
{
$res = app::where (' id ', ' < ', 5)->select ();
foreach ($res as $val) {
$res = $val->toarray ();
Dump ($res);
// }
$res = app::where (' id ', 1)->value (' name ');
Dump ($res);

}
"Get single Field One"
Public Function Index ()
{
$res = app::where (' id ', 1)->value (' name ');
Dump ($res);

}

"Get a single field multi-bar"
Public Function Index ()
{
$res = App::column (' name ', ' key ');
Dump ($res);

}

Using models to add data

"Create Method"
Public Function Index ()
{
$res = App::create ([
' Name ' = ' Samsung ',
' Is_encryption ' =>1,
' Key ' = ' SX ',
' Status ' =>1
// ]);
When adding a field that does not have a database, it will error, and will not be added, plus the second argument, true
$res = App::create ([
' Name ' = ' Apple Mac ',
' Is_encryption ' =>1,
' Key ' = ' Mac ',
' Status ' =>1,
' Momo ' = ' 123 '
],true);

Only add name and status two fields are allowed
$res = App::create ([
' Name ' = ' MacBook ',
' Is_encryption ' =>1,
' Key ' = ' book ',
' Status ' =>1,
' Momo ' = ' 123 '
],[' name ', ' status ');
Dump ($res);
}

"Save Method"

Public Function Index ()
{
$appModel = new App;
$res = $appModel->save ([
' Name ' = ' phone ',
' Is_encryption ' =>1,
' Key ' = ' SJ ',
' Status ' =>1,
' Demo ' =>111
// ]);
Filtering Non-database fields
$appModel = new App;
$res = $appModel->allowfield (True)->save ([
' Name ' = ' phone ',
' Is_encryption ' =>1,
' Key ' = ' SJ ',
' Status ' =>1,
' Demo ' =>1111
// ]);

$appModel = new App;
$res = $appModel->allowfield ([' Name ', ' Is_encryption '])->save ([
' Name ' = ' phone ',
' Is_encryption ' =>1,
' Key ' = ' SJ ',
' Status ' =>1,
' Demo ' =>1111
]);
Dump ($res);
}

"Add more than one data"
Public Function Index ()
{
$appModel = new App;
$res = $appModel->saveall ([
[' Name ' = ' book '],
[' Name ' = ' cat ']
]);
Dump ($res);
}

Updating data using Models

"Update method Updates"
Public Function Index ()
{
$res = App::update ([
' ID ' =>8,
' Is_encryption ' =>1
// ]);

If there is no condition, update () plus the second parameter
$res = App::update ([
' Is_encryption ' =>1,
],[' id ' =>12]);

also supports closure functions
$res = App::update ([
' Is_encryption ' =>1
],function ($quest) {
$quest->where ("id", ' = ', 13);
});
Dump ($res);
}


"Where Condition Update" "Recommended"
Public Function Index ()
{
$res = app::where (' id ', 5)->update ([
' Key ' = ' HW '
// ]);
Dump ($res);
}

"Save method Update" "Recommended"
Public Function Index ()
{

$appModel = new App;
$res = $appModel->save ([
' Key ' = ' MB ',
],[' id ' =>8]);

$appModel = new App;
$res = $appModel->save ([
' Key ' = ' sj11 ',
],function ($quest) {
$quest->where (' id ', 11);
});
Dump ($res);
}

"SaveAll Update"
Public Function Index ()
{
$appModel = new App;


$res = $appModel->saveall ([
[' ID ' =>12, ' key ' = ' BK '],
[' ID ' =>13, ' key ' = ' cat ']
]);
Dump ($res);
}

Using Models to delete data

[Destroy Method]
Public Function Index ()
{
$res = App::d Estroy ([' ID ' =>11]);
Dump ($res);

$res = App::d Estroy (function ($quest) {
$quest->where (' id ', 13);
// });

Dump ($res);
}


[Delete method]
Public Function Index ()
{
$appModel = App::get (12);
$res = $appModel->delete ();

$res = app::where (' id ', ' > ', 7)->delete ();
Dump ($res);
}

Model operation of the thinkphp

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.