converting simple query to cake query?

前端 未结 3 1279
悲&欢浪女
悲&欢浪女 2021-01-28 20:05

Actually I have 1 query but I am unable to convert it into CakePHP query format.

$result = \"select 
             * from esl_userresults
           where 
               


        
相关标签:
3条回答
  • 2021-01-28 20:47

    You can easily run direct sql queries on cake using e.g.: $this->Picture->query("SELECT * FROM pictures LIMIT 2;"); (cake manual)

    Or try something simillar to this:

        $result = Esl_Userresults->find('all' , array('conditions' => array(
                 "Esl_Userresults.esl_songID" => "Esl_Lyrics.id",
                 "Esl_Lyrics.song_name LIKE" => '%'.$esl_keyword.'%'),
                  'recursive' => 1)
                  );
    

    ..given that you have properly linked Esl_Userresults and Esl_Lyrics models.

    0 讨论(0)
  • 2021-01-28 20:49

    Use Containable behaviour rather than recursive. It will give you control down to individual field level. Using it now, at an early stage, will make it second nature later.

    If you get confused building the conditions, build it outside the method call.

    Try to avoid the use of double quotes except where you need to include escaped or parsed data - they're slower.

    $conditions = array(
        'EslUserresult.esl_songID' => 'EslLyric.id',
        'EslLyric.song_name LIKE' => '%'.$esl_keyword.'%'
                       )
    $this->EslUserresult->contain('EslLyric.text');
    $result = $this->EslUserresult->find('all',array('conditions'=>$conditions));
    
    0 讨论(0)
  • 2021-01-28 21:03

    You can use AppModel's query() function to run SQL query .

    ex : $this->ModelName->query('SELECT * FROMtable'); this will return an array of selected records . You can pass simple query as well as complex join query .

    0 讨论(0)
提交回复
热议问题