Validating form data against related db table

Sorry if this is a simple task to do but I just started using the framework (its actually my first) and I’m still learning things.

Anyway, I’m trying to create a simple time-in / time-out system. I have 3 tables right now: account (holds login info), employee (misc info like first_name, last_name, timesheet_code) and timesheet (holds time_in and time_out entries). In order to record timesheet entries, the user/employee needs to first enter in a timesheet code that’s specific to this user then hit submit and the system uses the server’s timestamp to automatically grab the current time. How would I be able to validate the timesheet_code element against the employee table?

I already have the time_in part working, but it doesn’t validate the timesheet_code yet. Only validation I’ve been able to do is by passing both employee and timesheet models to the view file and using the employee model’s rules (array(‘timesheet_code’, ‘required’, ‘on’=>‘timesheet’))

Hmm, ok I just found out about the exist validator.


array('timesheet_code', 'exist', 'on'=>'timesheet'),

It works but it seems to check all records under employee table. Here’s the timein action in my Timesheet controller




public function actionTimeIn()

	{

		$timesheet=new Timesheet;

		$employee=new Employee('timesheet');


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['Timesheet']) && isset($_POST['Employee']))

		{

			$timesheet->attributes=$_POST['Timesheet'];

			$employee->attributes=$_POST['Employee'];

			

			$valid=$timesheet->validate();

			$valid=$employee->validate() && $valid;

			if ($valid) {

				if($timesheet->save(false))

					$this->redirect(array('timeout'));

			}

		}

		

		$this->render('timein',array(

			'timesheet'=>$timesheet,

			'employee'=>$employee

		));

	}



I believe this happens because I’m just creating a new instance of the Employee model and using that in the controller and view?