JModel or JTable?

For Joomla! 1.5 Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general
Locked
texdc
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Wed Apr 11, 2007 8:16 pm

JModel or JTable?

Post by texdc » Wed Apr 25, 2007 10:12 pm

The model is the part of the component that encapsulates the application’s data. It will often provide routines to manage and manipulate this data in a meaningful way in addition to routines that retrieve the data from the model. In our case, the model will contain methods to add, remove and update information about the books in the database. It will also contain methods to retrieve the list of books from the database. In general, the underlying data access technique should be encapsulated in the model. In this way, if an application is to be moved from a system that utilizes a flat file to store its information to a system that uses a database, the model is the only element that needs to be changed, not the view or the controller.
OK, so if the model is to encapsulate access to the database, why have a JModel class?  It seems to me that both JTable and JModel need to be one class, not separate.

Also, if JTable models a database table, but doesn't include JOIN and FOREIGN KEY functionality, why bother?

Anyone care to straighten me out?

TIA!

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

Re: JModel or JTable?

Post by tcp » Thu Apr 26, 2007 1:17 am

texdc wrote:
OK, so if the model is to encapsulate access to the database, why have a JModel class?  It seems to me that both JTable and JModel need to be one class, not separate.

Also, if JTable models a database table, but doesn't include JOIN and FOREIGN KEY functionality, why bother?

Anyone care to straighten me out?

TIA!
In summary, the model focuses on business logic and the table on data entities in the database.  By entity, I mean a single row in a table in a database.  Usually loading an entity would not require a JOIN, but you're right, JTable does not support foreign keys.  Remember that we still have people using MySQL 3.* out there.

So, if you have business logic named "addNewOrder", you may have a JModel method addNewOrder(...) that works with one more more JTable objects.

Hope that helps.

tcp
Your solution for a single-page checkout on any website.
http://moolah-ecommerce.com

texdc
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Wed Apr 11, 2007 8:16 pm

Re: JModel or JTable?

Post by texdc » Thu Apr 26, 2007 2:35 am

Thanks tcp.  I'll give you some ip points for responding quickly ;)

So, a JModel class would have one, or more, JTable classes as properties?  Like this?

[php]
myModelOrder extends JModel {
  protected $order;
  protected $user;

  function __construct( $config=array(), $orderid=null ) {
    parent::__construct( $config );
    $this->order = new myTableOrder( '#__my_orders', 'id', JFactory::getDBO() );
    $this->user = new myTableUser( '#__users', 'id', JFactory::getDBO() );

    if ( isset( $orderid ) ) {
      $this->order->load( intval( $orderid ) );
      $userid = $this->order->get( 'userid' );
      $this->user->load( intval( $userid ) );
    }
  }

  // etc...
}
[/php]

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

Re: JModel or JTable?

Post by tcp » Thu Apr 26, 2007 1:57 pm

That is the idea.  Here are some additional thoughts with some sample code.

1) You can use the constructor of your JTableDerived object to consolidate some of your code.

Code: Select all

function __construct() 
	{
		$db	= JFactory::getDBO();
		
		parent::__construct('#__mytable', 'id', $db );
		
		$this->_type	= 'query';
	} // end constructor
2) I'm a big fan of get methods and lazy loading in models.  Example below.

Code: Select all

	function getConnection() 
	{
		if ( ! is_object($this->_connection) )
		{
			
			$id		= $this->_table->connection_id;
			$this->_connection	= LQMConnection::getInstance($id);

		}
		
		return $this->_connection;
	} // end getConnection()
tcp
Your solution for a single-page checkout on any website.
http://moolah-ecommerce.com

texdc
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Wed Apr 11, 2007 8:16 pm

Re: JModel or JTable?

Post by texdc » Fri Apr 27, 2007 2:27 am

Thanks.  I've actually come up with a more flexible solution, albeit quite a bit more complicated to set up.  It involves some overloading and a bit of refactoring via derived classes.  I'm in the middle of a very huge project, which is why I requested some assistance, so I can't post any code here, at least not anytime soon.

Thanks again.


Locked

Return to “Joomla! 1.5 Coding”