1

问题是我从数据库中只得到 1 张图像。我想要的是将所有图像放入不同的数组中,以便我可以在每个项目中查看它们。

索引控制器.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){

**项目从这里开始**

      [“身份证”]=>
      字符串(1)“1”
      [“名称”]=>
      字符串(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;

   }
}

?>

在项目表中

<?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;
 }

}

在项目图像中

<?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 回答