I have some table in Symfony app
id
user_id
test_id
type_id
points
I need search by different pairs: user_id and test_id, user_id and type_id, test_id and type_id.
I can write methods in repository, like this for all pairs:
$hints = $this->createQueryBuilder('points')
->andWhere('IDENTITY(points.user) = :user_id')
->andWhere('IDENTITY(points.test) = :test_id')
->setParameters([
'user_id' => $user->getId(),
'test_id' => $test->getId(),
])
->getQuery()
->getResult();
But I want not to add each time new function in repository for new combination and execute searching by standard methods like $repository->findBy(['IDENTITY(points.user)' => 1]) or $repository->matching($creteria).
But in this way I get error Unrecognized field: IDENTITY(points.user)
How search by standard functions with IDENTITY?
As Skyd said in comments it could be executed by $pointsRep->findBy(['user' => 1]); or by $pointsRep->findBy(['user' => $user]);
Related
using Symfony 5 and Omines, I have 2 tables sending a request to the same url:
the first table show products sold this month,
the second table show products sold before this month
Therefore, I want to pass as a parameter a date. and then in the query_builder filter it.
I do not find any hint in the documentation.
Any help would be great.
Thanks,
add your desired variable which you want to use in querybuilder like shown in example below
$table->createAdapter(ORMAdapter::class, [
'entity' => Product::class,
'query' => function (QueryBuilder $builder) use ($date) {
$builder
->select('p')
->from(Product::class, 'p')
->where("p.date = :date")
->setParameter("date", $date)
;
},
]);
I need to find a phone number in a recording where the contract ID is not equal to current contract ID.
It is easy to find in a specific contract ID. $value is the entity instance in my custom validator.
$existingPhone = $this->contractRepository->findOneBy(['phone' => $value->getPhone(), 'contractId' => $value->getContractId()]);
but how to find in other than the current contract ID?
You need to create a method in your contractRepository, and use the Doctrine QB.
$qb = $this->createQueryBuilder('c');
$qb
->where('c.phone = :phone')
->andWhere(
$qb->expr()->neq('c.contractId', 'contractId')
)
->setParameters([
'phone' => $phone,
'contractId' => $contractId,
])
->setMaxResults(1)
->getQuery()
->getOneOrNullResult();
All,
I have a query as such:
_AddOrderBy(sortOptions, query)
.Fetch(x => x.ImageType).Eager
.Fetch(x => x.User).Eager
.Fetch(x => x.Partner).Eager
.Inner.JoinAlias(x => x.Partner, () => p).Fetch(x => x.Company).Eager
.Skip(startIndex)
.Take(pageSize)
.List<ImageRequest>();
In the above QueryOver I call _AddOrderBy() method which adds an order by clause. The challenge I face is how do I create an "order by" that references a property (ordering by "CompanyName") that lies within the following association path without conflicting with my Fetch()/Inner joins:
ImageRequest.Partner.Company.CompanyName
Inside my _AddOrderBy() I have this:
Partner p = null;
Company comp = null;
order = query.Inner.JoinAlias(x => x.Partner, () => p)
.Inner.JoinAlias(x => x.Company, () => comp)
.OrderBy(x => comp.CompanyName);
But this gives me a run time exception stating that I have duplicate key (referring to Partner) in the criteria. I can see that this is conflicting with my eager fetching.
My questions is:
How do I add an "order by" so it works with my Fetching.
The beauty of using an Alias in QueryOver is that you don't have to use Fetch or JoinAlias in your _AddOrderBy() method again if the Join happens in the query already. You only need to declare the Alias with the same name.
Therefore your _AddOrderBy() can just look like this:
Partner p = null;
Company comp = null;
order = query
.Inner.JoinAlias(x => p.Company, () => comp) // you can use p here if it was used in the query before
.OrderBy(x => comp.CompanyName);
The reason this works is this: If you put the whole code into one method it will obviously work. Splitting it into two methods still works, because Partner p is not a reference to an object in memory but an Alias that is simply translated into a string for the SQL query.
What is DQL findBy() query syntax?
Like this:
$this->getDoctrine()->getRepository("AppBundle:Users")->findBy($queryWhere);
It will create criteria for WHERE CLAUSE
$repository->findBy(['email' => 'test#test.com', 'city' => 'Berlin'])
SELECT * FROM table WHERE email = "test#test.com" AND city = "Berlin"
if you are interested, you can take a look in the method getSelectSQL under the following link
http://www.doctrine-project.org/api/orm/2.5/source-class-Doctrine.ORM.Persisters.Entity.BasicEntityPersister.html#877-891
This is typically used with a property of the entity. It takes an array with the key as the property name on the entity and the value as what you're searching for.
Examples:
$this->getDoctrine()
->getRepository('AppBundle:Users')
->findBy(['id' => $id])
;
$this->getDoctrine()
->getRepository('AppBundle:Users')
->findBy(['userName' => $userName])
;
$this->getDoctrine()
->getRepository('AppBundle:Users')
->findBy(['email' => $email])
;
You can read more about it here: https://symfony.com/doc/current/doctrine.html#fetching-objects-from-the-database
I want to get doctrin2 query result in object format but it gives me in array format.
I tried but I can't succeed.
This is demo query but if I want to apply similar types of join in another query than also requires results in object format but in all of them.
Can you have any solution?
return $this->createQueryBuilder("u")
->select("u.id as userid, up.id as profileid, u.username, u.email, u.isactive, up.firstname, up.lastname, up.profileimage, up.address, up.zipcode, up.biography")
->innerjoin("u.userprofile", "up")
->where("u.id = :userid")
->setParameter(":userid", $userid)
->getQuery()
->getResult();
ResultSet :
Array
(
[userid] => 4
[profileid] => 3
[username] => Test user
[email] => testuser#yahoo.com
[isactive] => 1
[firstname] => MyFname
[lastname] => MyLname
[profileimage] => 5111ea998c9476c2231180050d5ad64dc3298fe0.jpeg
[address] => My Address
[zipcode] => 36102555
[biography] => This is my first symfon2.3 project.
)
May be it's because you 're not loading the object fully, as you're loading it partially. That's why ORM is converting it automatically. You can use partial keyword to force it to load as partial object, but be carefull.