I've never used a framework before, so I wanted to see if this fairly simple scenario was done correctly or could be improved:
public function actionCreate($id) {
// Is request Ajax
if(Yii::app()->request->getIsAjaxRequest()) {
$userID = Yii::app()->user->id;
try {
$cmd = Yii::app()->db->createCommand();
$cmd->insert('potential_item',array(
'item_id'=> (int) $id,
'user_id' => (int) $userID,
),'id=:id', array(':id'=>$id));
echo 'Item Added';
} catch(CDbException $e){
echo '<strong>Error!</strong> ' . $e->getCode() . '</div>';
}
}
}
Here's what I wanted to achieve with the code. It's basically just a wish list. It's a pretty basic SQL builder statement that I think is secure.
I have a constraint on the DB so that a user and item ID have to be unique. That's why I used the try catch
block, and it's really the only DB issue I see occurring. Would the scenario I have used work okay, or am I missing other exceptions?
I also decided to echo the error code. Would you recommend that I just leave a generic message for the end user?
As I really only expect a constraint error, should I check the error code with an if-statement?
views
where you can assign the success state or error messages and all the layout is handled in a template? \$\endgroup\$ – mheinzerling Aug 12 '13 at 12:49