So, I must say that I am completely new to symfony, i’ve been using it for a total of about 4 days now but I’ve come quite far in my knowledge thus far…
I was recently put onto a project to add a new feature, so I checked out the repo via git and created the new entity and the crud to begin working on it… It’s worth noting that this system is using the PDO MySQL drivers.
In my doctrine config yml for my entity I have:
Ecs\CrmBundle\Entity\TimeClock:
type: entity
table: null
fields:
id:
type: integer
id: true
generator:
strategy: AUTO
in1:
type: datetime
out1:
type: datetime
in2:
type: datetime
out2:
type: datetime
totaltime:
type: string
daydate:
type: datetime
manyToOne:
noteBy:
targetEntity: Ecs\AgentManagerBundle\Entity\User
lifecycleCallbacks: { }
This is for a timeclock for our employees to use… Then in my timeclock controller, in the index() function what I need to do is search the database for entries that are this payperiod but only for the agent that is logged in…
So lets say for example that i’ve already written the code for $start and $end that will hold dates in the datetime format… so like:
$start = "2012-03-24 00:00:00" and $end = "2012-03-31 23:59:59" respectively..
The code I currently have that is pulling all entries for the agent logged in is:
public function indexAction()
{
$em = $this->getDoctrine()->getEntityManager();
$user = $this->get('security.context')->getToken()->getUser();
// date format: YYYY-MM-DD HH:MM:SS
$entities = $em->getRepository('EcsCrmBundle:TimeClock')->findBy(array('noteBy' => $user->getid()));
return $this->render('EcsCrmBundle:TimeClock:index.html.twig', array(
'entities' => $entities,
));
}
So, how do I expand upon $entities to include narrowing it down with $start and $end?
[EDIT]
I’ve done a little research and ive re-written the indexAction function a little bit..
public function indexAction()
{
$em = $this->getDoctrine()->getEntityManager();
$user = $this->get('security.context')->getToken()->getUser();
// date format: YYYY-MM-DD HH:MM:SS
$today = time();
if (date('l') == "Saturday") {
$end = date('Y-m-d ')."23:59:59";
} else {
$end = date('Y-m-d ', strtotime('next_saturday', $today))."23:59:59";
}
$start = date('Y-m-d H:i:s', strtotime('last_sunday', $today));
$entities = $em->getRepository('EcsCrmBundle:TimeClock');
echo $user->getid();
$query = $entities->createQueryBuilder('tc')
->where('tc.noteBy = :user')
->where('tc.daydate <= :end')
->where('tc.daydate >= :start')
->setParameter('user', $user->getid())
->setParameter('end', $end)
->setParameter('start', $start)
->getQuery();
$entities = $query->getResult();
return $this->render('EcsCrmBundle:TimeClock:index.html.twig', array(
'entities' => $entities,
));
}
But now when I run it, it gives me this:
Invalid parameter number: number of bound variables does not match number of tokens but doesn’t tell me where the problem is… If i remove the 2 where(tc.daydate) and the corresponding setParameter functions, it returns the expected results minus the filtering on the date they were entered.. any ideas?
I figured it out after searching and searching and coming up with not much of anything… The problem, as i’m guessing was too many
wherecommands…changing the 2 for the
daydatetoandWherefixed the problem completely..