1

問題は、データベースから画像を 1 つしか取得できないことです。私が欲しいのは、プロジェクトごとに表示できるように、すべての画像を別の配列に入れることです。

IndexController.php

public function projectsAction() {

  $projects = $this->getProjectsTable()->fetchAll();

  return new ViewModel(array(
  'projects' => $projects,
  ));
 }

public function getProjectsTable() {
 if (!$this->projectsTable) {
     $sm = $this->getServiceLocator();
     $this->projectsTable = $sm->get('Application\Model\ProjectsTable');
    }
  return $this->projectsTable;
}

ProjectsTable.php

public function fetchAll() {
 $select = new Select();

  $select->from('projects', array('projects.*'))
      ->join('project_images', 'projects.id=project_images.project_id', array('*'))
      ->where('projects.id=project_images.project_id');

  //echo $select->getSqlString();
  $resultSet = $this->selectWith($select);
  $resultSet->buffer();
  return $resultSet;
 }

出力:

配列(17) {

**プロジェクトはここから始まります**

      ["id"]=>
      文字列(1) "1"
      ["名前"]=>
      string(55) "名前"
      ["国"]=>
      文字列(6)「国」
      ["location_country"]=>
      文字列(13)「場所」

**プロジェクトの画像はここから始まります**

    ["project_id"]=>
      文字列(1) "1"
      ["画像名"]=>
      文字列(29) "1_1370202251.808419328090.png"
      ["日付"]=>
      文字列(22) "2013-06-02 07:44:11 PM"
    }
4

1 に答える 1

1

私はこれを行うことでそれを愛しました:

コントローラー内

public function projectsAction() {

  $projects = $this->getProjectsTable()->fetchAll();

  $i = 0;
  foreach ($projects as $project) {
   $imgs = array();
   $pros[] = $project;
   $images = $this->getProjectImagesTable()->fetchAll($project->id);
   foreach ($images as $img) {
    $imgs[] = $img;
    $pros[$i]->images = $imgs;
   }
   $i++;
  }

  return new ViewModel(array(
      'projects' => $pros
  ));
 }

プロジェクト内

<?php
//IN MODEL FOLDER
namespace Application\Model;

class Projects 
{
  public function exchangeArray($data)
  {
    $this->id     = (isset($data['id'])) ? $data['id'] : null;
    $this->name = (isset($data['name'])) ? $data['name'] : null;
    $this->country = (isset($data['country'])) ? $data['country'] : null;

   }
}

?>

ProjectsTable 内

<?php
 //IN MODEL FOLDER
namespace Application\Model;

use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Sql\Select;
use Zend\Db\Sql\Sql;

class ProjectsTable extends AbstractTableGateway {

 protected $table = 'projects';

 public function __construct(Adapter $adapter) {
  $this->adapter = $adapter;
  $this->resultSetPrototype = new ResultSet();
  $this->resultSetPrototype->setArrayObjectPrototype(new Projects());
  $this->initialize();
 }

 public function fetchAll() {
  $select = new Select();

  $select->from('projects', array('projects.*'));

  //echo $select->getSqlString();
  $resultSet = $this->selectWith($select);
  $resultSet->buffer();
  return $resultSet;
 }

}

ProjectImages 内

<?php
//IN MODEL FOLDER

namespace Application\Model;

class ProjectImages {

 public $id, $project_id, $image_name, $date;

 public function exchangeArray($data) {
  $this->id = (isset($data['id'])) ? $data['id'] : null;
  $this->project_id = (isset($data['project_id'])) ? $data['project_id'] : null;
  $this->image_path = (isset($data['image_path'])) ? $data['image_path'] : null;
  $this->date = (isset($data['date'])) ? $data['date'] : null;
 }

}

ProjectImagesTable 内

<?php

namespace Application\Model;

use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Sql\Select;

class ProjectImagesTable extends AbstractTableGateway {

 protected $table = 'project_images';

 public function __construct(Adapter $adapter) {
  $this->adapter = $adapter;
  $this->resultSetPrototype = new ResultSet();
  $this->resultSetPrototype->setArrayObjectPrototype(new ProjectImages());
  $this->initialize();
 }

 public function fetchAll($id) {
  $select = new Select();

  $select->from('project_images', array())
          ->where(array('project_id' => $id));

  //echo $select->getSqlString();
  $resultSet = $this->selectWith($select);
  $resultSet->buffer();
  return $resultSet;
 }

}
于 2013-06-09T11:23:05.537 に答える