Welcome, Guest
Username: Password: Remember me

TOPIC: Customise a model

Customise a model 23 Mar 2015 09:52 #12891

  • Tainder
  • Tainder's Avatar
  • Offline
  • New Member
  • Posts: 2
  • Karma: 0
Hey,

I'm actually trying to customise my component, but I have some question about how the models works.

I was testing with my query but it seems like i have to load all the models in order to use my request.
Shall I call all the model in a single one ? And how we do this ?

Or there's already a model which call all the others and should I work in it ?

thanks for you time.
The administrator has disabled public write access.

Customise a model 24 Mar 2015 14:31 #12892

  • BTB300
  • BTB300's Avatar
  • Online
  • Moderator
  • Posts: 414
  • Thank you received: 130
  • Karma: 46
Hi Tainder,
Well that depends if you want to modify the model itself or leave the model intact and modify the query

to understand what the model is doing as you customize your model just echo the query at the end of the prepareQuery function

But first
Although it may look like all of the models are loaded the switch statement takes care of which model / fields / filters etc are actually included in the query
switch($this->getState('context', 'all'))
{
	case 'layoutname.modelname':

	//BASE FIELDS
	$this->addSelect(	'a.somefield,'
	.......
	.......
 
 // $this->getState('context', 'all')  returns the the layout names and associated model
// $this->getState('context', 'all') would be in the following format layoutname.modelname
// and the case picks up the layout and model name

// so you can set the state in your display function located in the specific view.html.php file
$state->set('context', 'layoutname.modelname');

to call the model from somewhere in your code
$model = Model::getInstance('component', 'viewModel');

// add field to the query in the model
$this->addSelect('a.somefield AS field');

to modify the model you can add various options
$model = Model::getInstance('component', 'viewModel');

// add field to the current model somewhere in your code
model->addSelect('a.somefield AS field');

// call a specific instance of the model

// somewhere in your code
$model = Model::getInstance('component', 'viewModel');

// call a second model
$modelTwo = Model::getInstance('component', 'viewModelTwo');

Left Join in the model (example)
//SELECT
$this->addSelect('_left_table_alias_.field AS `_left_table_alias_some_field`');

//JOIN
$this->addJoin('`#__left_table_name` AS _left_tabl_alias_ ON _left_table_alias_.id = a.some_some_field', 'LEFT');

Adding to the $query variable in the model
you can call some custom function to build a custom query parameter such as below and simply add it to the $query variable
$join = buildMyLeftJoin();
// and this function would then return something like 
// '`#__left_table_name` AS _left_tabl_alias_ ON _left_tabl_alias_.id = a.some_some_field';
$query->join('LEFT', $join);

adding Where
$query->where('a.id = somevalue');

$this->addWhere('a.id = somevalue');

// somewhere in your code, view, helper...
$model->addWhere('a.id = somevalue');

so here would be a practical example...
so lets say you want to completely replace the model that is normally used in that layout and your replacement model has no joins no filters but you want some data from another table and filter the data
// get the alternate model
$model = $model = Model::getInstance('component', 'viewModel');

//-- create the left join --//
// add the select
$model->addSelect('_some_table_alias.some_field AS some_table_alias_some_field')

// add the left join
$model->$this->addJoin('`#__some_table` AS _some_table_alias_ ON _some_table_alias_.id = a.some_some_field', 'LEFT');

// apply a filter to the query
$model->addWhere('a.id = _some_table_alias_some_field');
So now you have a completely different filtered, left joined dataset available in your layout

PS Its late here... if i have made a mistake in my explanation i apologize in advance and only too happy to adjust this reply should there be a mistake

but hope it helps to understand a little better
Last Edit: 24 Mar 2015 14:53 by BTB300.
The administrator has disabled public write access.
The following user(s) said Thank You: admin, cdfncy54, Tainder

Customise a model 26 Mar 2015 09:44 #12894

  • Tainder
  • Tainder's Avatar
  • Offline
  • New Member
  • Posts: 2
  • Karma: 0
Thanks for the answer, but I still have some troubles when I want to use the result of the request in my view.

I coded this in my model :
$this->addSelect(	'#__train_marches.nom_marche AS marche,'
						 . '#__train_lieux.nom_lieux AS lieux,'
						 . '#__train_lieux.pk_lieux AS pk,'
						 . '#__train_lieux.latitude_lieux AS latitude,'
						 . '#__train_lieux.longitude_lieux AS longitude,'
						 . '#__train_signalisation.description_sgnalisation AS signalisation');

		$query->join('INNER','#__train_estcomposede ON #__train_estcomposede.id_marche = #__train_marches.id_marche');
		$query->join('INNER','#__train_troncon ON #__train_estcomposede.id_troncon = #__train_troncon.id_troncon');
		$query->join('INNER','#__train_ligneferroviaire ON #__train_troncon.id_ligne_troncon = #__train_ligneferroviaire.id_ligne');
		$query->join('INNER','#__train_comporte ON #__train_comporte.id_ligne = #__train_ligneferroviaire.id_ligne');
		$query->join('INNER','#__train_lieux ON #__train_lieux.id_lieux = #__train_comporte.id_lieux');
		$query->join('INNER','#__train_signalisation ON #__train_signalisation.id_signalisation = #__train_comporte.id_signalisation');

And when in my view/tml/default.php I coded this but nothing appears, had some SQL errors, I corrected them and now i got a blank page :
<?php foreach($this->items as $i => $item): ?>
		<tr class="row<?php echo $i % 2; ?>">
			<td>
				<?php echo $item->marche ; ?>
			</td>
			<td>
				<?php echo $item->lieux ; ?>
			</td>
			<td>
				<?php echo $item->pk ; ?>
			</td>
			<td>
				<?php echo $item->latitude ; ?>
			</td>
			<td>
				<?php echo $item->longitude ; ?>
			</td>
			<td>
				<?php echo $item->signalisation ; ?>
			</td>
		</tr>
<?php endforeach; ?>


Edit : Ok now I had a mistake in the sql
Last Edit: 26 Mar 2015 12:41 by Tainder.
The administrator has disabled public write access.

Customise a model 26 Mar 2015 19:09 #12898

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
Thanks BTB,

as usual, it is detailed, precise and reliable.
K++
K++

Just for information, when you use query->join() it is correct and native.
When you use addJoin() (from Cook) it will merge two (JOIN) identical statements if they match exactly the same. Sometimes it is usefull when you have many contexts. (From my own experiment...)
Coding is now a piece of cake
The administrator has disabled public write access.
Time to create page: 0.084 seconds

Get Started