1

I am a complete newbie in the world of yii and doing my best to learn the framework as best as I can, while reading the book on Yii - 'Web Application Development Using Yii and PHP' by Jeffrey Winesett,

I came across :

'params'=>array(':projectId'=>$this->_project->id)

If anyone has read the book, can someone explain why do we use :projectId why not directly projectId? What does the ':' stand for? Is it the specific syntax or is it something php specific.

This was the exact code :

public function actionIndex()
    {

        $dataProvider=new CActiveDataProvider('Issue', array(
            'criteria'=>array(
                'condition'=>'project_id=:projectId',
                'params'=>array(':projectId'=>$this->_project->id),
            ),
        ));
        $this->render('index',array(
            'dataProvider'=>$dataProvider,
        ));
    }

Any answer would be highly appreciated. Regards.

2
  • Is it (this params array) being used in an SQL query for binding parameters? Commented Apr 16, 2013 at 17:27
  • This was the exact code public function actionIndex() { //$dataProvider=new CActiveDataProvider('Issue'); $dataProvider=new CActiveDataProvider('Issue', array( 'criteria'=>array( 'condition'=>'project_id=:projectId', 'params'=>array(':projectId'=>$this->_project->id), ), )); $this->render('index',array( 'dataProvider'=>$dataProvider, )); } Commented Apr 16, 2013 at 17:28

1 Answer 1

4

The ":" id there to identify param within the condition string. It's a way to safely bind params to an sql statement.

In your case, when you specify this condition

project_id=:projectId

In your db schema, Yii know that project_id is an integer, then when you bind the param with :

'params'=>array(':projectId'=>$this->_project->id),

Yii will make sure you bind an integer and write the SQL statement for it and generate SQL statement like this : project_id=26

This is the same thing for string ex.:

        'criteria'=>array(
            'condition'=>'name=:myname',
            'params'=>array(':myname' => 'Ronald McDonald'),
        ),

then your sql statement for this condition will be name='Ronald McDonald'

Sign up to request clarification or add additional context in comments.

2 Comments

Is the binding using ':' specific to only Yii or is it PHP specific or also used in other frameworks?
This is specific for the framework, I've seen it in others frameworks too, but this is not specific for PHP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.