代码到Db连接的Apigility - 如何扩展Collection Class并获得必要的字段

时间:2014-11-19 18:17:27

标签: php api zend-framework2 apigility

我遵循本教程http://techportal.inviqa.com/2013/12/03/create-a-restful-api-with-apigility/并从代码连接中创建一个与数据库连接的API。

为实体提供以上方法,让我有可能选择在JSON中返回哪个字段

public function getArrayCopy()
{
    return array(
        'id' => $this->id,
        'cat_id' => $this->cat_id,
        'name' => $this->name,
        'url' => $this->url,
    );
}

public function exchangeArray(array $array)
{
    $this->id = $array['id'];
    $this->cat_id = $array['cat_id'];
    $this->name = $array['name'];
    $this->url = $array['url'];
}

现在我不会对集合做同样的事情(只返回db中所有表字段的一些字段)......但是不能理解在哪里。

我有调用Mapper的资源

return $this->mapper->fetchAll();

并在mapper中

public function fetchAll()
{
    $select = new Select('eye_medic');
    $paginatorAdapter = new DbSelect($select, $this->adapter);
    $collection = new MedicCollection($paginatorAdapter);
    return $collection;
}

我如何编辑它来创建我自己的集合数组并且只能由我的字段选择?

示例实体响应(非常好):

[id] => 3
[cat_id] => 1
[name] => Abbadini Dr. Pasqualino Odontoiatra 
[url] => abbadini-dr-pasqualino-odontoiatra
[_links] => stdClass Object ...

收集的响应是完整的数据库字段(不好,我只想要4个字段)

[15] => stdClass Object
 (
   [id] => 19
   [user_id] => 0
   [country_id] => 105
   [region_id] => 13
   [province_id] => 68
   [city_id] => 5889
   [url] => amicarelli-dr-alfonso-studio-dentistico
   [name] => Amicarelli Dr. Alfonso Studio Dentistico 
   [description] => 
   [poster] => 
   [cat_id] => 1
   [status] => LIVE
   [publish_ts] => 2014-11-13 18:46:47
   [edited_ts] => 
   [_links] => stdClass Object ...

提前致谢

0 个答案:

没有答案