miércoles, 10 de agosto de 2016

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'],
],
)); ?>

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();
}

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();

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();

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
)

Traer varios registros de la base de datos

$users = \Yii::$app->db ->createCommand('SELECT * FROM user;')
->queryAll();
After running this command, our $users variable will be populated  with an array of users:
Array
(
[0] => Array
(
[id] => 1
[email] => test@example.com
[password] => test123
[first_name] => test
[last_name] => user
[created_at] => 0
[updated_at] => 0
)
)

Traer un registro de la base de datos

$user = \Yii::$app->db ->createCommand('SELECT * FROM user WHERE id= 1;')
->queryOne();
The queryOne() method returns an array of data for a single element. In the event that no data is found, this method will return false :
Array
(
[id] => 1
[email] => test@example.com
[password] => test123
[first_name] => test
[last_name] => user
[created_at] => 0
[updated_at] => 0
)