{"id":91,"date":"2014-02-02T20:41:57","date_gmt":"2014-02-03T04:41:57","guid":{"rendered":"http:\/\/www.tech.dimprash.com\/?p=91"},"modified":"2014-02-02T20:42:36","modified_gmt":"2014-02-03T04:42:36","slug":"yii-db-built-in-functions-reference","status":"publish","type":"post","link":"http:\/\/www.tech.dimprash.com\/?p=91","title":{"rendered":"Yii DB built in functions &#8211; Reference"},"content":{"rendered":"<p>Quick reference &#8211; Yii inbuilt functions &#8211; DB related<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p>###### Active Record functions: ############<br \/>\nDifferent ways to query<br \/>\n\/\/ 1st way<br \/>\n$criteria=new CDbCriteria;<br \/>\n$criteria->select=&#8217;title&#8217;;  \/\/ only select the &#8216;title&#8217; column<br \/>\n$criteria->condition=&#8217;postID=:postID&#8217;;<br \/>\n$criteria->params=array(&#8216;:postID&#8217;=>10);<br \/>\n$post=Post::model()->find($criteria);<\/p>\n<p>\/\/ 2nd way<br \/>\n$post=Post::model()->find(array(<br \/>\n    &#8216;select&#8217;=>&#8217;title&#8217;,<br \/>\n    &#8216;condition&#8217;=>&#8217;postID=:postID&#8217;,<br \/>\n    &#8216;params&#8217;=>array(&#8216;:postID&#8217;=>10),<br \/>\n));<\/p>\n<p>\/\/Other ways<br \/>\n\/\/ Find All<br \/>\n\/\/ find all rows satisfying the specified condition<br \/>\n$posts=Post::model()->findAll($condition,$params);<br \/>\n\/\/ find all rows with the specified primary keys<br \/>\n$posts=Post::model()->findAllByPk($postIDs,$condition,$params);<br \/>\n\/\/ find all rows with the specified attribute values<br \/>\n$posts=Post::model()->findAllByAttributes($attributes,$condition,$params);<br \/>\n\/\/ find all rows using the specified SQL statement<br \/>\n$posts=Post::model()->findAllBySql($sql,$params);<\/p>\n<p>User::model()->findAll(&#8216;first_name=? AND last_name=?&#8217;, array(&#8216;Paul&#8217;, &#8216;Smith&#8217;));<br \/>\nUser::model()->findAllByAttributes(array(&#8216;first_name&#8217;=>&#8217;Paul&#8217;, &#8216;last_name&#8217;=>&#8217;Smith&#8217;));  <\/p>\n<p>\/\/ find one<br \/>\n$post=Post::model()->find($condition,$params);<br \/>\n$post=Post::model()->find(&#8216;postID=:postID&#8217;, array(&#8216;:postID&#8217;=>10));<\/p>\n<p>\/\/ find by primary key<br \/>\n$post=Post::model()->findByPk($postID,$condition,$params);<\/p>\n<p>\/\/ find by attributes<br \/>\n$post=Post::model()->findByAttributes($attributes,$condition,$params);<br \/>\n$post=Post::model()->findByAttributes();<\/p>\n<p>\/\/ find by specified SQL statement<br \/>\n$post=Post::model()->findBySql($sql,$params);<\/p>\n<p>\/\/ count<br \/>\n$n=Post::model()->count($condition,$params);<\/p>\n<p>\/\/ CREATE : creating record<br \/>\n$post=new Post;<br \/>\n$post->title=&#8217;sample post&#8217;;<br \/>\n$post->content=&#8217;content for the sample post&#8217;;<br \/>\n$post->create_time=time(); \/\/ $post->create_time=new CDbExpression(&#8216;NOW()&#8217;);<br \/>\n$post->save();<\/p>\n<p>\/\/ UPDATE: Updating Record<br \/>\n$post=Post::model()->findByPk(10);<br \/>\n$post->title=&#8217;new post title&#8217;;<br \/>\n$post->save(); \/\/ save the change to database<\/p>\n<p>\/\/ DELETE: Delete record<br \/>\n$post=Post::model()->findByPk(10); \/\/ assuming there is a post whose ID is 10<br \/>\n$post->delete();<\/p>\n<p>\/\/ Scopes<br \/>\nhow to make use of scopes<br \/>\nIn your model make a entry as below<br \/>\n\/\/ scope to get the 5 records of status=1 and recently updated<br \/>\npublic function scopes()<br \/>\n{<br \/>\n    return array(<br \/>\n        &#8216;published&#8217;=>array(<br \/>\n            &#8216;condition&#8217;=>&#8217;status=1&#8217;,<br \/>\n        ),<br \/>\n        &#8216;recently&#8217;=>array(<br \/>\n            &#8216;order&#8217;=>&#8217;create_time DESC&#8217;,<br \/>\n            &#8216;limit&#8217;=>5,<br \/>\n        ),<br \/>\n    );<br \/>\n}<\/p>\n<p>And then use the below statement to query.<br \/>\n$posts=Post::model()->published()->recently()->findAll();<\/p>\n<p>\/\/ write in model class to to some ops before saving<br \/>\npublic function beforeSave() {<br \/>\n    if ($this->isNewRecord)<br \/>\n        $this->created = new CDbExpression(&#8216;NOW()&#8217;);<br \/>\n    else<br \/>\n        $this->modified = new CDbExpression(&#8216;NOW()&#8217;);<br \/>\n    \/\/ anything else.. like, date modification, time modification, etc..<\/p>\n<p>    return parent::beforeSave();<br \/>\n}<\/p>\n<p>######## Query builder functions: ############<br \/>\n\/\/ NEXT : sql command writing<br \/>\n$command = Yii::app()->db->createCommand(&#8216;SELECT * FROM tbl_user&#8217;);<\/p>\n<p>\/\/ fetch row<br \/>\n$user = Yii::app()->db->createCommand()<br \/>\n    ->select(&#8216;id, username, profile&#8217;) \/\/ to fetch all use ->select(&#8216;*&#8217;)<br \/>\n    ->from(&#8216;tbl_user&#8217;)  \/\/ for multiple table use as ->from(array(&#8216;tbl_user&#8217;, &#8216;tbl_profile&#8217;))<br \/>\n    ->where(&#8216;id=:id&#8217;, array(&#8216;:id&#8217;=>$id))<br \/>\n    ->queryRow();<br \/>\nOR<br \/>\n$row = Yii::app()->db->createCommand(array(<br \/>\n    &#8216;select&#8217; => array(&#8216;id&#8217;, &#8216;username&#8217;),<br \/>\n    &#8216;from&#8217; => &#8216;tbl_user&#8217;,<br \/>\n    &#8216;where&#8217; => &#8216;id=:id&#8217;,<br \/>\n    &#8216;params&#8217; => array(&#8216;:id&#8217;=>1),<br \/>\n))->queryRow();<\/p>\n<p>\/\/ Fetch row using join<br \/>\n$user = Yii::app()->db->createCommand()<br \/>\n    ->select(&#8216;id, username, profile&#8217;)<br \/>\n    ->from(&#8216;tbl_user u&#8217;)<br \/>\n    ->join(&#8216;tbl_profile p&#8217;, &#8216;u.id=p.user_id&#8217;)   \/\/ join, leftJoin, rightJoin, crossJoin, naturalJoin<br \/>\n    ->where(&#8216;id=:id&#8217;, array(&#8216;:id&#8217;=>$id))<br \/>\n    ->queryRow();<\/p>\n<p>\/\/ fetch all<br \/>\n$user = Yii::app()->db->createCommand()<br \/>\n    ->select(&#8216;id, username, profile&#8217;)<br \/>\n    ->from(&#8216;tbl_user&#8217;)<br \/>\n    ->where(&#8216;id=:id&#8217;, array(&#8216;:id&#8217;=>$id))<br \/>\n ->limit(10, 20) \/\/ limit 10 and offset 20<br \/>\n ->queryAll();<\/p>\n<p>$users = Yii::app()->db->createCommand()<br \/>\n    ->select(&#8216;*&#8217;)<br \/>\n    ->from(&#8216;tbl_user&#8217;)<br \/>\n    ->queryAll(); <\/p>\n<p>\/\/ display SQL statement<br \/>\n$sql = Yii::app()->db->createCommand()<br \/>\n    ->select(&#8216;*&#8217;)<br \/>\n    ->from(&#8216;tbl_user&#8217;)<br \/>\n    ->text; <\/p>\n<p>\/\/ INSERT<br \/>\n\/\/ build and execute the following SQL:<br \/>\n\/\/ INSERT INTO `tbl_user` (`name`, `email`) VALUES (:name, :email)<br \/>\n$command->insert(&#8216;tbl_user&#8217;, array(<br \/>\n    &#8216;name&#8217;=>&#8217;Tester&#8217;,<br \/>\n    &#8217;email&#8217;=>&#8217;tester@example.com&#8217;,<br \/>\n));<\/p>\n<p>\/\/ UPDATE `tbl_user` SET `name`=:name WHERE id=:id<br \/>\n$command->update(&#8216;tbl_user&#8217;, array(<br \/>\n    &#8216;name&#8217;=>&#8217;Tester&#8217;,<br \/>\n), &#8216;id=:id&#8217;, array(&#8216;:id&#8217;=>1));<\/p>\n<p>\/\/ DELETE FROM `tbl_user` WHERE id=:id<br \/>\n$command->delete(&#8216;tbl_user&#8217;, &#8216;id=:id&#8217;, array(&#8216;:id&#8217;=>1));<\/p>\n<p>\/\/ NEXT<br \/>\n\/\/ create a new entry with time modification example<br \/>\n$post=new Post;<br \/>\n$post->title = &#8220;Some title&#8221;;<br \/>\n$post->desciption = &#8220;Some description&#8221;;<br \/>\n$post->create_time=new CDbExpression(&#8216;NOW()&#8217;);<br \/>\n\/\/ $post->create_time=&#8217;NOW()&#8217;; will not work because<br \/>\n\/\/ &#8216;NOW()&#8217; will be treated as a string<br \/>\n$post->save();<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Quick reference &#8211; Yii inbuilt functions &#8211; DB related &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- ###### Active Record functions: ############ Different ways to query \/\/ 1st way $criteria=new CDbCriteria; $criteria->select=&#8217;title&#8217;; \/\/ only select the &#8216;title&#8217; column $criteria->condition=&#8217;postID=:postID&#8217;; $criteria->params=array(&#8216;:postID&#8217;=>10); $post=Post::model()->find($criteria); \/\/ 2nd way $post=Post::model()->find(array( &#8216;select&#8217;=>&#8217;title&#8217;, &#8216;condition&#8217;=>&#8217;postID=:postID&#8217;, &#8216;params&#8217;=>array(&#8216;:postID&#8217;=>10), )); \/\/Other ways \/\/ Find All \/\/ find all rows satisfying the specified condition &hellip; <a href=\"http:\/\/www.tech.dimprash.com\/?p=91\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Yii DB built in functions &#8211; Reference<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-91","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/91","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=91"}],"version-history":[{"count":2,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/91\/revisions"}],"predecessor-version":[{"id":93,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/91\/revisions\/93"}],"wp:attachment":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=91"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=91"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=91"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}