dpcwz210393 2014-10-19 17:15
浏览 7

Zf2实体加入

Can you help me? I can't understand how to join 2 and more tables, each table has the entity. Whether it is possible to use as in doctrine? I don't want to use doctrine.

How to join entities and use in views?

class User
{
    protected $id;
    protected $email;
    protected $password;

    public function getId()
    {
        return $this->id;
    }
    public function setId($value)
    {
        $this->id = $value;
    }
    public function setEmail($value)
    {
        $this->email = $value;
    }
    public function getEmail()
    {
        return $this->email;
    }
    public function setPassword($value)
    {
        $this->password = $value;
    }
    public function getPassword()
    {
        return $this->password;
    }
}

class Info
{
    protected $id;
    protected $lastname;
    protected $firstname;

    public function getId()
    {
        return $this->id;
    }
    public function setId($value)
    {
        $this->id = $value;
    }
    public function setLastname($value)
    {
        $this->lastname = $value;
    }
    public function getLastname()
    {
        return $this->lastname;
    }
    public function setFirstname($value)
    {
        $this->firstname = $value;
    }
    public function getFirstname()
    {
        return $this->firstname;
    }
}
  • 写回答

1条回答 默认 最新

  • dongmen5867 2014-10-20 07:50
    关注
    class User
    {
        ...
        protected $info;
    
        ...
        public function readInfo()
        {
            return $this->info;
        }
    
        public function writeInfo(Info $entity)
        {
            $this->info = $entity;
            return $this;
        }
    }
    
    class ModelUser
    {
        public function get()
        {
            $query = 'query for user with info';
            $adapter = Zend\Db\TableGateway\Feature\GlobalAdapterFeature::getStaticAdapter();
            $result = $adapter->query($query, \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
    
            /* Or use tableGateway */ 
    
            /* Class methods hydrator */
            $hydrator = new \Zend\Stdlib\Hydrator\ClassMethods;
            /* Hydrate User entity from result */
            $userEntity = $hydrator->hydrate($result->toArray(), new User);
            /* Hydrate Info entity from result */
            $infoEntity = $hydrator->hydrate($result->toArray(), new Info);
            /* Write Info entity to User entity */
            $userEntity->writeInfo($infoEntity);
    
            return $userEntity;
        }
    }
    
    class UserController
    {
        public function indexAction()
        {
            $model = new ModelUser();
            $userEntity = $model->get();
    
            return array(
                'user' => $userEntity
            );
        }
     }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数