Jump to content

Creating an ObjectModel for a Module


mohdoh

Recommended Posts

I am creating a new module that create, read, update and delete a table in the database. Thus, I am creating a new ObjectModel for this module. Below is what I did, appreciate if someone can point out what I am doing wrong that the Controller not reading the classl:
 
I created a classes folder in the module directory, and place the following 'Measurement.php' file in it:

<?php
class MeasurmentCore extends ObjectModel
{
	public $id_customer = null;
	public $alias;
	public $weight;
	public $height;
	public $date_add;
	public $date_upd;
	public $deleted = 0;
	public static $definition = array(
		'table' => 'measure',
		'primary' => 'id_measure',
		'fields' => array(
			'id_customer' => 		array('type' => self::TYPE_INT, 'validate' => 'isNullOrUnsignedId', 'copy_post' => false),
			'alias' => 				array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => true, 'size' => 32),
			'weight' => 			array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required' => true),
			'height' => 			array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required' => true),
			'deleted' => 			array('type' => self::TYPE_BOOL, 'validate' => 'isBool', 'copy_post' => false),
			'date_add' => 			array('type' => self::TYPE_DATE, 'validate' => 'isDateFormat', 'copy_post' => false),
			'date_upd' => 			array('type' => self::TYPE_DATE, 'validate' => 'isDateFormat', 'copy_post' => false),
		),
	);
}

Then in the ModuleFrontController 'measurements.php' located in the 'measure/controllers/front/'

<?php
Class MeasureMeasurementsModuleFrontController extends ModuleFrontController
{
	protected $_measure;

	public function init()
	{
		parent::init();

		include_once(_PS_MODULE_DIR_.'measure/classes/measurement.php');
		$this->_measure = new Measurement($id_measure);
		
	}  

	public function initContent()
	{
		parent::initContent();
		$this->setTemplate('measure.tpl');
				
	} 
}

When I load the '/index.php?fc=module&module=measure&controller=measurements' page, I get this error:

Fatal error: Class 'Measurement' not found in C:\wampn\.*.*\modules\measure\Controllers\front\Measurements.php on line 11

Thanks in advance,

Edited by mohdoh (see edit history)
Link to comment
Share on other sites

Hi

 

i think you miss indicate $id_measure

if i look Addresscontrller.php the $id_adress = somethink

 

try


public function init()
{
parent::init();

include_once(_PS_MODULE_DIR_.'measure/classes/measurement.php');

$id_measure = (int)Tools::getValue('id_measure', 0);

// Initialize measure
if ($id_measure)
{
$this->_measure = new Measurement($id_measure);
}

}  

@++

 

Loulou66

Link to comment
Share on other sites

  • 1 year later...

You "should" use include_once before class definition

<?php
require_once(...);

Class MeasureMeasurementsModuleFrontController extends ModuleFrontController
{

But ... in overall: You should never use require/include methods. Use composer instead of it and you can rely on it's autoloader.

 

Also few more things: follow the rules and standards: PSR-2, SOLID, camel case naming convention, etc.

This is crucial to keep your code clean, extendable and understanable when you will have to work with it later.

Link to comment
Share on other sites

  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...