Phpactiverecord, updating NOW() for an object
-
14-11-2019 - |
문제
I've been attempting to use phpactiverecord for a project.. and I'm struggling to see if I can actually pass a mysql function through any of its native updating methods. I'm a little surprised I couldn't find an answer to this question anywhere else =P.
EDIT:
After a year of using the framework, a simple answer is to simply do:
$object->date = 'now'
해결책
If I understand correctly you want to call NOW()
to update a timestamp field. There are several options.
use an "updated_at" field: http://www.phpactiverecord.org/projects/main/wiki/Utilities#automatic-timestamps
Models with fields named created_at and updated_at will have those fields automatically updated upon model creation and model updates.
Do some sort of update on a callback: http://www.phpactiverecord.org/projects/main/wiki/Callbacks
Simply update it using a php function manually
$obj->yourDateField = new \DateTime();
다른 팁
While I could be off base, but I think that directly you don't pass sql into active record. However my understanding of active record (from working with Yii's version of active record) is that active record is built upon a CDbConnection, from the class CActiveRecord:
/** * @var CDbConnection the default database connection for all active record classes. * By default, this is the 'db' application component. * @see getDbConnection */
in Yii to access this you can use the code:
$sql = 'select * from mytable';
$rows = Yii::app()->db->createCommand($sql)->queryAll();
if you aren't using Yii you'll have to lookup how to access the db object.