Symfony get className object from String class declaration - doctrine-orm

I have this code that returns column names of the className declared in the 2nd line :
public function listColumns(EntityManagerInterface $em ) {
$class = $em->getClassMetadata(Assure::class);
$fields = [];
if (!empty($class->discriminatorColumn)) {
$fields[] = $class->discriminatorColumn['name'];
}
$fields = array_merge($class->getColumnNames(), $fields);
foreach ($fields as $index => $field) {
if ($class->isInheritedField($field)) {
unset($fields[$index]);
}
}
foreach ($class->getAssociationMappings() as $name => $relation) {
if (!$class->isInheritedAssociation($name)){
foreach ($relation['joinColumns'] as $joinColumn) {
$fields[] = $joinColumn['name'];
}
}
}
return $fields;
}
I am trying to make this function parametrable so I can give it every time which table/className I am trying to get its columns

This is a possible solution to do what I want (extracting table column names ) differently :
public function listColumns2(EntityManagerInterface $em ) {
$conn = $this->getEntityManager()->getConnection();
$sql = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME =
N'Assure' ";
$stmt = $conn->prepare($sql);
$stmt->execute();
return $stmt->fetchAllAssociative();
}

Related

A new entity was found through the relationship?

I have a handle code
if ($this->request->isMethod('POST') && $valid) {
$em = $this->getDoctrine()->getManager();
$formData = $form->getData();
$staffValue = $formData['staff'];
$campaignDetailFilter = $modelCampaignDetail->getRepository()->countDataByCampaignDetailId($formData['campaignDetail']);
$total = count($campaignDetailFilter);
$totalStaff = count($formData['staff']);
foreach ($campaignDetailFilter as $valDetailId) {
$detailDataEntity = $modelDetailData->getEntity($valDetailId['id']);
$batchSize = $total / $totalStaff;
$i = 0;
foreach ($staffValue as $staffVal) {
$detailDataEntity->setStaff($staffVal);
$em->persist($detailDataEntity);
if (($i % $batchSize) === 0) {
$em->flush();
$em->clear();
}
++$i;
}
$em->flush();
$em->clear();
}
}
But when I give $i = 0 it gets an error: A new entity was found through the relationship...that was not configured to cascade persist operations for entity.
You should remove $em->clear().
if (($i % $batchSize) === 0) {
$em->flush();
}

How reuse formatted Doctrine 2 with QueryBuilder formatted query in other query

I have formatted query with doctrine query builder. It is kind of complicated because in that query there are some my own defined doctrine functions and doctrine extensions. Also very important to mention that this query using filter which is require some preselected values. The problem is when i want to count records from that query. Of course i Can write count(results), but it is very bad for performance.
I try to create new sql query with my own predefined query as source, but the problem that parameters are not injected.
Query which is formatted and which result i want to count :
private function getCategoriesQuery(Filter $filter = null, Sort $sort = null, Pagination $pagination = null)
{
$qb = $this->createQueryBuilder('c')
->select('c.id as id')
->addSelect('c.title as title')
->addSelect('DATE_FORMAT(c.updationDate, \'%Y-%m-%d %H:%i:%S\') as updationDate')
->addSelect('DATE_FORMAT(c.creationDate, \'%Y-%m-%d %H:%i:%S\') as creationDate')
->addSelect('coalesce(sum(r.amount), 0.00) as total')
->addSelect('round(AveragePerMonth(coalesce(sum(r.amount), 0.00), c.creationDate, CURRENT_DATE()), 2) as averagePerMonth')
->leftJoin('c.records', 'r')
->groupBy('c.id')
->andWhere('c.user = :user')->setParameter('user', $this->user);
if ($filter) $this->applyFilters($filter, $qb);
if ($sort) $this->applySort($sort, $qb);
if ($pagination) $this->applyPagination($pagination, $qb);
return $qb->getQuery();
}
I tried:
public function countUserCategories(Filter $filter = null)
{
$rsm = new ResultSetMapping();
$sql = $this->_em->createNativeQuery('select count(*) from ('.$this->getCategoriesQuery($filter)->getSQL().') as src', $rsm);
return $sql->getSingleScalarResult();
}
I expected the answer if there is possibility to combine these queries and if there is possiblity how i can do that. Thanks
ApplyFilter method:
private function applyFilters(Filter $filter, QueryBuilder $qb)
{
if ($filter->getCategories()) {
$qb->andWhere('c.id IN(:categories)')->setParameter('categories', $filter->getCategories());
}
if ($filter->getEndDate()) {
$qb->andWhere('r.date <= :endDate')->setParameter('endDate', $filter->getEndDate());
}
if ($filter->getStartDate()) {
$qb->andWhere('r.date >= :startDate')->setParameter('startDate', $filter->getStartDate());
}
if ($filter->getStartTotal() !== null) {
$qb->andHaving('total >= :startTotal')->setParameter('startTotal', $filter->getStartTotal());
}
if ($filter->getEndTotal() !== null) {
$qb->andHaving('total <= :endTotal')->setParameter('endTotal', $filter->getEndTotal());
}
if ($filter->getStartAveragePerMonth() !== null) {
$qb->andHaving('averagePerMonth >= :startAveragePerMonth')->setParameter('startAveragePerMonth', $filter->getStartAveragePerMonth());
}
if ($filter->getEndAveragePerMonth() !== null) {
$qb->andHaving('averagePerMonth <= :endAveragePerMonth')->setParameter('endAveragePerMonth', $filter->getEndAveragePerMonth());
}
}
Suggested way to solve this issue but return response : The query returned multiple rows. Change the query or use a different result function like getScalarResult().
public function countUserCategories(Filter $filter = null)
{
return (clone $this->getCategoriesQueryBuilder($filter))
->resetDQLPart('select')
->select('COUNT(c)')
->getQuery()
->getSingleScalarResult();
}
UPDATE
I make this work by using SQL query builder
Query builder creation
private function getCategoriesQueryBuilder(Filter $filter = null, Sort $sort = null, Pagination $pagination = null)
{
$qb = $this->_em->getConnection()->createQueryBuilder()
->select('c.id AS id')
->addSelect('c.title AS title')
->addSelect('DATE_FORMAT(c.updation_date, \'%Y-%m-%d %H:%i:%S\') AS updationDate')
->addSelect('DATE_FORMAT(c.creation_date, \'%Y-%m-%d %H:%i:%S\') AS creationDate')
->addSelect('COALESCE(SUM(r.amount), 0.00) AS total')
->addSelect('(SELECT ROUND(COALESCE(SUM(r.amount), 0.00) / COUNT(DISTINCT(DATE_FORMAT(calendar.date, \'%Y-%m\'))), 2)
FROM calendar as calendar
WHERE calendar.date BETWEEN DATE_FORMAT(c.creation_date, \'%Y-%m-%d\') AND DATE_FORMAT(CURRENT_DATE(), \'%Y-%m-%d\'))
AS averagePerMonth')
->from('category', 'c')
->leftJoin('c', 'record', 'r', 'r.category_id = c.id')
->join('c', 'users', 'u', 'u.id = c.user_id')
->where('u.id = :userId')->setParameter('userId', $this->user->getId())
->groupBy('c.id');
if ($sort) $this->applySort($sort, $qb);
if ($pagination) $this->applyPagination($pagination, $qb);
if ($filter) $this->applyFilter($filter, $qb);
return $qb;
}
Filter
private function applyFilter(Filter $filter, QueryBuilder $qb)
{
if ($filter->getCategories()) {
$qb->andWhere('c.id IN(:categories)')->setParameter('categories', $filter->getCategories());
}
if ($filter->getEndDate()) {
$qb->andWhere('r.date <= :endDate')->setParameter('endDate', $filter->getEndDate());
}
if ($filter->getStartDate()) {
$qb->andWhere('r.date >= :startDate')->setParameter('startDate', $filter->getStartDate());
}
if ($filter->getStartTotal() !== null) {
$qb->andHaving('total >= :startTotal')->setParameter('startTotal', $filter->getStartTotal());
}
if ($filter->getEndTotal() !== null) {
$qb->andHaving('total <= :endTotal')->setParameter('endTotal', $filter->getEndTotal());
}
if ($filter->getStartAveragePerMonth() !== null) {
$qb->andHaving('averagePerMonth >= :startAveragePerMonth')->setParameter('startAveragePerMonth', $filter->getStartAveragePerMonth());
}
if ($filter->getEndAveragePerMonth() !== null) {
$qb->andHaving('averagePerMonth <= :endAveragePerMonth')->setParameter('endAveragePerMonth', $filter->getEndAveragePerMonth());
}
}
Count
public function countUserCategories(Filter $filter = null)
{
$subQuery = $this->getCategoriesQueryBuilder($filter);
$params = $subQuery->getParameters();
return $this->_em->getConnection()->createQueryBuilder()
->select('COUNT(src.id)')
->from('(' . $subQuery . ')', 'src')
->setParameters($params)
->execute()
->fetchColumn(0);
}
Fetch
public function findUserCategories(Filter $filter = null, Sort $sort = null, Pagination $pagination = null)
{
return $this->getCategoriesQueryBuilder($filter, $sort, $pagination)->execute()->fetchAll();
}
You can use the original query as subquery to get count.
public function countUserCategories(Filter $filter = null)
{
$querysub = $this->getCategoriesQuery($filter);
$query = $this->getDoctrine()->getConnection()->createQueryBuilder();
$query
->select('COUNT(*)')
->from('(' . $querysub->getSQL() . ')', 'ttt');
$i = 0;
foreach ($querysub->getParameters() as $parameter) {
$query->setParameter($i++, $parameter->getValue(), $parameter->getType());
}
$count = $query->execute()->fetchColumn(0);
return $count;
}
This will work if parameters are set in order with their appearance in query.

Symfony: update many-to-many

I have Post and Tag entities with many-to-many relationship. In Post create and edit form there is a textbox where I can enter tags separated by comma relevant to that post. For example, when I enter tag1, tag2, tag3 for post with title 'Post1', the form will create post and tag entities and add these tags to tags list of that post.I use data transformer to create tag entities.
class Post{
/**
* #ORM\ManyToMany(targetEntity="Tag", mappedBy="posts",cascade={"all"})
*/
protected $tags;
public function __construct() {
$this->tags = new ArrayCollection();
}
/**
* #return ArrayCollection
*/
public function getTags()
{
return $this->tags;
}
/**
* #param Tag $tag
*/
public function addTag(Tag $tag)
{
$tag->addPost($this);
$this->tags->add($tag);
}
/**
* #param Tag $tag
*/
public function removeTag(Tag $tag)
{
$this->tags->removeElement($tag);
}
}
PostType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', TextType::class, array('label' => 'Title'))
->add('tags', TextType::class, array('label' => 'Tags'))
;
$builder->get('tags')
->addModelTransformer(new TagViewTransformer($this->manager));
}
TagViewTransformer
class TagViewTransformer implements DataTransformerInterface
{
public function transform($value)
{
/...
}
public function reverseTransform($value)
{
$tags = array();
if ( $value )
{
if( strpos($value, ',') !== false )
{
$list = array_unique(explode(",", $value));
}
else
{
$list = array(trim($value));
}
foreach ( $list AS $tagName )
{
$tag = $this->em
->getRepository('CoreBundle:Tag')
->findOneBy(array('name' => trim($tagName)));
if( !$tag )
{
$tag = new Tag();
$tag->setName(trim($tagName));
$this->em->persist($tag);
}
$tags[] = $tag;
}
}
return $tags;
}
}
This works fine when I try to create Post, all tags are transformed to entities and are added to Post's tags list. but when I try to edit, I start having problems
public function editAction(Request $request, Post $post)
{
$deleteForm = $this->createDeleteForm($post);
$editForm = $this->createForm(PostType::class, $post);
$editForm->handleRequest($request);
$originalTags = $post->getTags();
if ($editForm->isSubmitted() && $editForm->isValid()) {
$em = $this->getDoctrine()->getManager();
$newTags = $editForm->get('tags')->getData();
foreach ($originalTags as $currentTag) {
if (!in_array($currentTag, $newTags)) {
$post->removeTag($currentTag);
}
}
$em->persist($post);
$em->flush();
return $this->redirectToRoute('post_show', array('id' => $post->getId()));
}
return $this->render('AppBundle:Post:edit.html.twig', array(
'entity' => $post,
'form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
Let's say Post has tags: tag1, tag2, tag3, but I want to remove tag3 and add tag4. So I will change tags textbox to tag1, tag2, tag4. However when I submit form, I get tag1, tag2, tag3, tag4. So tag3 is not removed from Post's tag list.
What is wrong with the editAction code?
try with this
public function editAction(Request $request, Post $post)
{
$deleteForm = $this->createDeleteForm($post);
$editForm = $this->createForm(PostType::class, $post);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($post);
$em->flush();
return $this->redirectToRoute('post_show', array('id' => $post->getId()));
}
return $this->render('AppBundle:Post:edit.html.twig', array(
'entity' => $post,
'form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
Use Orphan removal to do this :
class Post
{
/**
* #ORM\ManyToMany(targetEntity="Tag", mappedBy="posts", cascade={"all"}, orphanRemoval=true)
*/
protected $tags;
/* Rest of your class */
}
Do some tests, maybe cascade all is not necessary here.

how to add product to cart in opencart

Below is add to product code . But I am not getting where the values are storing . Kindly help to find out solution for this . I want to know logic behind this code
public function add($product_id, $qty = 1, $option = array(), $recurring_id = 0) {
$this->data = array();
$product['product_id'] = (int)$product_id;
if ($option) {
$product['option'] = $option;
}
if ($recurring_id) {
$product['recurring_id'] = (int)$recurring_id;
}
$key = base64_encode(serialize($product));
if ((int)$qty && ((int)$qty > 0)) {
if (!isset($this->session->data['cart'][$key])) {
$this->session->data['cart'][$key] = (int)$qty;
} else {
$this->session->data['cart'][$key] += (int)$qty;
}
}
}
The product details with options are stored in $key = base64_encode(serialize($product));. Where $this->session->data['cart'][$key] contains the number of quantity added by the customer.
For more details check the getProducts() function on the same page. Where you can find
foreach ($this->session->data['cart'] as $key => $quantity) {
....
$product = unserialize(base64_decode($key));
....
}

Group Events by Week Commencing in Eloquent

I have the following method in my Event model for my events:
public static function getEvents($perPage = 10)
{
$events = Event::where('active', '=', 1)->paginate($perPage);
return $events;
}
Each event has a start_date and I need to take that start date and group the results in the view by week commencing, as in the Monday of the week each event occurs. I have this in my controller:
public function index()
{
$events = Event::getEvents($perPage = 5);
$this->layout->content = View::make('events.index')->with('events', $events);
}
Can anyone help me out with grouping these events by week commencing? Thanks.
I'm part way there in terms of getting the week commencing date using:
foreach ($events as $event)
{
$start_date = DateTime::createFromFormat('Y-m-d', $event->start_date);
$week = $start_date->format('YW');
$monday = 1;
$day_offset = ($monday - $start_date->format('N'));
$week_commencing = $start_date->modify("$day_offset days");
$week_commencing = $week_commencing->format('jS F Y');
}
Now I just need to use that to group the events but I'm not sure how to.
EDIT: I feel I'm getting closer to a solution but still need a little help. I now have the below in my controller, and it does print out 1 week commencing then all the events but when var_dump(ing) the $events->weeks_commencing there is only 1 date in the object, so I'm nearly there. Any pointers guys?:
foreach ($events as $event)
{
$start_date = DateTime::createFromFormat('Y-m-d', $event->start_date);
$week = $start_date->format('YW');
$monday = 1;
$day_offset = ($monday - $start_date->format('N'));
$week_commencing = $start_date->modify("$day_offset days");
if (!array_key_exists($week, $events))
{
$events->weeks_commencing = (object) array(
'week' => $week_commencing->format('jS F Y')
);
}
}
EDIT: OK I now have this in my controller. It's getting closer but not quite right yet.
foreach ($events as $event)
{
$start_date = DateTime::createFromFormat('Y-m-d', $event->start_date);
$week = $start_date->format('YW');
$monday = 1;
$day_offset = ($monday - $start_date->format('N'));
$week_commencing = $start_date->modify("$day_offset days");
if (array_key_exists($week, $events))
{
$events = (object) array(
'week' => $week_commencing->format('jS F Y'),
'events' => array()
);
}
$events->events[] = $event;
}
OK, so I've solved my issue now, with the following code:
public function index()
{
$events = Event::getEvents($perPage = 10);
$events_by_week = array();
foreach ($events as $event)
{
$start_date = DateTime::createFromFormat('Y-m-d', $event->start_date);
$week = $start_date->format('YW');
$monday = 1;
$day_offset = ($monday - $start_date->format('N'));
$week_commencing = $start_date->modify("$day_offset days");
if (!array_key_exists($week, $events_by_week))
{
$events_by_week[$week] = (object) array(
'week' => $week_commencing->format('jS F Y'),
'events' => array()
);
}
$events_by_week[$week]->events[] = $event;
}
$this->layout->content = View::make('events.index')->with(array('events_by_week' => $events_by_week, 'events' => $events));
}