cakePHP 根據ID讀取數據庫

Apache,MySQL start

Database:mydata

Table:people

 

1.Model

PeopleTable.php

C:\xampp\htdocs\mycakeapp\src\Model\Table

 

<?php
namespace App\Model\Table;

use Cake\ORM\Table;
class PeopleTable extends Table{

    public function initialize(array $config){
        
        $this->setTable('people');
        $this->setDisplayField('mail');
        $this->setPrimaryKey('id');
    }
}
?>

 

2.View

index.ctp

C:\xampp\htdocs\mycakeapp\src\Template\People

 

<p>This is People table records.</p>
<pre>
<?php print_r($data->toArray()); ?>
</pre>

3.Controller

PeopleController.php

C:\xampp\htdocs\mycakeapp\src\Controller

<?php
namespace App\Controller;

use App\Controller\AppController;

class PeopleController extends AppController{
    
    public function index() {
        $id = $this->request->query['id'];
        $data = $this->People->get($id);
        $this->set('data', $data);
    }
}
?>

4.

http://localhost/mycakeapp/people/?id=1

http://localhost/mycakeapp/people/?id=2

相關文章
相關標籤/搜索