Hi Mandy and Schmidt,
I was able to get going..but would appreciate if you can help me in
optimizing my code
Here is the my table structure
Articles
id
title
Keywords
id
title
Articles_Keywords_Users
id
article_id
keyword_id
user_id // always 1
I created a simple view and here is the view code
=========== view ===============
<form action="<?php echo $html->url('/demo/add'); ?>" method="post">
<div class="required">
<?php echo $form->labelTag('Article/title', 'Title');?>
<?php echo $html->input('Article/title', array('size' => '60'));?>
<?php echo $html->tagErrorMsg('Article/title', 'Please enter the
Title.');?>
</div>
<div class="required">
<?php echo $form->labelTag('Keyword/list', 'Title');?>
<!-- user provides a comma separated list of keywords -->
<?php echo $html->areaTag('Keyword/list', 25, 5 );?>
<?php echo $html->tagErrorMsg('Keyword/list', 'Please enter
Keywords.');?>
</div>
<div class="submit">
<?php echo $html->submit('Add');?>
</div>
</form>
and here is my controller method that handles the view
=========== method add
function add(){
if(empty($this->data)){
$this->render();
}
else{
$message = "";
// extract article title
$article = $this->data['Article'];
$condition = array('title' => " = {$article['title']} ")
//check if article
exists or not.
// if it exists get its
id otherwise save and then get its id
if($this->Article->hasAny($condition)){
$result = $this->Article->find($condition);
$article['id'] = $result['Article']['id'];
$message = $message + "Article: " .
$article['title'] . " already
exists" . '<br/>';
}
else{
$this->Article->create();
if($this->Article->save(array('Article' =>
$article))){
//if
successfully saved get its id
// although
next two lines doesn't make sense
//but only
after executing the below command
// i was able
to get id successfully
$this->Article->execute("SELECT
LAST_INSERT_ID() from articles");
$article['id'] =
$this->Article->getLastInsertID();
$message = $message + "Article: " .
$article['title'] . "
successfully saved" . '<br/>';
}else{
die();
}
}
// handle comma
separated keywords
$list = $this->data['Keyword']['list'];
unset($this->data['Keyword']['list']);
$keys = explode(",", $list);
foreach ($keys as $key){
// get each key
$key = trim($key);
$keyword = array();
$keyword['title'] = $key;
$keyCondition = array('title' => " = {$key}");
//check if keyword is already present in the
database
// if not add it
// otherwise get it ID
if($this->Keyword->hasAny($keyCondition)){
$result =
$this->Keyword->find($keyCondition);
$keyword['id'] =
$result['Keyword']['id'];
$message = $message + $key . " already
exists in database" .
'<br/>';
}else{
$this->Keyword->create();
if($this->Keyword->save(array('Keyword'
=> $keyword))){
$this->Keyword->execute("SELECT
LAST_INSERT_ID() from keywords");
$keyword['id'] =
$this->Keyword->getLastInsertID();
$message = $message + 'Keyword
: ' . $key . ' successfully saved
<br/>';
}
else{
$message = $message + 'Error:
0000 Unable to save keyword : ' .
$key . '<br/>';
continue;
}
}
//update joining table
$this->ArticlesKeywordsUser->create();
$joiner = array('article_id' => $article['id'],
'keyword_id' =>
$keyword['id'],
'user_id' => 1);
if($this->ArticlesKeywordsUser->save(array('ArticlesKeywordsUser'
=> $joiner))){
$message = $message + 'joiner table
updated ' . '<br/>';
}else{
$message = $message + 'Error 0001:
Failed to save joiner table ' .
'<br/>';
}
}// next key
$this->Session->setFlash($message);
}// end of if data is present
} // end of add
Now things are working...but I would like to optimize the code..One way
I can think of is to create a method that first verifies if the record
already exists or not ..if it exists then get its id otherwise save and
then return its id..
One problem that I am still not able to solve is related to
setFlash...I am not getting any message.
I would be thankful if some expert baker can take out some time to help
in optimizing this code. Probably, then I can convert it into a
tutorial
Regards
Bingo
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---