bbsbbs
- bbsbbs 赞了回答
先创建连接对象
$connection = new \yii\db\Connection([ 'dsn' => $dsn, 'username' => $username, 'password' => $password, ]); $connection->open();
查询返回多行:
$command = $connection->createCommand('SELECT * FROM post'); $posts = $command->queryAll();
返回单行:
$command = $connection->createCommand('SELECT * FROM post WHERE id=1'); $post = $command->queryOne();
查询多行单值:
$command = $connection->createCommand('SELECT title FROM post'); $titles = $command->queryColumn();
查询标量值/计算值:
$command = $connection->createCommand('SELECT COUNT(*) FROM post'); $postCount = $command->queryScalar();
要养成先查文档的习惯:
http://www.yiichina.com/doc/guide/2.0/db-dao - bbsbbs 赞了回答
我创建的时候 这两个时间一起插入数据库没有问题 但是当我修改的时候 两个时间没有变化
如果我同时给这两个时间加上值 update_at=time()和created_at=time() 数据库同时两个时间都更新了 我只想让update_at一个时间更新 怎么做
modelpublic function behaviors() { return [ [ 'class' => TimestampBehavior::className(), 'attributes' => [ ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'], ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'], ], 'value' => new Expression('NOW()'), ], ]; }
controller
$post['updated_at']=date('Y-m-d H:i:s',time()); $post['created_at']=date('Y-m-d H:i:s',time()); $news->attributes = $post; $news->save(); if ($news->hasErrors()) { return $this->redirectMessage($news->getError()); } return $this->redirect('/news');
我创建的时候 这两个时间一起插入数据库没有问题 但是当我修改的时候 两个时间没有变化
如果我同时给这两个时间加上值 update_at=time()和created_at=time() 数据库同时两个时间都更新了 我只想让update_at一个时间更新 怎么做
modelpublic function behaviors() { return [ [ 'class' => TimestampBehavior::className(), 'attributes' => [ ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'], ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'], ], 'value' => new Expression('NOW()'), ], ]; }
controller
$post['updated_at']=date('Y-m-d H:i:s',time()); $post['created_at']=date('Y-m-d H:i:s',time()); $news->attributes = $post; $news->save(); if ($news->hasErrors()) { return $this->redirectMessage($news->getError()); } return $this->redirect('/news');