<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'user.username',
[
'attribute' => 'live',
'header' => 'Live?',
'value'=>function($data) {
return ($data->live == 1) ? "Live" : "Draft";
},
'filter' => ['0' => 'Draft', '1' => 'Live'],
],
'title',
[
'header' => 'Snippet',
'value'=>function($data) {
return substr($data->content, 0, 100);
290CHAPTER 12. WORKING WITH WIDGETS
},
'format'=>'html'
],
// 'date_updated',
// 'date_published',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
miércoles, 10 de agosto de 2016
Giltros en gridview
# views/user/index.php
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'username',
'email:email',
[
'attribute' => 'type',
'filter' => ['public' => 'Public', 'author' => 'Author',
'admin' => 'Admin'],
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'username',
'email:email',
[
'attribute' => 'type',
'filter' => ['public' => 'Public', 'author' => 'Author',
'admin' => 'Admin'],
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
Grid en yii2
# views/page/index.php
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'user.username',
[
'header' => 'Live?',
'value'=>function($data) {
return ($data->live == 1) ? "Live" : "Draft";
}
],
'title',
[
'header' => 'Snippet',
'value'=>function($data) {
return substr($data->content, 0, 100);
},
'format' => 'html'
],
'content:html',
// 'date_updated',
// 'date_published',
['class' => 'yii\grid\ActionColumn'],
],
)); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'user.username',
[
'header' => 'Live?',
'value'=>function($data) {
return ($data->live == 1) ? "Live" : "Draft";
}
],
'title',
[
'header' => 'Snippet',
'value'=>function($data) {
return substr($data->content, 0, 100);
},
'format' => 'html'
],
'content:html',
// 'date_updated',
// 'date_published',
['class' => 'yii\grid\ActionColumn'],
],
)); ?>
martes, 9 de agosto de 2016
Transactions
$transaction = \Yii::$app->db->beginTransaction();
try {
\Yii::$app->db->createCommand($sql)->execute();
\Yii::$app->db->createCommand($sql)->execute();
//[ ... more queries ...]
$transaction->commit();
} catch (\Exception $e) {
$transaction->rollBack();
}
try {
\Yii::$app->db->createCommand($sql)->execute();
\Yii::$app->db->createCommand($sql)->execute();
//[ ... more queries ...]
$transaction->commit();
} catch (\Exception $e) {
$transaction->rollBack();
}
ejecutar querys con parametros
\Yii::$app->db ->createCommand("UPDATE user SET first_name = 'Tom' WHERE id = " . $_GET['id']) ->execute();
\Yii::$app->db ->createCommand("UPDATE user SET first_name = :name WHERE id = :id)
->bindValue(':name', 'Tom')
->bindValue(':id', $_GET['id'])
->execute();
Alternatively, we can use the bindValues() method to bind several parameters into a
single call:
\Yii::$app->db ->createCommand("UPDATE user SET first_name = :name WHERE id = :id)
->bindValues([ ':name' => 'Tom', ':id' => $_GET['id'] ])
->execute();
For convenience, the previous query can be rewritten so that the parameters are in line
with the createCommand() method:
$params = [ ':name' => 'Tom', ':id' => $_GET['id'] ];
\Yii::$app->db ->createCommand("UPDATE user SET first_name = :name WHERE id = :id, $params) ->execute();
\Yii::$app->db ->createCommand("UPDATE user SET first_name = :name WHERE id = :id)
->bindValue(':name', 'Tom')
->bindValue(':id', $_GET['id'])
->execute();
Alternatively, we can use the bindValues() method to bind several parameters into a
single call:
\Yii::$app->db ->createCommand("UPDATE user SET first_name = :name WHERE id = :id)
->bindValues([ ':name' => 'Tom', ':id' => $_GET['id'] ])
->execute();
For convenience, the previous query can be rewritten so that the parameters are in line
with the createCommand() method:
$params = [ ':name' => 'Tom', ':id' => $_GET['id'] ];
\Yii::$app->db ->createCommand("UPDATE user SET first_name = :name WHERE id = :id, $params) ->execute();
Ejecutando querys deinsert y update
\Yii::$app->db ->createCommand('INSERT INTO user (email, password) VALUES
("test3@example.com", "test3");') ->execute();
// INSERT ( tablename, [ attributes => attr ] )
\Yii::$app->db
->createCommand()
->insert('user', [
'email' => 'test4@example.com',
'password' => 'changeme7',
'first_name' => 'Test',
'last_name' => 'User',
'created_at' => time(),
'updated_at' => time()
])
->execute();
// DELETE ( tablename, condition )
\Yii::$app->db
->createCommand()
->delete('user', 'id = 3')
// batchInsert( tablename, [ properties ], [ rows ] )
\Yii::$app->db
->createCommand()
->batchInsert('user', ['email', 'password', 'first_name', 'last_name',
'created_at', 'updated_at'],
[
['james.franklin@example.com', 'changeme7', 'James', 'Franklin',
time(), time()],
['linda.marks@example.com', 'changeme7', 'Linda', 'Marks', time(),
time()]
['roger.martin@example.com', 'changeme7', 'Roger', 'Martin',
time(), time()]
])
->execute();
("test3@example.com", "test3");') ->execute();
// INSERT ( tablename, [ attributes => attr ] )
\Yii::$app->db
->createCommand()
->insert('user', [
'email' => 'test4@example.com',
'password' => 'changeme7',
'first_name' => 'Test',
'last_name' => 'User',
'created_at' => time(),
'updated_at' => time()
])
->execute();
// DELETE ( tablename, condition )
\Yii::$app->db
->createCommand()
->delete('user', 'id = 3')
// batchInsert( tablename, [ properties ], [ rows ] )
\Yii::$app->db
->createCommand()
->batchInsert('user', ['email', 'password', 'first_name', 'last_name',
'created_at', 'updated_at'],
[
['james.franklin@example.com', 'changeme7', 'James', 'Franklin',
time(), time()],
['linda.marks@example.com', 'changeme7', 'Linda', 'Marks', time(),
time()]
['roger.martin@example.com', 'changeme7', 'Roger', 'Martin',
time(), time()]
])
->execute();
uso de queryColumn en yii2
$user = \Yii::$app->db->createCommand('SELECT email FROM user;')
->queryColumn();
Like queryAll() , queryColumn() will return an array of results:
Array
(
[0] => test@example.com
)
->queryColumn();
Like queryAll() , queryColumn() will return an array of results:
Array
(
[0] => test@example.com
)
Suscribirse a:
Entradas (Atom)