joomla jview class get methode passing variables

For Joomla! 1.5 Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general
Locked
User avatar
spiderglobe
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 151
Joined: Mon Apr 03, 2006 1:45 pm
Location: Netherlands

joomla jview class get methode passing variables

Post by spiderglobe » Fri Aug 01, 2008 9:19 am

I don't know if this is the place for this topic, but I will give it a try.

Within the Joomla VMC model it is not possible to pass any variables to the functions that are called, special with the get functions from the views object you cannot pass any variables. This is of course sometimes necessary to pass parameters to the model function that is being called.

For example a view class currently within joomla:

Code: Select all

class persons_Viewpersons extends JView {

	/*
	 * Edit the person object
	 */
	function edit_person () {
                 // get the person object;
                 $person = $this->get ('person');

                 // now I want to fetch the person reservations from the reservations model, but I cannot pass the 
                 // parameter for the person which would be nice 

                 $reservations = $this-get('person_reservations' ,' reservations');

	}

}
What I would like is this so I've more flexibility in my reservation model :

Code: Select all

class persons_Viewpersons extends JView {

	/*
	 * Edit the person object
	 */
	function edit_person () {
                 // get the person object;
                 $person = $this->get ('person');

                 // now I want to fetch the person reservations from the reservations model
                 //  but I cannot pass the parameter for the person which would be nice 
                $start_date = '2008-01-01';
                $start_date = '2009-01-01';
                $reservations = $this->get('person_reservations' ,' reservations',$person->id,  $start_date, $end_date  );

	}

}
But the current Joomla view class doesn't allow to pass parameters, this is the following function in the following file: /libraries/joomla/application/component/view.php

Code: Select all

	function &get( $property, $default = null )
	{

		// If $model is null we use the default model
		if (is_null($default)) {
			$model = $this->_defaultModel;
		} else {
			$model = strtolower( $default );
		}

		// First check to make sure the model requested exists
		if (isset( $this->_models[$model] ))
		{
			// Model exists, lets build the method name
			$method = 'get'.ucfirst($property);

			// Does the method exist?
			if (method_exists($this->_models[$model], $method))
			{
				// The method exists, lets call it and return what we get
                $result = $this->_models[$model]->$method();
                return $result;
			}

		}

		// degrade to JObject::get
		$result = parent::get( $property, $default );
		return $result;
	}
If the function is changed into the following it allows to pass one or more parameters:

Code: Select all

	function &get( $property, $default = null )
	{
		// If $model is null we use the default model
		if (is_null($default)) {
			$model = $this->_defaultModel;
		} else {
			$model = strtolower( $default );
		}

		// First check to make sure the model requested exists
		if (isset( $this->_models[$model] ))
		{
			// Model exists, lets build the method name
			$method = 'get'.ucfirst($property);

			// Does the method exist?
			if (method_exists($this->_models[$model], $method))
			{
				$numargs = func_num_args();
				if ($numargs > 2 ) {
					$arg_list = func_get_args();
    				$args = array();
					for ($i = 2; $i < $numargs; $i++) {
        				$args[] = $arg_list[$i];
    				}
					return ( call_user_func_array(array( $this->_models[$model], $method),$args));
				} else {
					return ( $this->_models[$model]->$method() );
				}
			}

		}
		// degrade to JObject::get
		$result = parent::get( $property, $default );
		return $result;
	}
Now the model function can receive variables:

Code: Select all

<?php
class model_reservations extends JModel{
      function person_reservations( $person_id = null, $periode_start = null, $periode_end = null ) {
                   // here is the logic of the function
       }
   
}
Can this be implemented within the Jview class? It would be nice if parameters can be passed.

Regards,

Richard

rmullinnix
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 108
Joined: Fri Nov 02, 2007 3:39 pm

Re: joomla jview class get methode passing variables

Post by rmullinnix » Fri Aug 01, 2008 1:19 pm

No answer, but a couple of points
1) Doesn't this approach violate the MVC pattern in that the view is setting the state of the model? The view should just display the data contained in the model.
2) You can define member variables in your model to hold the person object and set them when you call $this->get('person'). Then access the person object from the model when $this->get('person_reservation') is invoked.
3) You can simply bypass the $this->get('dataitem') and invoke your model function directly -- $model->getPersonReservation($startdate, $enddate); -- (where $model = $this->getModel())

I try to make sure any model state information is managed by the model and not the view.

User avatar
spiderglobe
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 151
Joined: Mon Apr 03, 2006 1:45 pm
Location: Netherlands

Re: joomla jview class get methode passing variables

Post by spiderglobe » Fri Aug 01, 2008 2:03 pm

Thanks for your response.

The view is not setting the state of the model. It gives just parameters to the model to act on.
2) You can define member variables in your model to hold the person object and set them when you call $this->get('person'). Then access the person object from the model when $this->get('person_reservation') is invoked.
Yes, I know that this is possible but when you need to pass some parameters the code and the model self get expanded a lot (lot of extra lines and functions) and that for passing parameters..... In my case I retrieve an object A form a model A and use the parameters from object A to retrieve information from another model so I do need to pass these parameters through the model request...
3) You can simply bypass the $this->get('dataitem') and invoke your model function directly -- $model->getPersonReservation($startdate, $enddate); -- (where $model = $this->getModel())
This is what I actual are using on the moment (this is the only way). But this is not what you want within the view (to fetch the model object to pass parameters). A more coding nicer approach would be that you can pass the parameters through the get command. Less code, less errors good readable,etc.. And I assume if I want to get an object I can specify the parameters for getting the object....

Regards,

Richard

rmullinnix
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 108
Joined: Fri Nov 02, 2007 3:39 pm

Re: joomla jview class get methode passing variables

Post by rmullinnix » Fri Aug 01, 2008 2:53 pm

Understand . . . thanks for the clarification. I would recommend that you post the request under under J1.5 Feature Requests - White Papers - http://forum.joomla.org/viewforum.php?f=500

User avatar
tcp
Joomla! Ace
Joomla! Ace
Posts: 1548
Joined: Wed Sep 21, 2005 9:25 am
Location: Thailand
Contact:

Re: joomla jview class get methode passing variables

Post by tcp » Thu Aug 14, 2008 5:01 pm

Interesting idea. Here is how we do it in JHTML::_() .

Code: Select all

if (is_callable( array( $className, $func ) ))
{
	$args = func_get_args();
	array_shift( $args );
	return call_user_func_array( array( $className, $func ), $args );
}
Same idea. I'm not sure if there is a reason why we don't do this in JView or the other MVC classes.
Your solution for a single-page checkout on any website.
http://moolah-ecommerce.com

User avatar
spiderglobe
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 151
Joined: Mon Apr 03, 2006 1:45 pm
Location: Netherlands

Re: joomla jview class get methode passing variables

Post by spiderglobe » Thu Aug 14, 2008 5:14 pm

Same idea. I'm not sure if there is a reason why we don't do this in JView or the other MVC classes.
I can't think of any reason... I've made use of other MVC frameworks which support this, also the example provided is from an own developed MVC framework. It make the code cleaner and better readable from the view part. The functionality is easy to add and has no influence on the beheauviour of components already using the J1.5 MVC framework.

Regards.

Richard

User avatar
tcp
Joomla! Ace
Joomla! Ace
Posts: 1548
Joined: Wed Sep 21, 2005 9:25 am
Location: Thailand
Contact:

Re: joomla jview class get methode passing variables

Post by tcp » Thu Aug 14, 2008 5:34 pm

Let me ask around to see if there is a reason.
Your solution for a single-page checkout on any website.
http://moolah-ecommerce.com

gesellix
Joomla! Apprentice
Joomla! Apprentice
Posts: 39
Joined: Fri Sep 02, 2005 8:45 pm
Contact:

Re: joomla jview class get methode passing variables

Post by gesellix » Mon Feb 23, 2009 1:07 pm

Though this thread is a bit old - are there any news?
I would also like to have a possibility to pass parameters...


Locked

Return to “Joomla! 1.5 Coding”