Yii database Operations (curd operations)

Source: Internet
Author: User
Tags numeric value yii

Database operations

Getting Model objects

$model = model Name:: Models (); or $model = new model name ();

1. Querying multiple records (return value: two-dimensional array)

$result = $model->findall ();

$result = $model->findall (conditions);

$result = $model->findall ("select" and "=" fields, fields, Fields ... "));//Limit the fields that the query displays

$result = $model->findall (Array ("select" and "=" field, field, field ... "," condition "=" condition "));//Limit the fields and conditions that the query displays

$result = $model->findall (Array ("select" and "=" field, field, field ... "," condition "=" condition "," order "=" field desc "));// Limit the fields, conditions, and sorts that the query displays

$result = $model->findall (Array ("select" and "=" field, field, field ... "," condition "=" condition "," order "=" Field desc "," limit "= >, "offset" = start value);//Limit the field, condition, sort, limit, offset for the query display

$result = $model->findallbysql ("SELECT statement");

2. Query a record (return value: one-dimensional array)

$model = model Name:: Models (); or $model = new model name ();

$result = $model->find ();

$result = $model->find (conditions);

$result = $model->findbypk (primary key);

$result = $model->findbypk (Array (primary key 1, primary key 2 ...));

$result = $model->findbysql ("SELECT statement");

3. Add record (return value: Number of rows affected)

$model = new model name ();

$model, field = value;

$model, field = value;

$model, field = value;

$result = $model->save ();

4. Delete Record (return value: Number of rows affected)

$model = model Name:: Models (); or $model = new model name ();

$result = $model->deleteall ("condition");

$result = $model->DELETEBYPK (primary key value);

$result = $model->deletebypk (Array (primary key 1, primary key 2 ...));

5. Modify record (return value: Number of rows affected)

$model = model Name:: Models (); or $model = new model name ();

$result = $model->updateall (associative array, condition);

$result = $model->updatebypk (primary key, associative array);

$result = $model->updatebypk (Array (primary key 1, primary key 2 ...), associative array);

6. Execute SQL statement (return value: two-dimensional array)

$db = Yii::app ()->db;

$st = $db->createcommand ("SELECT query Statement");

$rs = $st->queryall ();//Get more than one record

7. Execute SQL statement (return value: one-dimensional array)

$db = Yii::app ()->db;

$st = $db->createcommand ("SELECT query Statement");

$rs = $st->queryrow ();//Get a record

8. Execute SQL statement (return value: one-dimensional array)

$db = Yii::app ()->db;

$st = $db->createcommand ("SELECT query Statement");

$rs = $st->querycolumn ();//Get All records for the first field

9. Execute SQL statement (return value: two-dimensional array)

$db = Yii::app ()->db;

$st = $db->createcommand ("SELECT query Statement");

$obj = $st->query ();

$rs = $st->readall ();//Get more than one record

10. Execute SQL Statement (return value: one-dimensional associative array)

$db = Yii::app ()->db;

$st = $db->createcommand ("SELECT query Statement");

$obj = $st->query ();

$rs = $obj->read ();//Get a record

11. Execute SQL Statement (return value: Number of rows affected)

$db = Yii::app ()->db;

$st = $db->createcommand ("Insert, UPDATE, DELETE statement");

$rs = $st->execute ();//execute INSERT, UPDATE, DELETE statement

12. Execute SQL statement, bind parameter (return value: Success or failure)

$db = Yii::app ()->db;

$st = $db->createcommand ("Insert, UPDATE, DELETE statement");

$st->bindparam (": Name", $ variable, PDO::P aram_str);

$st->bindparam (": Name", $ variable, PDO::P aram_str);

$st->bindparam (": Name", $ variable, PDO::P aram_str);

$rs = $st->execute ();//execute INSERT, UPDATE, DELETE statement

13. Number of records to be queried (return value: numeric value)

$model = model Name:: Models (); or $model = new model name ();

$totalRow = $model->count ();

$totalRow = $model->count (conditions);

14, through the Cdbcriteria class to implement the query operation (return value: two-dimensional array)

Location: framework/db/schema/cdbcriteria.php

$model = new Model (), or $model = class Name:: Model ()

$criteria = new Cdbcriteria ();

$criteria->select = "field, field, field |*";

$criteria->condition = "condition";

$criteria->order = "sort";

$criteria->limit = number of bars;

$criteria->offset = start value;

$result = $model->findall ($criteria);

Yii database Operations (curd operations)

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.