CakePHPã®DboSource::expression()ã調ã¹ã¦ã¿ããconditionsçã«SQLãæ¸ãããã®ãã®ããªï¼
DboSource::expression() ã¨ããã¡ã½ãããè¦ããã¦ãã¡ãã£ã¨æ°ã«ãªã£ãã®ã§èª¿ã¹ã¦ã¿ã¾ãããèªåã¨ãã¦ã¯ãã£ã¨éæ³ã®ã¡ã½ããçãªä¾¿å©ãªã®ã ã£ãããããªã¼ã¨ãæã£ã¦ããã§ãããæ®å¿µãªããããã§ã¯ç¡ãã¿ãããã§ãæè§èª¿ã¹ãã®ã§ã¨ã³ããªã¼ã«ãã¦ããã¾ãã
ãã¶ãconditionsçã«SQLãæ¸ãããã®ãã®
æ
å ±ãããã¾ãè¦ã¤ãããªãã£ãã®ã§ã¡ããã¨ææ¡ä»åãã¦ã¾ããããªã®ã§è¦ããã使ãæ¹ãè²¼ã£ã¦ããã¾ããééããå«ãå¯è½æ§é«ãã§ããéã«ãããexpression()
ã使ã£ã¦ãããã©ããã風ã«ä½¿ã£ã¦ããæãã¦ããããã¨å¬ããã§ãã
SQLã®NOW()ã使ã£ã¦ãINSERTæã®æéã§ä¿åãã
<?php $this->data['SomeModel']['your_datetime_field'] = DboSource::expression('NOW()'); $this->Model->save($this->data);php - Submitting current timestamp in CakePHP - Stack Overflow
ãµãã¯ã¨ãªã使ã
<?php $conditionsSubQuery['`User2`.`status`'] = 'B'; $dbo = $this->User->getDataSource(); //Datasourceãªãã¸ã§ã¯ããåå¾ $subQuery = $dbo->buildStatement( array( 'fields' => array('`User2`.`id`'), 'table' => $dbo->fullTableName($this->User), 'alias' => 'User2', 'limit' => null, 'offset' => null, 'joins' => array(), 'conditions' => $conditionsSubQuery, 'order' => null, 'group' => null ), $this->User ); $subQuery = ' `User`.`id` NOT IN (' . $subQuery . ') '; $subQueryExpression = $dbo->expression($subQuery); $conditions[] = $subQueryExpression; $this->User->find('all', compact('conditions'));http://book.cakephp.org/ja/view/1030/%E8%A4%87%E9%9B%91%E3%81%AA-find-%E3%81%AE%E6%9D%A1%E4%BB%B6
ãªã³ã¯å ã§ã¯ãå ¨ç¶idå±æ§ãç¡ãã®ã§ãè¯ãä½ç½®ã«#ã§ãªã³ã¯åºæ¥ãªãã§ããããã¼ã¸ã®ããªãä¸ã®ã»ãã§ãã
CASEå¼ã使ã
<?php $expression = $this->testDb->expression("CASE Sample.id WHEN 1 THEN 'Id One' ELSE 'Other Id' END AS case_col"); $result = $this->testDb->fields($this->Model, null, array("id", $expression));https://github.com/cakephp/cakephp/blob/1.3.6/cake/tests/cases/libs/model/datasources/dbo_source.test.php#L2978
ã¨ã¹ã±ã¼ãã¯èªåã§ãã
Notice that every user input should be properly sanitized and escaped
if you plan to use it as a SQL expression with DboSource::expression
().
So, as you can see in my article, you can use SQL functions like this:<?php // inside the model scope $db = $this->getDataSource(); $this->data[$this->alias]['signedup'] = $db->expression('CURDATE()');but be aware that expressions won't be automatically escaped so you
need to it manually with DboSource::value() :<?php $db = $this->getDataSource(); $expression = sprintf("CONCAT('[', %s, ']')", $db->value($userInput)); $this->data[$this->alias]['signedup'] = $db->expression($expression);http://cakephp.1045679.n5.nabble.com/REgarding-using-Mysql-functions-like-NOW-CURDATE-etc-td1309285.html#a1309290
expression()
ã§æ¸ããSQLã¯ã¨ã¹ã±ã¼ããããªãã®ã§ãå¤é¨ããã®å
¥åå¤ãªã©ã¯DboSource::value()ã§ã¨ã¹ã±ã¼ããã¦ç½®ããªãã¨ã§ãã
解説
http://book.cakephp.org/ja/view/1030/%E8%A4%87%E9%9B%91%E3%81%AA-find-%E3%81%AE%E6%9D%A1%E4%BB%B6 ã§è²ã
æ¸ãã¦ããã¨ãããconditionsã«ã¯CakePHPã§ã®æ¸ãæ¹ã§æ¡ä»¶ãæ¸ãã¦ããã¾ãããªã®ã§expression()
ã§SQLãæ¸ãã®ã¯ãããã¨å¹²æ¸ããªãããã«ã£ã½ãã§ããã
<?php function expression($expression) { $obj = new stdClass(); $obj->type = 'expression'; $obj->value = $expression; return $obj; }https://github.com/cakephp/cakephp/blob/1.3.6/cake/libs/model/datasources/dbo_source.php#L206
ã³ã¼ãã¯ãããã·ã³ãã«ã§ããåã«stdClass
ã«è©°ãã¦è¿ãã ããªã®ã§ããã®ãªãã¸ã§ã¯ããåãåã£ãå´ã§ãã¾ããã¨å¦çãããã§ãããããã¶ãã»ã»ã»ãããã¾ã§è¿½ãããã¾ããã§ãããå
¨ææ¤ç´¢ãããã¦ããstdClassã®type === 'expression'
ãè¦ã¦ãã®ã3ç®æãããããè¦å½ãããªããã§ããããã