DAO bindings and AR queries

Hi everyone, I’m new to Yii, so the Q is - when I execute queries this way, everything works fine:




$resutl = $this->conn->createCommand($sql)->query();

    

foreach ($result AS $row) {

      

      echo $row['title'];

      

    }



but when I try to do somthing like (DAO style):




    $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();



or (AR style):




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


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



both examples didn’t work.

Model code:




class Movies extends CActiveRecord {

  

  public static function model($className = __CLASS__) {

    parent::model($className);

    

  }

  

  public function tableName() {

    return 'movies';

  }

  

}



Though, I know that the @Override of method tableName is needless, because table name is compatible to a class name of a Model.

PS db is configured well, table name is movies, also there is strange behaviour - code just crashs and exceptions or even errors didn`t occur. Sorry for my English.


public static function model($className = __CLASS__) {

    parent::model($className);

    

  }

should be


public static function model($className = __CLASS__) {

    return parent::model($className);

    

  }

Thank You very much - this works gooood with AR, and what about bindings in DAO style queries?

I suggest to make the following change.




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



can be




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



or




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



change


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

with


"SELECT title, year_made FROM movies WHERE year_made=:ym"

if it does not work report your error message.

This is awesome, thanks a lot. It works well.