Advice on Yii. Сhapter 7.
Basics of working with a database, we take Open Real Estate as an example.
-
findByPk.
- $id = 25;
- $result = Apartment::model()->findByPk($id);
- echo $result->title_en;
Result:
Apartment on Novy Arbat St.
-
findAllByPk.
- $ids = array(25, 26, 27);
- $result = Apartment::model()->findAllByPk($ids);
-
- foreach ($result as $item) {
- echo '<pre>';
- print_r($item->title_en);
- echo '</pre>';
- }
Result:
Apartment on Novy Arbat St.
Apartment, Bolshaya Polyanka St., 28
1-Bedroom Apartment near Prospekt Mira metro station
-
findByAttributes.
- $result = Apartment::model()->findByAttributes(array('title_en' => 'Apartment on Novy Arbat St.'));
-
- echo $result->title_en;
- echo '<br>'.$result->id;
Result::
Apartment on Novy Arbat St.
25
-
findAllByAttributes.
- $result = Apartment::model()->findAllByAttributes(array('title_en' => array('2-Bedroom Apartment within a minute walk from Paveletskaya metro station', 'Apartment, Bolshaya Polyanka St., 28')));
-
- foreach ($result as $item) {
- echo '<pre>';
- print_r($item->title_en);
- print_r('<br>'.$item->id);
- echo '</pre>';
- }
Result:
Apartment, Bolshaya Polyanka St., 28
26
2-Bedroom Apartment within a minute walk from Paveletskaya metro station
29
One more example:
- $result = Apartment::model()->findAllByAttributes(array('id' => array(19, 20), 'title_en' => array('Apartment on Novy Arbat St.', 'House on Trubetskaya St.')));
-
- foreach ($result as $item) {
- echo '<pre>';
- print_r($item->title_en);
- print_r('<br>'.$item->id);
- echo '</pre>';
- }
As a result we will have the following::
House on Trubetskaya St.
19
-
find.
- $numMax = 19;
- $result = Apartment::model()->find('id < :numMax', array(':numMax' => $numMax));
-
- echo $result->title_en;
- echo '<br>'.$result->id;
Result:
2-Bedroom Apartment, Aviamotornaya St.
18
-
findAll.
- $numMax = 20;
- $result = Apartment::model()->findAll('id < :numMax', array(':numMax' => $numMax));
-
- foreach ($result as $item) {
- echo '<pre>';
- print_r($item->title_en);
- print_r('<br>'.$item->id);
- echo '</pre>';
- }
or:
- $numMax = 20;
- $result = Apartment::model()->findAll("id < {$numMax}");
-
- foreach ($result as $item) {
- echo '<pre>';
- print_r($item->title_en);
- print_r('<br>'.$item->id);
- echo '</pre>';
- }
Result:
2-Bedroom Apartment, Aviamotornaya St.
18
House on Trubetskaya St.
19
The same way we can choose all elements that are sorted by the title:
- $result = Apartment::model()->findAll(array('order' => 'title_en ASC'));
-
findBySql.
- $result = Apartment::model()->findBySql('SELECT id FROM {{apartment}} WHERE square = 60 AND title_en = "2-Bedroom Apartment, Aviamotornaya St."');
- echo $result->id;
Result:
18
-
findAllBySql.
- $result = Apartment::model()->findAllBySql('SELECT id, title_en FROM {{apartment}} WHERE square <= 50 AND square >= 40');
-
- foreach ($result as $item) {
- echo '<pre>';
- print_r($item->title_en);
- print_r('<br>'.$item->id);
- echo '</pre>';
- }
Result:
Apartment, Pskovskaya St.
21
Apartment on Novy Arbat St.
25
-
countBySql.
- $result = Apartment::model()->countBySql('SELECT COUNT(id) FROM {{apartment}} WHERE square <= 50 AND square >= 40');
- echo $result;
Result:
2
-
count.
- $result = Apartment::model()->count('id < 25');
- echo $result;
or:
- $result = Apartment::model()->count('id < :num', array(':num' => 25));
- echo $result;
Result:
Apartment on Novy Arbat St.
-
exists.
- $result = Apartment::model()->exists('id < :num', array(':num' => 25));
- echo var_dump($result);
Result:
bool(true)
- $result = Apartment::model()->exists('id < :num', array(':num' => 1));
- echo var_dump($result);
Result:
bool(false)
-
UpdateByPK.
- $ids = array(18, 19, 20);
- $result = Apartment::model()->updateByPk($ids, array('title_en' => 'New title'));
Result:
It returns true or false.
If it has been successfully executed, the value of a title_en field for listings with ID of 18, 19 and 20 in the table is displayed as a "New title".
-
updateAll.
- $result = Apartment::model()->updateAll(array('title_en' => 'New title'), 'title_en = :title', array(':title' => '2-Bedroom Apartment, Aviamotornaya St.'));
Result:
It returns true or false.
If it has been successfully executed, listings with a title_en field which have a value of '2-Bedroom Apartment, Aviamotornaya St.' will have a new value that is a "New title".
-
deleteByPk.
- $result = Apartment::model()->deleteByPk(18);
Result:
It returns true or false.
If it has been successfully executed, the listing with ID of 18 will be deleted.
-
deleteAll.
- $result = Apartment::model()->deleteAll('title_en = :title', array(':title' => 'House on Trubetskaya St.'));
Result:
It returns true or false.
If it has been successfully executed, the listing with a title_en field which has a value of 'House on Trubetskaya St.' will be deleted.
Discuss the article in the forum