Yii Framework Forum: Getting An Id (Query Builder) - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Getting An Id (Query Builder) Rate Topic: -----

#1 User is offline   haosmark 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 35
  • Joined: 27-January 13

Posted 11 February 2013 - 08:17 PM

I have a form where I collect "job" data from to insert a new row into a DB table. Two fields are then used to recover a store id from a separate DB table. I'm just wondering if there's a better way to do this kind of stuff. Here's my bit of code, it just feels like there's a better code for this:

Job controller:
		if(isset($_POST['Job']))
		{
			$model->attributes=$_POST['Job'];
            $model->store_id = $test = Job::getStoreId($model->account, $model->store_number);
			if($model->save())
				$this->redirect(array('view','id'=>$model->work_order));
		}


Job model (query to retrieve the ID):
    public static function getStoreId($account, $number)
    {
        $_id = Yii::app()->db->createCommand()
                ->select('id')
                ->from('store')
                ->where('account = :ui_account AND number = :ui_store_number', array(
                    ':ui_account' => $account,
                    ':ui_store_number'  => $number
                ))->queryRow();
        return $_id['id'];
    }

0

#2 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,327
  • Joined: 21-February 11
  • Location:Greece

Posted 23 February 2013 - 04:45 PM

Hi my friend

May you can do it by Relational Active Record but is depended by the complication of your Ar schema

http://www.yiiframew...en/database.arr
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users