Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /usr/home/shadbb/domains/czub.info/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383
Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /usr/home/shadbb/domains/czub.info/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383
Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /usr/home/shadbb/domains/czub.info/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383
Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /usr/home/shadbb/domains/czub.info/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383
Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /usr/home/shadbb/domains/czub.info/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383
Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /usr/home/shadbb/domains/czub.info/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383
Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /usr/home/shadbb/domains/czub.info/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383
Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /usr/home/shadbb/domains/czub.info/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383
Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /usr/home/shadbb/domains/czub.info/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383
Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /usr/home/shadbb/domains/czub.info/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383
Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /usr/home/shadbb/domains/czub.info/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383
Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /usr/home/shadbb/domains/czub.info/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383
Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /usr/home/shadbb/domains/czub.info/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383
Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /usr/home/shadbb/domains/czub.info/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383
Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /usr/home/shadbb/domains/czub.info/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383
Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /usr/home/shadbb/domains/czub.info/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383
Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /usr/home/shadbb/domains/czub.info/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383
Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /usr/home/shadbb/domains/czub.info/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383
Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /usr/home/shadbb/domains/czub.info/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383
Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /usr/home/shadbb/domains/czub.info/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383
Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /usr/home/shadbb/domains/czub.info/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383
Wyszła nowa wersja Yii2, nawiasem mówiąc fajny framework (inni się nie zgodzą, ale każdy używa co mu pasuje), tylko jest nie zgodny z Yii. Dlatego będzie trzeba porobić trochę przykładów kodu:
Operacje CRUD w Yii2, założenia i inne przykłady w dokumentacji:
Active Record
Query Builder
Tworzenie (create):
Utworzenie i zapisanie wartości modelu:
$model = new Test;
$model->value = 'test';
$model->status = 0;
$model->save(); |
$model = new Test;
$model->value = 'test';
$model->status = 0;
$model->save();
Zapisanie wartości przez insert:
Yii::$app->db->createCommand()
->insert('test', [
'value' => 'test',
'status' => 0])
->execute(); |
Yii::$app->db->createCommand()
->insert('test', [
'value' => 'test',
'status' => 0])
->execute();
Zapisanie wartości przez zapytanie batch’owe:
Yii::$app->db->createCommand()
->batchInsert('table', ['value', 'status'],
[
['test', 0],
['test2', 1],
['test3', 2],
])
->execute(); |
Yii::$app->db->createCommand()
->batchInsert('table', ['value', 'status'],
[
['test', 0],
['test2', 1],
['test3', 2],
])
->execute();
Odczyt (read):
Odczytanie jednego wiersza:
$row = Yii::$app->db->createCommand('SELECT * FROM table WHERE id=:id',
[
':id' => $id,
])->queryOne(); |
$row = Yii::$app->db->createCommand('SELECT * FROM table WHERE id=:id',
[
':id' => $id,
])->queryOne();
Wyszukanie według głównego klucza:
$model = Test::find(1);
if($model){
echo $model->id;
echo $model->status;
} |
$model = Test::find(1);
if($model){
echo $model->id;
echo $model->status;
}
Wyszukanie wszystkich elementów
$model = Test::find()->all(); |
$model = Test::find()->all();
Wyszukanie według warunków
$model = Test::find()
->where('id > :id', [':id' => 1])
->one(); |
$model = Test::find()
->where('id > :id', [':id' => 1])
->one();
$model = Test::find()
->where(['id > 20', 'status' => 1])
->one(); |
$model = Test::find()
->where(['id > 20', 'status' => 1])
->one();
$model = Test::find()
->where("id > 20 and status=1")
->all(); |
$model = Test::find()
->where("id > 20 and status=1")
->all();
Wyszukanie według warunków
$model = Test::find()
->where('id > :id', [':id' => 20])
->orWhere('status > :status', [':status' => 20])
->andWhere('value = :value', [':value' => 'test'])
->all(); |
$model = Test::find()
->where('id > :id', [':id' => 20])
->orWhere('status > :status', [':status' => 20])
->andWhere('value = :value', [':value' => 'test'])
->all();
Zmiana (update):
Poprzez użycie modelu
$id=1;
$model = Test::find($id);
$model->value= 'testXXX';
$model->status = 345;
$model->save(); |
$id=1;
$model = Test::find($id);
$model->value= 'testXXX';
$model->status = 345;
$model->save();
Test::updateAllCounters(['status' => 125]); |
Test::updateAllCounters(['status' => 125]);
Poprzez użycie komend DB
Yii::$app->db->createCommand()
->update('test', ['value'], 'testXXX')
->execute(); |
Yii::$app->db->createCommand()
->update('test', ['value'], 'testXXX')
->execute();
Yii::$app->db->createCommand()
->update('test', ['status' => 125], 'id > 130')
->execute(); |
Yii::$app->db->createCommand()
->update('test', ['status' => 125], 'id > 130')
->execute();
$command = Yii::$app->db->createCommand('UPDATE test SET status=1 WHERE id=1');
$command->execute(); |
$command = Yii::$app->db->createCommand('UPDATE test SET status=1 WHERE id=1');
$command->execute();
Kasowanie (delete):
Poprzez użycie modelu
$id = 1;
$model = Test::find($id);
$model->delete(); |
$id = 1;
$model = Test::find($id);
$model->delete();
$user = Test::findOne(2);
$user->delete(); |
$user = Test::findOne(2);
$user->delete();
Test::deleteAll('status > :status AND value = :value', [':value' => 'test', ':status' => 12]); |
Test::deleteAll('status > :status AND value = :value', [':value' => 'test', ':status' => 12]);
Poprzez użycie komend DB
Yii::$app->db->createCommand()
->delete('test ', 'status = 0')
->execute(); |
Yii::$app->db->createCommand()
->delete('test ', 'status = 0')
->execute();
Yii::$app->db->createCommand('DELETE FROM test WHERE id=1')
->execute(); |
Yii::$app->db->createCommand('DELETE FROM test WHERE id=1')
->execute();
Poprzez użycie komend PDO
$model = $connection->createCommand('DELETE FROM test WHERE id=:id');
$model->bindParam(':id', $id);
// delete row 1
$id = 1;
$model->execute();
// delete row 2
$id = 2;
$model->execute(); |
$model = $connection->createCommand('DELETE FROM test WHERE id=:id');
$model->bindParam(':id', $id);
// delete row 1
$id = 1;
$model->execute();
// delete row 2
$id = 2;
$model->execute();
Możliwość komentowania jest wyłączona.