Friday, May 25, 2012

Re: compare and update database records at once.

On Fri, May 25, 2012 at 6:48 AM, Teji <teji.infin@gmail.com> wrote:
> foreach($file['NewDataSet']['Products'] as $products):
>            {
>                $tp['text']=$products[1];
>                $tp['date']=$products[2];
>               $this->model->saveall($tp);
>     }endforeach;
>
> bt now i want to match this $file data with the existing records in
> database. if it match then update that record else make a new entry..
>
> how can i do this.
> actually i dnt know how to compare my $file data with my database
> records.

Once again, look up how to use foreach, it doesn't need the colon and
the brackets.

But anyway...

foreach($file['NewDataSet']['Products'] as $products) {
$tp['text']=$products[1];
$tp['date']=$products[2];

if($in_db = $this->model->find('first', array('conditions' =>
array('text' => $tp['text'])))) {
// The entry is in the database, just update it
$tp['id'] = $in_db['model']['id'];
} else {
// The entry is not in the database, create it
$this->model->create($tp);
}
// do the save stuff here, if there is an id, it gets edited,
otherwise it gets saved
}

I think that could be a starting point for you.

Mike.

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: