Doctrine2: Call to undefined method Doctrine\ORM\QueryBuilder::field() - symfony

I am trying to create a query with the doctrine query builder:
public function searchAuthors($q)
{
$qb = $this->createQueryBuilder('Author')
->field('person.firstname')->equals('test');
return $qb->getQuery()->getResult();
}
But I am getting :
Fatal error: Call to undefined method Doctrine\ORM\QueryBuilder::field() in ... on line 18
The field() method is from the doctrine documentation webpage. Could you please help me to resolve that.
Thank you.

field() exists only in Doctrine MongoDB ODM (to use it see here) not in Doctrine 2 ORM.

Related

Symfony Undefined function: 7 ERROR: operator does not exist: json ~~ unknown

While learning Symfony, I decided to play around with roles. I need to get all users who have the role 'ROLE_MODERATOR' in order to send them emails
Created the following method in the repository
public function getModerators() {
$role = 'ROLE_MODERATOR';
return $this->createQueryBuilder('u')
->where('u.roles = :role')
->setParameter('role', $role)
->getQuery()
->getResult();
}
As a result I get an error
An exception occurred while executing a query: SQLSTATE[42883]: Undefined function: 7 ERROR: operator does not exist: json ~~ unknown HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
Maybe the framework has some standard functions for getting users by roles?

Entity object to array

I'm trying to convert an entity to a associative array.
It seems that the method toArray() is not available for entity objects.
Reading Symfony doc, it seems I should use the SerializerInterface.
After enabling it, I can't seems to find the right syntax to convert my entity into an associative array.
Can someone correct my code please?
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\SerializerInterface;
// -----------------------
public function salleAction(Request $request, Projet $projet, SerializerInterface $serializer) {
return this->json(array(
'projet'=>$serializer->serialize($projet, new ObjectNormalizer())
));
}
With the code above, I'm getting this error message
Warning: Illegal offset type in isset or empty
If I replace new ObjectNormalizer() by 'jsons', I'm getting the next error message:
A circular reference has been detected when serializing the object of class "AppBundle\Entity\Projet" (configured limit: 1)
I suggest you to add the following method to an object, that needs to be converted
public function toArray()
{
return get_object_vars($this);
}
and use it everywhere $array = $projet->toArray();

Symfony: Query Builder: Catchable Fatal Error: Object of class DateTime could not be converted to string

I am trying to do a query in Symfony 3 to select the Commentaries of a Wish (Object Wishcom) with the contributionType included (Object ContributionType) thanks to a JOIN.
When running the webpage I get a:
Error: Method Doctrine\ORM\Query\Expr\Func::__toString() must not throw an >exception, caught Symfony\Component\Debug\Exception\ContextErrorException: >Catchable Fatal Error: Object of class DateTime could not be converted to >string
In the forums I could get that the datetime has to be converted with format. The thing is that I am not manipulating directly the date with my query although I know there is one DateTime attribute in my Wishcom object.
Should I select specifically the date and format it ? In that case were should it be done ? Or does the error come from something else ?
It seems the error comes from the where statement and the function ToString from Expr not being able to convert the date. I don't know what I should do.
$queryBuilder->where($queryBuilder->expr()->in('w.wish', $wish));
Here is my call in the controller:
$arraywishcom=$em->getRepository(Wishcom::class)->getWishcomWithContributionType($wish);
And my repository:
<?php
namespace Shaker\JRQBundle\Repository;
use Shaker\JRQBundle\Entity\Wish;
/**
* WishcomRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class WishcomRepository extends \Doctrine\ORM\EntityRepository
{
public function getWishcomWithContributionType(Wish $wish) {
$queryBuilder = $this
->createQueryBuilder('w')
->leftJoin('w.contributiontype', 'contributiontype')
->addSelect('contributiontype')
;
$queryBuilder->where($queryBuilder->expr()->in('w.wish', $wish));
return $queryBuilder
->getQuery()
->getResult()
;
}
}
I have found a different way of doing the query and it works.
Instead of the where I had with expr(), which was the problem, I did the following:
$queryBuilder->where('w.wish = :wish')->setParameter('wish', $wish);
But I still do not know why I got the error in the first place.

A "__toString()" method was not found on the objects of type "Admin\UserBundle\Entity\Extension" passed to the choice field

I installed easyAdmin Bundle(symfony2) then :
I combined it with fosUserBundle.
I add an entity "extension".
So i created a user but when i tried to edit the account in the
dashboard i get this error :
A "__toString()" method was not found on the objects of type "Admin\UserBundle\Entity\Extension" passed to the choice field
So can anyone help me?
Sorry for the following question, but what about adding a __toString() method?
In your Extension:
public function __toString()
{
return $this->fieldYouWantToDisplayAsChoice;
}
For example, if your Extension has a property username, you can use return
$this->username, and the choice field will be populated with the username property of each user.
See magic __toString.

Class not found when trying to index documents using Solr with Symfony2

I am very new to Solr and I am probably missing something simple however, having followed Xavier Briand's presentation I have set up Symfony2, Solarium and Nelmio\SolariumBundle.
"nelmio/solarium-bundle": "2.0.*#dev",
"solarium/solarium": "3.0.*"
Having implemented a toSolrDocument method for my doctrine php object.
public function toSolrDocument(\Solarium_Document_ReadWrite $doc)
{
$doc->id = $this->getId();
$doc->description = $this->getTitle();
$doc->path = "path";
return $doc;
}
I am faced with the following error.
Catchable Fatal Error: Argument 1 passed to ::toSolrDocument() must be an instance of Solarium_Document_ReadWrite, instance of Solarium\QueryType\Update\Query\Document given, called in Controller.php
The controller calling this toSolrDocument method has the following function
public function indexItems(){
$client = $this->get('solarium.client');
// get an update query instance
$update = $client->createUpdate();
// create documents
$documents = array();
$em = $this->getDoctrine()->getManager();
$repo = $em->getRepository('<Bundle>:<Object>');
$items = $repo->findAll();
foreach ($items as $item) {
$documents[] = $item->toSolrDocument($update->createDocument());
}
// add the documents and a commit command to the update query
$update->addDocuments($documents);
$update->addCommit();
// this executes the query and returns the result
$result = $client->update($update);
}
Most of this method again comes directly from Xavier's presentation and it is clear to see that the method $update->createDocument() does not return the correct type. In fact it returns an instance of the my php object. Does anyone know where I am going wrong here? Another thing that might be of help is that even when I try to pass a Solarium Document directly I get an exception.
foreach ($items as $item) {
$rw_item = new \Solarium_Document_ReadWrite();
$documents[] = $item->toSolrDocument($rw_item);
}
The exception is
FatalErrorException: Error: Class 'Solarium_Document_ReadWrite' not found in
I can't seem to find this class in any of the bundles and I am wondering if my setup might be causing the issues. Any help would be very much appreciated.
One additional point to note is that when I am running the solr jar I see the query requests come in from my symfony2 page it is only this indexing action that I can not work out so the config may be alright and I am miss understanding the use of the bundle.
You just need to use the correct class for the argument.
use Solarium\QueryType\Select\Result\AbstractDocument;
...
public function toSolrDocument(AbstractDocument $doc)
{
You could also not type hint it:
public function toSolrDocument($doc)
{

Resources