1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

(入門) CakePHP2 でdatabase接続

Last updated at Posted at 2016-12-01

手順

git clone https://github.com/tukiyo/docker-php5.6-apache
cd docker-php5.6-apache

sh files/get_cakephp.sh
chmod -R 777 app/tmp
docker-compose up
docker-compose.yml
web:
  image: tukiyo3/php5.6-apache
  volumes:
      - ./files/apache.conf:/etc/apache2/sites-available/000-default.conf
      - ./files/php.ini:/usr/local/etc/php/php.ini
      - ./cakephp/:/var/www/html
      - ./logs/:/var/log/apache2/
  ports:
      - "8080:80"
  #restart: always

aa.png

以下CakePHP2のメモ

初期設定

  • app/Config/core.php
    • Security.salt
    • Security.cipherSeed
  • app/Config/database.php
  • app/Config/routes.php

クラスの追加

  • app/Model/Phone.php (db)
  • app/Controller/PhonesController.php ($this->Phone->findとか)
  • app/View/Phones/index.ctp (view)
app/Model/Phone.php
class Phone extends AppModel {
    var $useTable = 'phone';
    public $validate = array(
        'number' => array(
            'rule' => 'notEmpty'
        ),
    );
}
app/Controller/PhonesController.php
class PhonesController extends AppController {
    public $helpers = array('Html', 'Form');
    public function index() {
        $this->set('phones', $this->Phone->find('all'));
    }
}
app/View/Phones/index.ctp
<table>
    <tr>
        <th>Id</th>
        <th>Number</th>
        <th>Created</th>
    </tr>
 
    <?php foreach ($phones as $p): ?>
        <tr>
            <td><?php echo $p['Phone']['id']; ?></td>
            <td><?php echo $p['Phone']['number']; ?></td>
            <td><?php echo $p['Phone']['date']; ?></td>
        </tr>
    <?php endforeach; ?>
    <?php unset($g); ?>
</table>
app/Config/routes.php
- Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
+ Router::connect('/', array('controller' => 'phones', 'action' => 'index'));

便利機能

  • alt + shift + f : ソース整形

  • table名を複数形ではなく任意のものを指定する

app/Model/Phone.php(例)
class Phone extends AppModel {
    var $useTable = 'phone';
}
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?