0

I'm new to Agiletoolkit and still learning the ropes. Here's my question:

I have a regular CRUD for managing jobs.

$this->add('CRUD')->setModel('Job');

Model contains field "job_number" which is should automatically be filled-in on the "Add" page: N762, N763, etc.

Is this something that can be done in the model or the addjob page being called from the CRUD table?

Can any one give me some very simple code example?

Thanks

romaninsh
  • 10,606
  • 4
  • 50
  • 70
Trevor
  • 11
  • 1
  • 4

2 Answers2

2

I believe this should be done on Model level. It basically have nothing to do with View level.

If this job_number field is strictly related with ID, then you can even make it as expression field in model and don't even store it in database. For example, job_number = id + 100.

But if you really want to store it in database, then you should probably do like this:

1) in model create job_number as ordinary field with type('number'), but with ->system(true) or ->editable(false) depending on where you want to see this field (form, grid);

2) in model init method add hook afterInsert.

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

    // ... your field definitions here

    // add afterInsert hook
    $this->addHook('afterInsert',array($this,'afterInsert'));
}

3) in model create method

function afterInsert($m,$new_id){ // <-- new_id is ID of newly inserted record
    $m->load($new_id);
    $m['job_number'] = $new_id + 100; // <-- your function here
    $m->save();
}

or maybe you can even write it simpler - not sure

function afterInsert($m,$new_id){ // <-- new_id is ID of newly inserted record
    $this->set('job_number',$new_id + 100); // <-- your function here
    $this->save();
}

This should work, but I didn't test this. Just writing here on the fly.

Try this out and if you need more help just let us know. Also you're more than welcome to join ATK4 IRC channel and ask your questions directly.

DarkSide
  • 3,670
  • 1
  • 26
  • 34
  • Thanks for the tip. Just a bit of clarification: the Jobs table has primary key (ID) with auto increment. What I need really is to have the regular field JOB_NO auto populated with a value eg: N762 based on the next auto increment of the Job table ID field when the add job button in the CRUD is clicked. So basically: pull the next auto increment number, add an "N" in front, put that into the JOB_NO field when the form comes up so the user doesn't have to deal with it. – Trevor Jan 25 '13 at 02:56
  • Yeah, that's exactly what I said in my answer. In my example I created job_number as ID+100, but you can change that to $m['job_number']='N'.$new_id and it should work. – DarkSide Jan 25 '13 at 09:52
  • But if you don't want to save this job_number physically in database (it's useless if it's hardly tied to ID which never change), then you can create it as expression (calculated) field in model with something like this $this->addExpression('job_number',"concat('N','id')") – DarkSide Jan 25 '13 at 09:54
0

Something like this should address your question:

in model init:

$this->addField("job_no")
    ->defaultValue("N" . $this->dsql()->del("fields")->field(
          $this->dsql()->expr("max(id) + 1"), "nr"
    )->getOne());

Not tested, though.

jancha
  • 4,916
  • 1
  • 24
  • 39