CakePHP Model with "Between dates" - mysql

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?

Related

How do I update multiple columns with their respective values in laravel?

I have a database record that I would like to update base on certain conditions. This condition is: when the pay_day is reached, I want to send the user that owns that record an email and then update the pay_day column to another date in the future using the interval_day column on the users' table. interval_day is just a number selected by the user.
Below is an illustration:
$now = Carbon::now();
User::where('approved', true)
->where('pay_day', '<', $now)
->chunkById(1000, function($users){
foreach ($users as $user) {
$interval = $user->interval;
$payDay = $now()->addDays($interval);
// update the user...
$user->update([
'pay_day' => $payDay,
]);
// if the user was updated, send an email next...
}
});
Now, let's say I have 100 different users with different interval values. I want their respective values in their interval columns to be what would be updated to their pay_day column and NOT the same date for all 100 users.
But when I run the above query it didn't update, neither the pay_day nor send email to the respective users. When I dd($interval & $payDay) it returns nothing.
Please what am I doing wrong? I need your suggestions. Thanks for your time in advance.
use like this
$updates = ([
'pay_day' => $payDay,
//other columns
]);
$x = User::where('approved', true)
->where('pay_day', '<', $now)->->update($updates);
if($x){
//succeed case
}
Not sure if $payDay = $now()->addDays($interval); is a typo or you've actually done that in your code, but $now is a variable not a function. Additionally you need to tell the closure in your chunkById function to use $now:
\App\Models\User::where('approved', true)
->where('pay_day', '<', \Carbon\Carbon::today())
->chunkById(1000, function ($users) {
$users->each(function ($user) {
$success = $user->update([
'pay_day' => \Carbon\Carbon::today()->addDays($user->interval)
]);
if ($success) {
// send email
}
});
});
The above finds all approved Users where their pay_day is before today then processes the results in chunks of 1000 and updates the pay_day for each of them to be today + the interval value.

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

Symfony 2 / Doctrine Not Saving Any Zeros in varchar field

UPDATE
As asked for,
$NewUser = new Users();
$Form = $this->createForm(new UserType(), $NewUser, [])
->add('save', 'submit', ['label' => 'Save',
'attr' => ['class' => 'SaveButton ftLeft'],
]);
$Form->handleRequest($request);
if ($Form->isValid()) {
/*
Sometimes add more data to the entity.....
*/
$em = $this->getDoctrine()->getManager();
$em->persist( $NewUser );
$em->flush();
}
$FormRender = $Form->createView();
return $this->render('xxxxBundle:Users/add.html.twig',
['AddUser' => $FormRender]);
Sometimes I will add extra to the entity, on fields being set within the php code, e.g. account sign up date, but in most cases just save the entity with the form data.
Very simple issue, I have a new (ish) system and have notice that when trying to save zeros, for anything, phone number inputs, it does not save any zeros?
I am using YMAL files to control my doctrine ORM, here is how I have set it up within my User table,
mobileNumber:
type: string
nullable: false
length: 50
options:
fixed: false
column: mobile_number
This does save/make the field has a varchar, and thought nothing about it until I did a random test and saw it was not saving any leading zeros?
I know you can not save leading zeros in a int field type, but this is a varchar. Also when I go into the database, and manually add a zero to that input, its fine, so I take its not the database. Something to do with how I get doctrine to save the data?
If so how do I change that? I have just been saving the data with a standard persist() and flush() commands? Not sure why it would not save the zeros? I was thinking that I had to change the YMAL type to something like phone? I have been over the docs, can not see it.
All help most welcome..
Thanks.
The reason is in your comment:
User form type has the mobile number set to 'number' input.
This value is being casted to int by the Form component. Change the field type to regular string.
If you want to validate value to be only digits, use Validation component

Use DB expression or PHP datetime as timestamp?

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.

Displaying data within a date range from a rails scaffold

This is in my controller:
#somethings = Something.find(:all)
And my count variable would display a break up of this data based on 'rca' as such:
#something_to_count = Something.count( :conditions => { :rca => "4X32W"})
#something_to_count2 = Something.count( :conditions => { :rca => "6X36W"})
..... etc to other count variables...
Simple as this is, I was trying to get it to display these stats/counts for all entries made from the 'Something' table within the last thirty days in one view and within a date range in another view. I have the default timestamps added by the scaffold function (created_at and updated_at) in my model.
My view would have to be split up into two pages. One page to accept a date range and display the count variables along with the data in a table and the other page to display just the same for the last thirty days. (i.e. def last_30_days and def within_dates would be the actions). I found a solution on line which has a date variable subtracted from 30 etc which did not seem to work. A full fledged working solution would be greatly appreciated. Please help. Also, how would I convert a simple date range entry into the same format as the datestamps?
You can create scopes to your querys, so you can reuse them.
My approach to your problem would be something like this:
scope :last_30_days, where("created_at between ? and ?", Date.today - 30, Date.today)
scope :within_dates, lambda { |start_date, end_date|
where("created_at between ? and ?", start_date, end_date)
}
scope :based_on_rca, lambda { |rca|
where(:rca => rca)
}
#something_to_count = Something.based_on_rca("4X32W").last_30_days.count
#something_to_count = Something.based_on_rca("4X32W").within_dates(Date.today - 4.month, Date.today - 1.month).count
Various scopes with small queries that can be chained to reflect on a larger one.
Regards.