Use DB expression or PHP datetime as timestamp? - yii2

I have a timestamp behavior (yii2) that looks like this (taken from user guide example)...
public function behaviors()
{
return [
'timestamp' => [
'class' => TimestampBehavior::className(),
'value' => new Expression('NOW()'),
],
];
}
But is it not better to use a PHP DateTime expression like...
'value' => (new /DateTime('NOW'))->format('Y-m-d H:i:s')
Seems to me like it would be better to work with one clock rather than possibly two (in case the database server is separate and time not in sync, which is not unlikely.) Especially if I'm using DateTime in my code to check other conditions.
Which approach is better and why?

Expression class created a DBMS function inside the SQL query, it works no matter which date type (or format, it is possible to force the date format in some DBMS) you have in your column. If you change the value of a behavior you should take care the format and type of your column does match, otherwise some time you would get an error.

Related

What is andFilterWhere() condition operands purpose?

I'm absolutely new to Yii2 framework. I'm trying to learn filtering, but somehow I don't understand it. I've looked in the documentation, but it didn't helped too. Could someone explain to me what this function does step by step?
public function filteringValues($query)
{
$query->andFilterWhere([
'>',
'table1.column1',
date('Y-m-d H:i:s'),
]);
}
}
just like the doc points out, andFilterWhere applies a condition if the operands are not empty
in your particular case (since date('Y-m-d H:i:s') always returns a value),
$query->andFilterWhere(['>', 'table1.column1', date('Y-m-d H:i:s')]);
will be equivalent to
$query->andWhere(['>', 'table1.column1', date('Y-m-d H:i:s')]);
which is translated to sql to a condtion like
AND (`table1`.`column1`) > '2017-08-12 06:10:32'
a propper use case for andFilterWhere is when comparing with an optional value (received as a filter param).
$query->andFilterWhere(['>', 'table1.column1', $date]);
it's purpose is to not have to check whether $date is empty or not
$query->andFilterWhere([
'table1.column1' => $param1,
'table1.column2' => $param2,
'table1.column3' => $param3,
]);
in this example, the query applies a condtion only for the params that are not empty, ignoring the extra ones

How to compare two fields/columns in a condition?

I am having a hard time trying to figure out how to get a sub-query working.
Imagine I have:
$schools
->select($this->Schools)
->select([
'pupilcount' => $this->Pupils
->find()
->select([
$this->Pupils->find()->func()->count('*')
])
->where([
'Pupils.school_id' => 'Schools.id',
]),
The problem I am experiencing (I think) is that Schools.id is always 0 and so the count is returned as 0. I can pull out the Pupils join and it shows Pupils there.
I tried changing my code to add a:
->select(['SCID' => 'Schools.id'])
and reference that in the sub-query but doesn't work, it will always return 0 for the pupilcount.
What am I doing wrong here?
Whenever encountering query problems, check what queries are actually being generated (for example using DebugKit). Unless being an expression object, the right hand side of a condition will always be bound as a parameter, ie you're comparing against a string literal:
Pupils.school_id = 'Schools.id'
Generally for proper auto quoting compatibility, column names should be identifier expressions. While the left hand side will automatically be handled properly, the right hand side would require to be handled manually.
In your specific case you could easily utilize QueryExpression::equalFields(), which is ment for exactly what you're trying to do, comparing fields/columns:
->where(function (\Cake\Database\Expression\QueryExpression $exp, \Cake\ORM\Query $query) {
return $exp->equalFields('Pupils.school_id', 'Schools.id');
})
It's also possible to create identifier expressions manually by simply instantiating them:
->where([
'Pupils.school_id' => new \Cake\Database\Expression\IdentifierExpression('Schools.id')
])
or as of CakePHP 3.6 via the Query::identifier() method:
->where([
'Pupils.school_id' => $query->identifier('Schools.id')
])
And finally you could also always pass a single string value, which is basically inserted into the query as raw SQL, however in that case the identifiers will not be subject to automatic identifier quoting:
->where([
'Pupils.school_id = Schools.id'
])
See also
Cookbook > Database Access & ORM > Query Builder > Advanced Conditions
API > \Cake\Database\Expression\QueryExpression::equalFields()
API > \Cake\Database\Expression\IdentifierExpression

CakePHP 3 patchEntity SQL date fails

I have an array like this:
['valid_from' => '2016-02-01']
In my model I have the following validation rule
$validator->date('valid_from')->allowEmpty('valid_from');
When I try to patch the entity with the array I get this:
'valid_from' => object(Cake\I18n\FrozenDate) {
'time' => '2168-12-02T00:00:00+00:00',
'timezone' => 'UTC',
'fixedNowTime' => false
}
The column in MySQL is of the type date. (I don't want to use datetime as I don't need the time for calculations)
What am I doing wrong?
Think your issue is that your locale is misconfigured try one of these methods.
The default locale can be set in your config/bootstrap.php folder by using the following line:
ini_set('intl.default_locale', 'fr_FR');
Changing the Locale at Runtime
use Cake\I18n\I18n;
I18n::locale('de_DE');

Cakephp 3.0 I would like to include a Year input field with a drop down, but it is inputing as an array

I have a Year field in a form and I am using FormHelper.
echo $this->Form->input('year', [
'type' => 'year',
'minYear' => date('Y')-10,
'maxYear' => date('Y')
]);
The table file validator looks like:
->add('year', 'valid', ['rule' => 'numeric'])
->allowEmpty('year')
I have a very similar input in another app that seems to work fine. I set the MySql column to int(5) to match what I had working elsewhere.
Checking debugkit it shows the "year" input as an array while the other inputs are strings. If I remove the validation rule it throws an illegal array to string conversion, so I assume this is where the error is.
Any help is greatly appreciated.
I have just tested with your above code and it is working fine for me. Try to delete the cache and check it once more.
Creates a select element populated with the years from minYear to maxYear. Additionally, HTML attributes may be supplied in $options. If $options['empty'] is false, the select will not include an empty option:
empty - If true, the empty select option is shown. If a string, that
string is displayed as the empty element.
orderYear - Ordering of
year values in select options. Possible values ‘asc’, ‘desc’. Default
‘desc’ value The selected value of the input.
maxYear The max year to
appear in the select element.
minYear The min year to appear in the
select element.
Try this one:
<?php
echo $this->Form->year('exp_date', [
'minYear' => date('Y')-10,
'maxYear' => date('Y'),
'id' => 'cc-year',
'class' => 'form-control',
'empty' => false,
'orderYear' => 'asc'
]);
?>
Official Documentation: CookBook - Creating Year Inputs

CakePHP Model with "Between dates"

I have a large data set (over a billion rows). The data is partitioned in the database by date. As such, my query tool MUST specify an SQL between clause on every query, or it will have to scan every partition.. and well, it'll timeout before it ever comes back.
So.. my question is, the field in the database thats partitioned is a date..
Using CakePHP, how can I specify "between" dates in my form?
I was thinking about doing "start_date" and "end_date" in the form itself, but this may bring me two a second question.. how do I validate that in a model which is linked to a table?
If I am following you correctly:
The user must specify start/end dates for find queries generated from a form
You need to validate these dates so that, for example:
end date after start date
end date not centuries away from start date
You want validation errors appearing inline within the form (even though this isn't a save)
Since you want to validate these dates they will be harder to grab when they are tucked away inside your conditions array. I suggest trying to pass these in separately and then dealing with them later:
$this->Model->find('all', array(
'conditions' => array(/* normal conditions here */),
'dateRange' => array(
'start' => /* start_date value */,
'end' => /* end_date value */,
),
));
You should hopefully be able to handle everything else in the beforeFind filter:
public function beforeFind() {
// perform query validation
if ($queryData['dateRange']['end'] < $queryData['dateRange']['start']) {
$this->invalidate(
/* end_date field name */,
"End date must be after start date"
);
return false;
}
/* repeat for other validation */
// add between condition to query
$queryData['conditions'][] = array(
'Model.dateField BETWEEN ? AND ?' => array(
$queryData['dateRange']['start'],
$queryData['dateRange']['end'],
),
);
unset($queryData['dateRange']);
// proceed with find
return true;
}
I have not tried using Model::invalidate() during a find operation, so this might not even work. The idea is that if the form is created using FormHelper these messages should make it back next to the form fields.
Failing that, you might need to perform this validation in the controller and use Session::setFlash(). if so, you can also get rid of the beforeFind and put the BETWEEN condition array in with your other conditions.
if you want to find last 20 days data .
$this->loadModel('User');
//$this->User->recursive=-1;
$data=$this->User->find('all', array('recursive' => 0,
'fields' => array('Profile.last_name','Profile.first_name'),'limit' => 20,'order' => array('User.created DESC')));
other wise between two dates
$start = date('Y-m-d') ;
$end = date('Y-m-d', strtotime('-20 day'));
$conditions = array('User.created' =>array('Between',$start,$end));
$this->User->find("all",$conditions)
You could write a custom method in your model to search between the dates:
function findByDateRange($start,$end){
return $this->find('all',array('date >= '.$start,'data >= .'$end));
}
As far as validating, you could use the model's beforeValidate() callback to validate the two dates. More info on this here.
function beforeValidate(){
if(Validation::date($this->data['Model']['start_date'])){
return false;
}
if(Validation::date($this->data['Model']['end_date'])){
return false;
}
return parent::beforeValidate();
}
Does that answer your question?