Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
0f6f7600
Commit
0f6f7600
authored
May 09, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
guide WIP [skip ci]
parent
64796aea
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
17 deletions
+34
-17
start-databases.md
docs/guide/start-databases.md
+34
-17
No files found.
docs/guide/start-databases.md
View file @
0f6f7600
...
...
@@ -2,40 +2,57 @@ Working with Databases
======================
In this section, we will describe how to create a new page to display data fetched from a database table.
To achieve this goal, you will configure the database connection, create an
[
Active Record
](
db-active-record.md
)
class
to fetch and represent database data, and then create an
[
action
](
structure-controllers.md
)
and
a
[
view
](
structure-views.md
)
to present the data to end users.
To achieve this goal, you will configure a database connection, create an
[
Active Record
](
db-active-record.md
)
class,
and then create an
[
action
](
structure-controllers.md
)
and a
[
view
](
structure-views.md
)
.
Through this tutorial, you will learn
*
How to configure
DB connections
;
*
How to configure
a DB connection
;
*
How to define an Active Record class;
*
How to query data using the Active Record class;
*
How to display data in a view in a paginated fashion.
Note that in order to finish this section, you should have basic knowledge and experience about databases.
Y
ou should know how to create a database and how to execute SQL statements using a DB client tool.
In particular, y
ou should know how to create a database and how to execute SQL statements using a DB client tool.
Configuring a Database Connection
--------------------
-------------
Preparing a Database
--------------------
To start, you should have a database ready. It can be a SQLite, MySQL, PostgreSQL, MSSQL or Oracle database.
For simplicity, in the following description, we will assume that you already have a MySQL database named
`basic`
.
To begin with, create a database from which you will fetch data in your application. You may create
a SQLite, MySQL, PostgreSQL, MSSQL or Oracle database. For simplicity, in the following description
let's assume you have created a MySQL database named
`basic`
.
Create a table named
`address`
and insert some sample data. The SQL statements are showing as follows,
Make sure you have installed the
[
PDO
](
http://www.php.net/manual/en/book.pdo.php
)
PHP extension and
the PDO driver for the database you are using (e.g.
`pdo_mysql`
for MySQL).
Now create a table named
`country`
and insert some sample data, using the SQL statements as shown below,
```
sql
CREATE
TABLE
`address`
(
`id`
int
(
11
)
NOT
NULL
AUTO_INCREMENT
,
`street`
varchar
(
128
),
`city`
varchar
(
128
),
`state`
varchar
(
128
),
`country`
varchar
(
128
)
);
CREATE
TABLE
`country`
(
`code`
char
(
2
)
NOT
NULL
PRIMARY
KEY
,
`name`
char
(
52
)
NOT
NULL
,
`population`
int
(
11
)
NOT
NULL
DEFAULT
'0'
)
ENGINE
=
InnoDB
DEFAULT
CHARSET
=
utf8
;
INSERT
INTO
`Country`
VALUES
(
'AU'
,
'Australia'
,
18886000
);
INSERT
INTO
`Country`
VALUES
(
'BR'
,
'Brazil'
,
170115000
);
INSERT
INTO
`Country`
VALUES
(
'CA'
,
'Canada'
,
1147000
);
INSERT
INTO
`Country`
VALUES
(
'CN'
,
'China'
,
1277558000
);
INSERT
INTO
`Country`
VALUES
(
'DE'
,
'Germany'
,
82164700
);
INSERT
INTO
`Country`
VALUES
(
'FR'
,
'France'
,
59225700
);
INSERT
INTO
`Country`
VALUES
(
'GB'
,
'United Kingdom'
,
59623400
);
INSERT
INTO
`Country`
VALUES
(
'IN'
,
'India'
,
1013662000
);
INSERT
INTO
`Country`
VALUES
(
'RU'
,
'Russia'
,
146934000
);
INSERT
INTO
`Country`
VALUES
(
'US'
,
'United States'
,
278357000
);
```
Configuring a DB Connection
---------------------------
Creating an Active Record
-------------------------
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment