dongyou8087 2015-03-03 12:38
浏览 47
已采纳

如何使用PHP中的Left Joined数据将数据从Mapper保存到Entity

I have simple custom made Entity and Mapper class. Mapper is used to get data from Database, and Entity is a collection object with variables, getter/setter methods and relationships between Entities.

Model\Entity\Category

class Category extends Entity
{
    private $id, $name, $link;

    // belongs to
    private $category;

    // has many
    private $categories;

    // relations - belongs_to, has_many, has_one...
    private $belongs_to = array ('category');
    private $has_many = array ('categories');

    // here goes getters, setters for id, name, category, categories...
}

Model\Mapper\Category

class CategoryMapper extends Mapper
{
    public function loadTopNavigation()
    {
        $categories = array();

        $data = $this->db->query('SELECT category.name, category.link, category_master.name AS category_name, category_master.link AS category_link
            FROM category
            LEFT JOIN category AS category_master
                ON category_master.id=category.category_id')
            ->get();

        foreach ($data as $row) {
            $categories[] = new Category($row);
        }

        return $categories;
    }
}

Usage

$categoryMapper = new CategoryMapper();
$categories = $categoryMapper->loadTopNavigation();
foreach ($categories as $category) {
    echo '<li>' . $category->getName() . ' - ' . $category->getCategory()->getName() . </li>;
}

Obtained array from database:

  • name
  • link
  • category_name
  • category_link

Expected array:

  • name
  • link
  • category => array (name, link)

I can get category array if I have 2 queries - first I get category and then second query would be getting master category based on child id and save it to $categories[]['category'];

Is there any suggestions what to do ? I could create method in Mapper class to check all category_ rows and set them in special category = array () or to do that in Entity class... Or is there any other way ?

  • 写回答

1条回答 默认 最新

  • dpnfjx755573 2015-03-04 10:27
    关注

    I figured it out! Like I said in post above, I created a method inside Mapper class which runs through fetched rows from database and looks if it contains category_ string. If it does, then saves it in in special $category array.

    First I wanted to put this method inside Entity class, but what if I switch SQL Mapper to XML or JSON or something like this with different key names, then again I will have problems with saving values properly.

    It looks something like this:

    foreach ($data as $key => $value) {
        $class = strstr($key, '_', true);
        if (in_array($class, $this->belongs_to)) {
            // adding $key and $value to newly created array $category = array()
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多