[php] PHP Zend 프레임워크에서 데이터베이스 연동
  1. 설정 파일 생성: 우선 config/autoload/global.php 파일에 데이터베이스 연결 정보를 추가합니다.
return [
    'db' => [
        'driver' => 'Pdo',
        'dsn'    => 'mysql:dbname=testdb;host=127.0.0.1',
        'username' => 'username',
        'password' => 'password',
    ],
];
  1. 모델 생성: 데이터베이스와 상호작용하는 모델을 생성합니다. 예를 들어, Album 모델을 생성할 수 있습니다.
namespace Application\Model;

use Zend\Db\TableGateway\TableGateway;

class AlbumTable
{
    protected $tableGateway;

    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
    }

    public function fetchAll()
    {
        $resultSet = $this->tableGateway->select();
        return $resultSet;
    }

    // 다른 CRUD 메서드 추가
}
  1. 컨트롤러에서 사용: 컨트롤러에서 모델을 이용하여 데이터베이스와 상호작용합니다.
namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class AlbumController extends AbstractActionController
{
    private $albumTable;

    public function __construct(AlbumTable $albumTable)
    {
        $this->albumTable = $albumTable;
    }

    public function indexAction()
    {
        return new ViewModel([
            'albums' => $this->albumTable->fetchAll(),
        ]);
    }
    
    // 다른 액션 메서드 추가
}

위에서는 Zend 프레임워크를 사용하여 PHP 애플리케이션에서 데이터베이스를 연동하는 방법을 살펴보았습니다. Zend 프레임워크 문서에서 더 많은 정보를 얻을 수 있습니다.