Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Autoría | Ultima modificación | Ver Log |

<?php

declare(strict_types=1);

namespace LeadersLinked\Mapper;

use LeadersLinked\Model\UserEducation;
use LeadersLinked\Mapper\Common\MapperCommon;
use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Log\LoggerInterface;
use LeadersLinked\Hydrator\ObjectPropertyHydrator;


class UserEducationMapper extends MapperCommon
{
    const _TABLE = 'tbl_user_educations';

    
    /**
     *
     * @var UserEducationMapper
     */
    private static $_instance;
    
    /**
     *
     * @param AdapterInterface $adapter
     */
    private function __construct($adapter)
    {
        parent::__construct($adapter);
    }
    
    /**
     *
     * @param AdapterInterface $adapter
     * @return \LeadersLinked\Mapper\UserEducationMapper
     */
    public static function getInstance($adapter)
    {
        if(self::$_instance == null) {
            self::$_instance = new UserEducationMapper($adapter);
        }
        return self::$_instance;
    }
    
    /**
     * 
     * @param int $id
     * @return UserEducation
     */
    public function fetchOne($id)
    {
        $prototype = new UserEducation;
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('id', $id);
        
        return $this->executeFetchOneObject($select, $prototype);
    }
    
    
    /**
     *
     * @param string $uuid
     * @return UserEducation
     */
    public function fetchOneByUuid($uuid)
    {
        $prototype = new UserEducation;
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('uuid', $uuid);
        
        return $this->executeFetchOneObject($select, $prototype);
    }
    
    
    /**
     *
     * @param int $user_id
     * @return UserEducation[]
     */
    public function fetchAllByUserId($user_id)
    {
        $prototype = new UserEducation;
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('user_id', $user_id);
        
        return $this->executeFetchAllObject($select, $prototype);
    }
    
    /**
     *
     * @param int $user_profile_id
     * @return UserEducation[]
     */
    public function fetchAllByUserProfileId($user_profile_id)
    {
        $prototype = new UserEducation;
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('user_profile_id', $user_profile_id);
        $select->order(['from_year DESC',  'to_year DESC']);
        
        return $this->executeFetchAllObject($select, $prototype);
    }
    
    /**
     * 
     * @param UserEducation $userEducation
     * @return int
     */
    public function insert($userEducation)
    {
        $hydrator = new ObjectPropertyHydrator();
        $values = $hydrator->extract($userEducation);
        $values = $this->removeEmpty($values);
        
        $insert = $this->sql->insert(self::_TABLE);   
        $insert->values($values);
        
        $result = $this->executeInsert($insert);
        if($result) {
            $userEducation->id = $this->lastInsertId;
        }
        
        return $result;
    }
    
    
    /**
     *
     * @param UserEducation $userEducation
     * @return int
     */
    public function update($userEducation)
    {
        $hydrator = new ObjectPropertyHydrator();
        $values = $hydrator->extract($userEducation);
        $values = $this->removeEmpty($values);
        
        $update = $this->sql->update(self::_TABLE);
        $update->set($values);
        $update->where->equalTo('id', $userEducation->id);
        
        return $this->executeUpdate($update);
    }
    
    
    /**
     *
     * @param UserEducation $userEducation
     * @return boolean
     */
    public function delete($userEducation)
    {
        $delete = $this->sql->delete(self::_TABLE);
        $delete->where->equalTo('id', $userEducation->id);
        
        return $this->executeDelete($delete);
    }
}