Excellent!, You are right about PK conflicts, I was avoiding to do
multiple operation in the DB, but with your suggestions I belive the
benefits outweigh the extra update in the process,
Thanks
On Feb 10, 9:59 am, Grant Cox <[EMAIL PROTECTED]> wrote:
> 1. None of the methods mentioned are thread safe - if you get
> multiple concurrent executions you will run into conflicts. You
> should always rely on the database's auto-increment primary key, don't
> create the PK yourself. So if you want to use the Client primary key
> in the client's account number, try:
>
> // insert the new client, so we have a guaranteed unique id
> $client_data = array( 'name'=>'Client name' );
> $this->Client->create();
> $this->Client->save( $client_data );
>
> // now that the client has been saved, with no possibility for PK
> conflict, update with the correct account number
> $account="$branch-".date("y")."-00".$this->Client->id;
> $this->Client->save( array( 'acct_numb' => $account ) );
>
> 2. Even if you do want to use something where you pull the highest
> id, at least do it the Cake way, none of this unnecessary custom SQL.
>
> $highest_id = $this->Client->field('id', null, 'id DESC');
>
> On Feb 10, 2:14 pm, brian_gaff <[EMAIL PROTECTED]> wrote:
>
> > If you just added the record then
>
> > $this->Model->getLastInserID(); //will give you the id you're looking
> > for
>
> > Now, suppose you added the record a while ago, and you just want to
> > find the highest ID, then you can add the following to your app_model:
>
> > function getHighestID()
> > {
> > $res = $this->query("Select `{$this->name}.id` from `{$this->name}`
> > ORDER BY `{$this->name}.id` DESC LIMIT 1";
> > return $res[$this->name]['id'];
> > }
>
> > Then from any controller you can do:
>
> > $this->Model->getHighestID();
>
> > On Feb 9, 3:00 pm, Salam Fall <[EMAIL PROTECTED]> wrote:
>
> > > Just solved the problem
> > > Here is how I have done it
> > > $ret = $this->Client->query("Select id from
> > > clients order by id
> > > DESC LIMIT 1");
> > > $newid=$ret[0]['clients']['id'] + 1;
> > > $yr=date("y");
> > > $account="$branch-$yr-00$newid";
> > > $this->data['Client']['acct_numb'] = $account;
> > > $this->Client->create();
> > > if ($this->Client->save($this->data)) etc....
>
> > > Now my other question is how do i create my own classes and make them
> > > available in the controller and the view??
>
> > > On Feb 9, 5:34 pm, Salam Fall <[EMAIL PROTECTED]> wrote:
>
> > > > Here is what i have done so far but it's not working
> > > > $branch="SEN";
>
> > > > $ret = $this->Client->findBySQL("Select id from
> > > > clients order by id
> > > > DESC LIMIT 1");
> > > > $lastId=$ret[0]['id'];
> > > > $act=$lastId + 1;
> > > > $yr=date("y");
> > > > $account="$branch-$yr-00$act";
> > > > $this->data['Client']['acct_numb'] =
> > > > $account;
> > > > $this->Client->create();
> > > > if ($this->Client->save($this->data) etc....
>
> > > > On Feb 9, 5:31 pm, Salam Fall <[EMAIL PROTECTED]> wrote:
>
> > > > > Nubee here
>
> > > > > How do I find the last record in the database and have it's ID value
> > > > > in the controller and be able to manipulate it mathematically.
>
> > > > > Because all i want to do is put the ID of the last record of the
> > > > > Clients table add 1 to it and append the result to a string that will
> > > > > be used to create the Client account number.
>
> > > > > Also if I want to create my own custom functions or classes to add to
> > > > > my cake APP how do I do that.
>
> > > > > Thanks for your help.- Hide quoted text -
>
> > > - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---