YII 1 Reprint Database operation

Source: Internet
Author: User
Tags yii

YII 1.0 Database Operation query, add, UPDATE, delete
1, according to the criteria to query a collection
$objectResult =post::model ()->findall ($condition, $params);
$objectResult =post::model ()->findall ("Username=:name", Array (": name" = = $username));
$objectResult =repairitem::model ()->findall ("Orderno=:orderno and Orderpostid=:orderpostid", Array (": OrderNo" = > $orderInfo [' OrderNo '], ': orderpostid ' = $orderInfo [' Orderpostid ']);

$INFOARR = Newslist::model ()->findall ("status = ' 1 ' ORDER by PostID DESC limit 10");
2, according to the primary key query a collection, you can use multiple primary keys FINDALLBYPK
$objectResult =post::model ()->findallbypk ($postIDs, $condition, $params);
$objectResult =post::model ()->findallbypk ($postid, "name Like:name and Age=:age", Array (': name ' = = $name, ' age ' = > $age));
$objectResult =post::model ()->findallbypk (array);
3, according to the condition query a set, can be more than one condition, put the condition into the array inside Findallbyattributes
$objectResult =post::model ()->findallbyattributes ($attributes, $condition, $params);
$objectResult =post::model ()->findallbyattributes (Array (' username ' = ' jack '));
4. Query an array based on SQL statements Findallbysql
$arrResult =post::model ()->findallbysql ($sql, $params);
$arrResult =post::model ()->findallbysql ("select * from Tbl_post where username like:name", Array (': Name ' = '?% ')) ;

5, according to the primary key query an object EG:FINDBYPK (1);
$arrResult =post::model ()->findbypk ($postID, $condition, $params);
$arrResult =post::model ()->findbypk (1);

6, according to the criteria to query out a set of data, "may be multiple, but he only return the first row of data"
$arrRow =post::model ()->find ($condition, $params);
$arrRow =post::model ()->find (' Username=:name ', Array (': Name ' = ' Jack '));

7, according to the criteria to query a set of data, "can be multiple conditions, put the conditions in the array, the query is the first data"
$objectResult =post::model ()->findbyattributes ($attributes, $condition, $params);
$objectResult =post::model ()->findbyattributes (Array (' username ' = ' objectresult '));
8, according to the SQL statement query a set of data, "query is also the first data"
$objectResult =post::model ()->findbysql ($sql, $params);
$objectResult =post::model ()->findbysql ("select * from Objectresult where username=:name", Array (': name ' = = ') Objectresult '));
9, through the Cdbcriteria class find query out an object
$criteria =new Cdbcriteria;
$criteria->select= ' username '; Limit which fields are displayed
$criteria->condition= ' Username=:username '; A query condition with acondition. Addcondition for multiple conditions
$criteria->params=array (":username=> ' Jack");
$criteria->order = "Postsort DESC";
$criteria->limit = "3";
$post =post::model ()->find ($criteria);
10. Multi-Conditional query statements
$criteria = new Cdbcriteria;
$criteria->addcondition ("postid=1"); Equivalent to where PostID = 1
$criteria->addincondition (' PostID ', Array (1,2,3,4,5)); Equivalent to where PostID in (1,2,3,4,5,);
$criteria->addnotincondition (' PostID ', Array (1,2,3,4,5));//equivalent to not in (1,2,3,4,5,)
$criteria->addcondition (' postid=1 ', ' or ');//equivalent to OR rather than and
$criteria->addsearchcondition (' username ', ' Jack ');//equivalent to where name like '%jack% '
$criteria->addbetweencondition (' PostID ', 1, 4);//equivalent to between 1 and 4
$criteria->compare (' PostID ', 1); Automatically processed into addcondition or addincondition according to your parameters.
$criteria->compare (' PostID ', array (n/a)); The array is called Addincondition


$criteria->select = ' postid,parentid,name '; Limit which fields are displayed
$criteria->join = ' xxx '; Connection table
$criteria->with = ' xxx '; Call relations
$criteria->limit = 10; Take 1 data, if less than 0, do not process
$criteria->offset = 1; Two is combined, which means that the limit is offset 1, or the representative. Limit 1,10
$criteria->order = ' xxx desc,xxx ASC ';//Sort condition
$criteria->group = ' group condition ';
$criteria->having = ' having condition ';
$criteria->distinct = FALSE; Whether the unique query

third, the number of queries to determine whether the query has results

Query the number of records for a collection based on a condition, and return an int type digit
$intCount =post::model ()->count ($condition, $params);
$intCount =post::model ()->count ("Username=:name", Array (": name" = = $username));

Query the number of records for a collection based on the SQL statement, returning an int type number
$intCount =post::model ()->countbysql ($sql, $params);
$intCount =post::model ()->countbysql ("select * from Objectresult where username=:name", Array (': name ' = = ') Objectresult '));

Based on a condition query query the resulting array has no data, there is data to return a true, otherwise no found
$boolExists =post::model ()->exists ($condition, $params);
$boolExist =post::model ()->exists ("Name=:name", Array (": name" = = $username));

Iv. methods to be added
$objectPost = new Post;
$objectPost->username = $username;
$objectPost->password = $password;
Maybe
$objectPost->attributes = $arrNewData;

if ($objectPost->save ()) {
$intPostId = $objectPost->primarykey; Generate primary Key ID
echo "Add success";
}else{
echo "Add failed";
}

v. Methods of modification

Post::model ()->updateall ($attributes, $condition, $params);
$count =post::model ()->updateall (Array (' username ' = ' 11111 ', ' password ' = ' 11111 '), ' password=:p ', ', array ' (':p-up ' and ' 1111a1 '));
if ($count > 0) {
echo "modified successfully";
}else{
echo "failed to modify";
}

$rt = Postlist::model ()->updateall (Array (' status ' = ' 1 '), ' Staff_postid=:staff and Host_postid=:host ', Array (': Staff ' = = $staff _postid, ': Host ' = $host _postid));

Post::model ()->updatebypk ($PK, $attributes, $condition, $params);
$count =post::model ()->updatebypk (1,array (' username ' = ' jack ', ' password ' = ' Jack '));

$count =post::model ()->updatebypk (array), Array (' username ' = ' jack1 ', ' password ' = ' jack1 '), ' username= : Name ', Array (': Name ' = ' Jack '));

if ($count > 0) {
echo "modified successfully";
}else{
echo "failed to modify";
}

Post::model ()->updatecounters ($counters, $condition, $params);

$count =post::model ()->updatecounters (Array (' status ' =>1), ' Username=:name ', Array (': Name ' = ' Jack '));

if ($count > 0) {
echo "modified successfully";
}else{
echo "failed to modify";
}
The array (' status ' =>1) represents the post table in the database according to the condition Username= ' Jack ', and all the results that are queried for the status field are added from 1

Vi. Methods of deletion
DeleteAll
Post::model ()->deleteall ($condition, $params);
$count = Post::model ()->deleteall (' Username=:name and password=:p ", Array (': Name ' = ' Jack ', ':p ' Jack '));
$count = Post::model ()->deleteall (' PostID in (") ');//delete the data PostID for these
if ($count >0) {
echo "Delete succeeded";
}else{
echo "Delete Failed";
}

Deletebypk
Post::model ()->deletebypk ($PK, $condition, $params);
$count = Post::model ()->deletebypk (1);
$count =post::model ()->deletebypk (array), ' Username=:name ', Array (': Name ' = ' Jack '));
if ($count >0) {
echo "Delete succeeded";
}else{
echo "Delete Failed";
}}

vii. Executing native SQL statements
$sql = "Select T.*, T1.userphone, T1.truename, t1.usermail from {{member_contact}} t ' left join ' {{member}} T1 on t.userid = T1.userid where T.contactid in (N/a) ";
$arrRows =yii::app ()->db->createcommand ($sql)->query ();
foreach ($arrRows as $k = = $v) {

}

Eight, transaction processing "Multiple table update insert operation, use transaction processing"
$transaction = Yii::app ()->db->begintransaction ();
try{
$arrOrderProfile = Array (
' OrderID ' = $orderId,
' Userip ' = $userip,
' ContactID ' = $contactId,
' UpdateTime ' = $now
);
$modelOrderProfile = new Repairorderprofile ();
$modelOrderProfile->attributes = $arrOrderProfile;
if (! $modelOrderProfile->save ()) {
throw new CException (' Maintenance order generation failed, notification transaction rollback ');
}
$recordCounter = Counter::model ()->updatebypk (1,array (' max_id ' = $orderId));
if ($recordCounter <= 0)
throw new CException (' Order counter update failed, notify transaction rollback ');
$transaction->commit (); Committing a transaction will actually perform a database operation
}catch (Exception $e) {
File_put_contents (' Action.log ', $e->getcode (). ': '. $e->getmessage (). ' --'. Date (' y-m-d h:i:s ', Time ()), file_append);
$transaction->rollback ();

}

YII 1 Reprint Database operation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.