PradoSoft

Lazy Loading In SQLMAP Tutorial

From PRADO Wiki

Lazy Loading is a powerfull tool for 1::n associations with lots of data and not always used in your application.

Here is an example of use in a SQLMap based application.

SQLMap Declaration

 
	<resultMap id="PlayerResult" class="Player">
		<result property="Id" column="player_id" nullValue="0" />
		<result property="Name" column="player_name" />
		<result property="MySkills" column="player_id" select="findAllSkillsByPlayerId" LazyLoad="true" />
	</resultMap>
[...]
         <resultMap id="SkillResult" class="Skill">
		<result property="Id" column="skill_id" nullValue="0" />
		<result property="Label" column="skill_label" />
         </resultMap>
 
	<select id="findAllSkillsByPlayerId" parameterClass="int" ListClass="TList" resultMap="SkillResult">
		SELECT sk.*
		FROM 
			skills as sk
		
		WHERE 
			sk.skill_playerid=#value#
	</select>

It is mandatory to have ListClass attribute filled.

Class

 
public function _getMySkills() {
		if(!$this->_mySkills instanceof TList && !$this->_mySkills instanceof TObjectProxy)
		$this->_mySkills = new TList();	
		return $this->_mySkills;
	}
	public function _setMySkills($value) {
	if($value instanceof TList || $value instanceof TObjectProxy)
		$this->_mySkills = $value;	
	
		$this->_mySkills = $value;
	}

You need to return a new TList() if attribute is null, if you don't, lazyloading will not work.

How to use it

And finally, here is an example of use :

 
$player=$MyPlayerDao->getOnePlayerById("02");
//at this moment, you have a TObjectProxy in mySkills
foreach($player->getMySkills() as $v)
    echo $v->getLabel();
 
// it returns nothing (maybe an error)
// Caution : LazyLoading activates itself only if you '''call''' some method on the List.
 
foreach($player->getMySkills()->toArray() as $v)
    echo $v->getLabel();
 
//now you have all your skills :)

I hope it was usefull for you !

Personal tools
Your user name:

Your password:

MediaWiki