Symfony : Create a queryBuilder between two entities without mapping - doctrine-orm

I have two entities (there is no relation between the two entities): A: Relation and B: Post.
I get every post from the current user to display them on the homepage with this request:
public function getPostsCurrentUser($currentUser)
{
$qb = $this->createQueryBuilder('p');
$qb->where('p.member = :member')
->setParameters(['member' => $currentUser])
->orderBy('p.created', 'DESC');
return $qb->getQuery()->getResult();
}
In the example "A: Relation" the user 2 follow the user 1. When a user follow an other user, I would like to also display on the homepage every post of users that the current user are following.
I don't see how to create this request. Can anybody help me ?
Thanks

In the sense of http://sqlfiddle.com/#!9/c5a0bf/2, I would suggest something like the following which uses a subquery to select the ids of all followed users:
class PostRepository {
public function getPostsOfUser($id) {
$query = $this->getEntityManager()->createQuery(
"SELECT p.content
FROM AppBundle:Post p
WHERE p.member = :id
OR p.memberId IN (
SELECT r.friend
FROM AppBundle:Relation r
WHERE r.user = :id
)
ORDER BY p.created DESC"
);
return $query->setParameter('id', $id)
->getResult();
}
}

Related

Complex QueryBuilder expresion in createQuery() with Sonata Admin

I'm using Symfony 4.2 and Sonata Admin bundle and I'm trying to display a specific list in one of my admin list view.
I want to display the lastest user by group. I created the SQL query and I'm now trying to translate it to QueryBuilder.
SELECT * FROM users
WHERE (created_at, group_id) IN (
SELECT MAX(created_at), group_id
FROM users
GROUP BY group_id)
My createQuery function in my Sonata Admin class :
public function createQuery($context = 'list')
{
$container = $this->getConfigurationPool()->getContainer();
$em = $container->get('doctrine.orm.entity_manager');
$expr = $em->getExpressionBuilder();
$subQuery = $em->createQueryBuilder()
->select('MAX(c.createdAt), c.group')
->from('ApiBundle:Configuration', 'c')
->groupBy('c.group')
->getDQL();
$query = parent::createQuery($context);
$alias = $query->getRootAlias();
$query->where($expr->in('('.$alias.'.createdAt, '.$alias.'.group)', $subQuery));
}
Unfortunately it is not working and I'm not sure it's the right way to do it.
I could make it work with only selecting the createdAt in the in() expression but it's not what I want to have at the end.
Here is the error I got.
[Syntax Error] line 0, col 77: Error: Expected Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS, got ','
How can I make it work using QueryBuilder ?
Thank you.

Laravel Scout: only search in specific fields

Laravel Scout: Is there a way that I search in only a specific field?
At the moment this line is working fine:
$es = Element::search($q)->get();
But it searches title, shortdescription and description fields. I need it to only search in title field.
You just need to change your toSearchableArray method from your model by adding the following code:
/**
* Get the indexable data array for the model.
*
* #return array
*/
public function toSearchableArray()
{
$array = $this->only('title', 'description');
$related = $this->user->only('name', 'email');
// Customize array...
return array_merge($array, $related);
}
Then call php artisan scout:import "App\YourModel" to reindex the new records. 
Note:
$this->only('title', 'description') will search only for its title and description fields
$this->user->only('name', 'email') will also search for its name and email from a related Model
So you can retrieve the related data by adding ->load('user') in your search method like the following code:
public function search(Request $request)
{
$query = $request->get('q');
return Task::search($query)->get()->load('user');
}
UPDATE
If you're trying to retrieve the data using ->paginate() method, you must need to load the relations separately:
...
$tasks = Task::search($query)->paginate($request->get('per_page'));
$tasks->load('user');
return $tasks;
Enjoy!
for meilisearch this worked
Element::search('test',
function ($searchEngine, string $query, array $options) use ($filter) {
$searchEngine->resetSearchableAttributes();
$searchEngine->updateSearchableAttributes(['field_name']);
return $searchEngine->search($query, $options);
}
)
You can do that by adding a callback function to the scout builder instance,
Person::search($searchString)->query(function($query) {
$query->select(['title']);
})->get();
You can do that by adding a callback function to the scout builder instance,
Person::search($searchString)->query(function($query) {
$query->addSelect(['title']);
})->get();
Worked on laravel 7
If you want standard query result, but search only in a specific column (field), you can try this solution:
Element::search($query)->rule(function($builder) {
return [
'must' => [
'match' => [
'some_column_name' => $builder->query
]
]
];
});
Tested on Laravel 6, but I think it will work on later versions to...

Doctrine : Translatable won't deliver translations for entities linked by #ManyToOne (or others associations)

Say you have a Category entity :
class Category
{
/**
* #Gedmo\Translatable
*/
private $categoryName;
}
And a Module entity.
Each Module belongs to one Category :
class Module
{
/**
* #Gedmo\Translatable
*/
private $moduleName;
/**
* #ManyToOne(targetEntity="Module")
*/
private $category;
}
Using the query hint I can get a Module translated to a specific locale :
function getModule($id, $locale)
{
$query = $em->createQuery('select m from AppBundle\Entity\Module m where m.id=' . $id);
$query->setHint(TranslatableListener::HINT_TRANSLATABLE_LOCALE, $locale);
$query->setHint(
\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER,
'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker'
);
$module = $query->getSingleResult();
return $module;
}
Testing :
$module = getModule(1234, 'en');
// SUCCEED
$this->assertEquals('module name in english', $module->getModuleName();
$category = $module->getCategory();
// FAIL !
$this->assertEquals('category name in english', $category->getModuleName();
// SUCCEED (but one more request)
$category->setTranslatableLocale('en');
$em->refresh($category);
$this->assertEquals('category name in english', $category->getModuleName();
In few words : the query hint is able to insert a JOIN to the SELECT statement for the Module entity.
Call to $module->getCategory() will trigger lazy loading. But this time, there will be no translation JOINed.
Meaning I have to explicitly ask for the english version of the category.
Question : is it possible to have associated entities translated when querying the owner entity ? (having the FAILed test to succeed)
I already tried eager fetching on the ManyToOne association. Won't work because I am querying through DQL.
UPDATE :
To make this specific example to work (and have a better code) I can JOIN the Category in my DQL :
select m,c from Module join m.category where m.id=id
This will load the module and its category with the correct translations.
But in a perfect world I would love to have a generic code like this one :
protected function getEntityById($className, $id, $locale = null)
{
$dql = 'select e from AppBundle\Entity\\' . $className . ' e where e.id=' . $id;
$query = $this->em->createQuery($dql);
$query->setHint(
\Gedmo\Translatable\TranslatableListener::HINT_TRANSLATABLE_LOCALE,
$locale
);
$query->setHint(
\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER,
'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker'
);
$entity = $query->getSingleResult();
return $entity;
}
How to update this piece of code to have the associated entities to be loaded with the correct locale ?
I'm afraid there is no way to affect the behaviour of $module->getCategory(). Association's loading can only be affected by using doctrine filters. Unfortunately these filters can only add to WHERE part of request, so it is not possible to add join.
I'd create a function in category repository to get category with the proper translation like you did in the getModule(). I don't think there is another way. Ex:
$category = $categoryRepository->findByModule18n($module, $locale);

Doctrine Query builder for simple search

Hi I am trying to write a simple search query using QueryBuilder. I have a single table with fields: Name, Description, Code.
Basically I want to check if an entered keyword is in any of the fields.
public function searchProducts( $keyword )
{
$qb = $this->productRepository->createQueryBuilder('u');
$qb->add('where' , 'u.name LIKE :search');
$qb->add('where' , 'u.description LIKE :search');
$qb->add('where' , 'u.code LIKE :search');
$qb->setParameter('search', '%'.$keyword.'%');
}
How do I add the orX context to this?
public function searchProducts( $keyword )
{
$qb = $this->productRepository->createQueryBuilder('u');
$qb->add('where', $qb->expr()->orX(
$qb->expr()->like('u.name', ':search'),
$qb->expr()->like('u.description', ':search'),
$qb->expr()->like('u.code', ':search')
))
$qb->setParameter('search', '%'.$keyword.'%');
}
I think this should work for you.
Additional I'm not sure, but isnt there a select/from missing like this:
$qb->add('select', 'u')
->add('from', 'User u')
I have taken it from the doctrine2 ORM Doc:
Docu

Symfony 2 & Doctrine 2: Complicated relation beetwen entities

I have two entities. First called "Status":
<?php
class Status {
protected $id;
protected $type = null; // standarized type of status (f.e. "locked", "disabled")
protected $value = true; // true or false (value of status)
protected $change_reason = null;
protected $changed_by;
protected $changed_at;
}
I've cleared annotations for better readability.
And the second called eg. Account. Because Account is not the only one entity using Statuses, relations beetwen Status and any other "statusable" entity (I think) should be many-to-many. For Account there would be join table account_status etc.
Additionaly one status belongs to only one Entity.
It all works in that configuration, but I really don't know how to retrieve a list of accounts with their latest statuses.
I wrote an SQL query to retrieve actual statuses:
SELECT * FROM (
SELECT t.type, t.value
FROM status AS t
ORDER BY t.changed_at DESC
) AS t1 GROUP BY t1.type
My questions are:
Is that idea correct at all?
How to retrieve a list of accounts with all their latest statuses?
Sorry for my poor English.
EDIT:
I want just to get an account, join its latest statuses, and then get them simply by: $task -> getStatus('highlighted') to get a value of a latest (youngest) status of type "highlighted"
EDIT2:
ideal would be still have ability to sort and filter by status of given type
class Task {
// this is list of all statuses
protected $statusHistory;
protected $lastStatus;
public function __construct() {
$this->statusHistory = new Arraycollection();
}
public function addStatus($status) {
$this->statusHistory[] = $status;
$this->lastStatus = $status;
}
public function getStatusHistory() {
return $this->statusHistory;
}
public function getLastStatus() {
return $this->lastStatus;
}
}
// to get list of statuses
$task->getStatusHistory();
// to get last status, it returns Status object, not collection
$task->getLastStatus();
It's more or less standard approach when you need first/last element from collection and getting the whole collection might be an overhead.