Problem With Related Tables

I have three tables

tbl_customer

customer_ID(PK)

tbl_job

job_ID

customer_ID(FK)

tbl_jobNumber

ID(PK)

jobNumber

customer_ID (FK)

I am trying to find a solution where if i save a new job,

  1. the app will look in tbl_jobNumber for a jobNumber corresponding to the customer_ID of the tbl_job

  2. save that as tbl_job.job_ID

  3. increment the value in tbl_jobnumber.jobNumber by one (ready for use on the next job)

i have tried a few thing including query builder, placing code in beforeSave in the job model …

i have job model with the following relations


'customer' => array(self::BELONGS_TO, 'Customers', 'customer_ID'),

and in the customer model the relationships are




'jobs' => array(self::HAS_MANY, 'Jobs', 'customer_ID'),  

'jobNos' => array(self::HAS_ONE, 'JobNo', 'Customer_ID'),



i would have thought that i could just do


$model->job_ID = $model->customer->jobNos->JobNo 

But that isn’t working , can anyone offer any solutions to my problem ?