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()
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥20 win11无法启动 持续蓝屏且系统还原失败,无法开启系统保护
  • ¥15 哪个tomcat中startup一直一闪而过 找不出问题
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码