Arthur Kushman
Arthur Kushman

Reputation: 3629

Yii sql query bindings, with params, do not work properly

I'm new to Yii and everything seems good, but the problem is, when I`m using the binding params, like (DAO stile):

$command = $this->conn->createCommand($sql);

$command->bindColumn("title", "test_title");

$result = $command->query();

or (Active Record):

$row = Movies::model()->find("m_id=:m_id", array(":m_id"=>27));
    or
$row = Movies::model()->findByPk(24); 

I've tried everything: 1) added a config param to mysql config. in main.php - 'enableParamLogging' => true
2) changed strings from ' to " 3) added another param just in case of mysql versions - 'emulatePrepare'=>true

Nothing works for me. I thought that the problem is in params, but bindColumn method doesn't use it, so my presumption is that some module of Yii hasn't been include in config file or something like that.

My model looks like this (created in /models dir):

class Movies extends CActiveRecord {

  public static function model($className = __CLASS__) {
    parent::model($className);

  }

}

Just for everybody to avoid unnecessary questions: the database is configured properly in main.php conf file, there is a table movies, there is a PKs 24 and 27 also.

All native SQL works fine, except using in DAO special methods to bind some params and if in AR using findByPk or find. I hope that this is clear, guys don't bother me with obvious simple technical possibilities, that I can (as U presume) did wrong.

PS Another helpful info - when calling

$command->bindColumn("title", "test_title");

FW's throwing an Exception - CDbCommand and its behaviors do not have a method or closure named "bindColumn". So, as mentioned above, I think that Yii don't see those special methods and this is certain. How can I repair it?

Ok, why this code don't work either? There is no Exceptions, just blank page.

$sql = "SELECT title, year_made FROM movies WHERE year_made=':ym'";

$command = $this->conn->createCommand($sql);

$command->bindParam(":ym", "2012", PDO::PARAM_STR);

$result = $command->query();

Upvotes: 1

Views: 7300

Answers (3)

Grampa
Grampa

Reputation: 1643

The DAO binding isn't working because there is no bindColumn. You only have bindParam, that binds a variable to a column, or bindValue, that binds a value. I don't know what's wrong with the AR query though.

Edit: Your DAO code should look like this:

$sql = "SELECT * FROM sometable WHERE title = :title";
$command = $this->conn->createCommand($sql);
$command->bindParam(":title", $tile_var);
$result = $command->query();

Upvotes: 4

acorncom
acorncom

Reputation: 5955

You may have two separate issues. At least your DAO code (if that is what you actually have in your code) is binding wrong.

Binding needs to occur before the query is done (and it's done on the command object), not on the result object. See more info here: http://www.yiiframework.com/doc/guide/1.1/en/database.dao#binding-parameters

As far as your AR queries go, those could also be problematic. You can use findByAttributes instead of this line (although your line should work):

$row = Movies::model()->find("m_id=:m_id", array(":m_id"=>27));

The below should work if you have a standard id key. If you don't (and are using m_id, have you set your model's primaryKey() function up?

$row = Movies::model()->findByPk(24); 

Also, you'll be getting a model object back, not a row if that changes how you are handling the results ...

Upvotes: 0

mkey
mkey

Reputation: 212

Have you generated movie model by Gii generator or you wrote it by yourself?

Edit:

As I know you have to add tableName

public function tableName() {
    return 'movies';
}

Upvotes: 0

Related Questions