Can someone assist me with this, I have a student table and I want to ensure that the user enters a date range from now to 3 months. I tried this but it didn't give me the results I wanted. Thinking about using datediff but not sure where I would put it. Is there another way like using a custom validation to validate the date.
* #Assert\Range
* (
* min= "today", max="+3 months"
* )
Error message:
This value should be a valid number.
#Assert\Callback(methods={"validatePayrollPeriod"})
public function validatePayrollPeriod(ExecutionContextInterface $context) {
$days = $this->startdate->diff($this->enddate)->days;
if ($days <= 7 || $days > 14) {
$context->buildViolation('Not a valid payroll period.')
->atPath('enddate')
->addViolation();
}
}
You could try to make a validation in your setter.
Something like
public function setEndDate($endDate)
{
if(your end date - your start date < 3){
$this->endDate= $endDate;
}else{
Throw whatever exepection you cant
}
return $this;
}
My question is about Faker.
I need to add one hour to a created date
Like this :
MyEntity:
startAt: '<dateTimeBetween("- 10 days", "now")>'
endAt: $startAt + 1 hour
Thx for your help.
Well that does not exist yet. Need to create a custom provider
Like this :
public static function manipulateTime($date, $action = '-30 years')
{
$date = new \DateTime($date->format('Y-m-d H:i:s'));
$date->modify($action);
return $date;
}
I need to query Statistics by month and/or year but I get this error:
Error: Expected known function, got 'YEAR'
My code:
public function findByMonthYear($month, $year)
{
$qb = $this->createQueryBuilder('p')
->where('YEAR(p.date) = :year')
->andWhere('MONTH(p.date) = :month');
$qb->setParameter('year', $year)
->setParameter('month', $month);
return $qb->getQuery()->getOneOrNullResult();
}
With some research it shows that dql doesn't have YEAR() function so it's fare...http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/cookbook/working-with-datetime.html
In the end, I'm not stick on the solution. So if you've got a clean one for the same result I'll take it as an answer.
A colleague told me that a solution as the following one may works:
where('p.date = :date')
setParameter('date', ''.$year.'-'.$month.'-%') // 2016-07-%
No success so far.
public function findByMonthYear($month, $year)
{
$fromTime = new \DateTime($year . '-' . $month . '-01');
$toTime = new \DateTime($fromTime->format('Y-m-d') . ' first day of next month');
$qb = $this->createQueryBuilder('p')
->where('p.date >= :fromTime')
->andWhere('p.date < :toTime');
->setParameter('fromTime', $fromTime)
->setParameter('toTime', $toTime);
return $qb->getQuery()->getOneOrNullResult();
}
I am trying to perform a left outer join for objects called Data, by putting a condition (status = 3) on the join table JoinData and then left joining the Data.
class JoinDataRepository extends EntityRepository
{
public function getDataWithStatusEqualsThree()
{
$qb = $this->createQueryBuilder('jd')
->select('jd','d')
->where('jd.status = 3')
->leftJoin('jd.data', 'd')
->addOrderBy('d.created', 'DESC');
return $qb->getQuery()
->getResult();
}
}
The result looks like this - having attributes of the two objects selected 1) the JoinData and 2) the Data attributes (which i actually would like to have as object):
SELECT
n0_.status AS status0
...
n1.attribute AS attribute0
...
FROM
ngl2_join_data n0_
LEFT JOIN ngl2_data n1_ ON n0_.data_id = n1_.id
WHERE
n0_.status = 2
ORDER BY
n1_.created DESC
The method returns a list of JoinData objects. Is it possible to return just Data objects? Or is there a workaround just using the QueryManager...?
What about this:
class JoinDataRepository extends EntityRepository
{
public function getDataWithStatusEqualsThree()
{
$qb = $this->createQueryBuilder('jd')
->select('d')
->where('jd.status = 3')
->leftJoin('jd.data', 'd')
->addOrderBy('d.created', 'DESC');
return $qb->getQuery()
->getResult();
}
}
The ASP.NET calendar always displays 6 weeks of dates in a 7x6 grid. My problem is that the first day of the target month does not necessarily appear in the first row... in some cases, the entire first row displays dates from the previous month. In other cases, the entire last row displays dates from the next row.
Is there a reliable way to query the calendar object to determine the 42-day range that would be rendered for a specific month/year?
For example, consider June 2008 and Feb 2009:
Notice that the first week contains ONLY dates from prior month http://img371.imageshack.us/img371/2290/datesmq5.png
I assume that the calendar tries to avoid bunching all of the "other month" dates at either the top or bottom of the grid, and therefore puts the first of the target month on the 2nd row. I am looking for an easy way to determine that the displayed range for June 2008 is May 25 - July 5, for instance.
Looking at the public members exposed by the ASP.NET Calendar control I do not believe that this information is something that you can just get from the calendar control.
You have a few options as "workarounds" to this though, although not nice....but they would work.
You could manually calculate the first week values
You can handle the "day render" event to handle the binding of the individual days, and record min/max values.
Granted neither is elegant, but AFAIK it is the only real option
Edit
After discussion in the comments, another option is a modified version of my second option above. Basically the first time Day Render is called, get the block of data for the next 42 days, then you can simply search the list for the proper day value to display on future calls to DayRender, avoiding a DB hit for each day. Doing this is another "non-elegant" solution, but it works, and reduces a bit of load on the DB, but introduces some overhead on the application side.
It will be important here to define well structured page level properties to hold the items during the binding events, but to ensure that if a month changed, etc that it wasn't loaded incorrectly etc.
I wrote a couple of methods to help with this. Just pass in Calendar.VisibleDate:
public static DateTime GetFirstDateOfMonth(DateTime date)
{
return new DateTime(date.Year, date.Month, 1);
}
public static DateTime GetFirstDisplayedDate(DateTime date)
{
date = GetFirstDateOfMonth(date);
return date.DayOfWeek == DayOfWeek.Sunday ? date.AddDays(-7) : date.AddDays((int)date.DayOfWeek * -1);
}
public static List<DateTime> GetDisplayedDates(DateTime date)
{
date = GetFirstDisplayedDate(date);
List<DateTime> dates = new List<DateTime>();
for (int i = 0; i < 42; i++)
{
dates.Add(date.AddDays(i));
}
return dates;
}
I've just been looking into this myself, and got directed to here. I'm personally tempted to go with option two, because the alternative is messy. Ronnie's version is nice, but unfortunately doesn't take into account cultures with different FirstDayOfWeeks.
Using Reflector, we can see how it's done internally:
...
DateTime visibleDate = this.EffectiveVisibleDate();
DateTime firstDay = this.FirstCalendarDay(visibleDate);
...
private System.Globalization.Calendar threadCalendar =
DateTimeFormatInfo.CurrentInfo.Calendar;
private DateTime EffectiveVisibleDate()
{
DateTime visibleDate = this.VisibleDate;
if (visibleDate.Equals(DateTime.MinValue))
{
visibleDate = this.TodaysDate;
}
if (this.IsMinSupportedYearMonth(visibleDate))
{
return this.minSupportedDate;
}
return this.threadCalendar.AddDays(visibleDate,
-(this.threadCalendar.GetDayOfMonth(visibleDate) - 1));
}
private DateTime FirstCalendarDay(DateTime visibleDate)
{
DateTime date = visibleDate;
if (this.IsMinSupportedYearMonth(date))
{
return date;
}
int num = ((int)
this.threadCalendar.GetDayOfWeek(date)) - this.NumericFirstDayOfWeek();
if (num <= 0)
{
num += 7;
}
return this.threadCalendar.AddDays(date, -num);
}
private int NumericFirstDayOfWeek()
{
if (this.FirstDayOfWeek != FirstDayOfWeek.Default)
{
return (int) this.FirstDayOfWeek;
}
return (int) DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek;
}
private bool IsMinSupportedYearMonth(DateTime date)
{
return this.IsTheSameYearMonth(this.minSupportedDate, date);
}
private bool IsTheSameYearMonth(DateTime date1, DateTime date2)
{
return (((this.threadCalendar.GetEra(date1) ==
this.threadCalendar.GetEra(date2)) &&
(this.threadCalendar.GetYear(date1) ==
this.threadCalendar.GetYear(date2))) &&
(this.threadCalendar.GetMonth(date1) ==
this.threadCalendar.GetMonth(date2)));
}
Sadly, the functionality is already there, we just can't get at it!
Mitchel,
Worked perfectly, thank you.
Started with a public variable
bool m_FirstDay = false
in the day_render function
if(m_FirstDay == false)
{
DateTime firstDate;
DateTime lastDate;
firstDate = e.Day.Date;
lastDate = firstDate.AddDays(41);
m_FirstDay = true;
}
I then had the visible date range of the asp.net calendar control. Thanks again.
see this one.
How to Remove the Last Week Of a Calendar