Find us on facebook

Feb 25, 2015

Yii2 basic commands

Save data
$member = new Member;
$member->name = 'John';
$member->email = 'john@gmail.com';
$member->save();  // equivalent to $member->insert();

Select *
$model = $connection->createCommand('SELECT * FROM member');
$members = $model->queryAll();

Select One row
$model = $connection->createCommand("SELECT * FROM member where member_id=$id");
$member = $model->queryOne();

Query Column
$model = $connection->createCommand('SELECT name FROM member');
$members = $model->queryColumn();

JSON format return
$items = ['book1', 'book2', 'book3', 'book4'];
\Yii::$app->response->format = 'json';
return $items;

OR

$items = ['book1', 'book2', 'book3', 'book4'];
return \yii\helpers\Json::encode($items);

No comments:

Post a Comment