I try to create default repository in my Symfony 3. First I created Repository class with method 'findByParentOrderedByName'. In next step I added in Entity line:
* #ORM\Entity(repositoryClass="AppBundle\Repository\ChildRepository")
Unfortunelly when I try to run findByParentOrderedByName() I get error
Undefined method 'findAllOrderedByName'. The method name must start with either findBy, findOneBy or countBy!
What I make incorrect?
Repository code:
<?php
namespace AppBundle\Entity;
/**
* Child
* #ORM\Entity(repositoryClass="AppBundle\Repository\ChildRepository")
*/
class Child
{
........
}
To use custom repository class properly, firstly repository class name must be defined in entity class.
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="AppBundle\Repository\ChildRepository")
*/
class Child
{
}
Then repository class created as like this:
namespace AppBundle\Repository;
use Doctrine\ORM\EntityRepository;
class ChildRepository extends EntityRepository
{
/**
* #return Child[]
*/
public function findAllOrderedByName()
{
return $this->getEntityManager()
->createQuery('SELECT * c FROM AppBundle:Child c ORDERED BY c.name ASC')
->getResult();
}
}
Related
I'm trying to make a custom Doctrine's ORM Repository and extend it but I can't find a way to make it work. So far this is what i have:
The original Repository
//AppBundle\Repository\LocaleRepository.php
namespace AppBundle\Repository;
use Doctrine\ORM\EntityRepository;
use JMS\DiExtraBundle\Annotation as DI;
class LocaleRepository extends EntityRepository
{
protected myCustomFunction(){
}
}
The extended Repository
//OfficeBundle\Repository\OfficeRepository.php
namespace OfficeBundle\Repository;
use AppBundle\Repository\LocaleRepository;
class OfficeRepository extends LocaleRepository
{
//Empty class
}
My entiy:
namespace OfficeBundle\Entity;
// some calls to traits
use Doctrine\ORM\Mapping as ORM;
/**
* Office
*
* #ORM\Table(name="office__office")
* #ORM\Entity(repositoryClass="OfficeBundle\Repository\OfficeRepository")
*/
class Office implements TranslatableInterface{
//...
}
And Finally the call:
$em = $this->getDoctrine()->getManager();
$this->getEntityManager();
$office=$em->getRepository('OfficeBundle:Office')->myCustomeFunction($slug);
This trows the exception:
Undefined method 'myCustomFunction'. The method name must start with either findBy or findOneBy!
If I place myCustomeFunction inside the OfficeRepository it works fine but it brings down the purpose of extendind the repository. Also, the repository loaded by the controller is the correct one, vardumping the class shows: 'OfficeBundle\Repository\OfficeRepository'.
Finally I'm using KNP DoctrineBehaviors(translatable) on the office entity.
You must make your method public if you are going to use it outside the repository class.
class LocaleRepository extends EntityRepository
{
public function myCustomFunction()
{
....
}
}
I have 2 bundles and i want to override the repository of one of them in the other :
I have a source bundle : SourceBundle.
I have my override bundle : OverrideBundle
First, in OurVendorOverrideBundle.php, I added :
public function getParent()
{
return 'SomeVendorSourceBundle';
}
Then
I wanted to add a custom method for the repository of an entity of SourceBundle.
The source entity is Response.php, and its repository is ResponseRepository.php
So i did :
<?php
namespace OurVendor\OverrideBundle\Repository;
use Doctrine\ORM\EntityRepository;
use SomeVendor\SourceBundle\Repository\ResponseRepository as BaseRepository;
class ResponseRepository extends BaseRepository
{
/**
*
* #return array
*/
public function getQueryExerciseAllResponsesForAllUsers($exoId)
{
$qb = $this->createQueryBuilder('r');
$qb->join('r.paper', 'p')
->join('p.exercise', 'e')
->where('e.id = ?1')
->andWhere('p.interupt = ?2')
->setParameters(array(1 => $exoId, 2 => 0));
return $qb->getQuery();
}
}
If i dont set the Entity in the OverrideBundle i have this error :
The autoloader expected class "CPASimUSante\ExoverrideBundle\Entity\Response" to be defined in file "/home/www/myproject/vendor/ourvendor/override-bundle/OurVendor/OverrideBundle/Entity/Response.php". The file was found but the class was not in it, the class name or namespace probably has a typo.
The SourceBundle entity is :
<?php
namespace SomeVendor\SourceBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* SomeVendor\SourceBundle\Entity\Response
*
* #ORM\Entity(repositoryClass="SomeVendor\SourceBundle\Repository\ResponseRepository")
* #ORM\Table(name="source_response")
*/
class Response
{
...
}
So i add the Entity in the OverrideBudle :
<?php
namespace OurVendor\OverrideBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use SomeVendor\SourceBundle\Entity\Response as BaseEntity;
/**
* SomeVendor\SourceBundle\Entity\Response
*
* #ORM\Entity(repositoryClass="OurVendor\OverrideBundle\Repository\ResponseRepository")
* #ORM\Table(name="override_response")
*/
class Response extends BaseEntity
{
public function __construct()
{
parent::__construct();
}
}
But then i have this error :
An exception occurred while executing 'SELECT u0_.id AS id0, u0_.ip AS ip1, u0_.mark AS mark2, u0_.nb_tries AS nb_tries3, u0_.response AS response4, u0_.paper_id AS paper_id5, u0_.interaction_id AS interaction_id6 FROM ujm_exoverride_response u1_ INNER JOIN ujm_paper u2_ ON u0_.paper_id = u2_.id INNER JOIN ujm_exercise u3_ ON u2_.exercise_id = u3_.id WHERE u3_.id = ? AND u2_.interupt = ?' with params ["1", 0]:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'u0_.id' in 'field list'
This seems that fields in the table are not found. So i changed to
<?php
namespace OurVendor\OverrideBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use SomeVendor\SourceBundle\Entity\Response as BaseEntity;
/**
* SomeVendor\SourceBundle\Entity\Response
*
* #ORM\Entity(repositoryClass="OurVendor\OverrideBundle\Repository\ResponseRepository")
* #ORM\Table(name="source_response")
*/
class Response extends BaseEntity
{
public function __construct()
{
parent::__construct();
}
}
And it worked.
...But when i reinstalled the bundle, to test if everything was ok i had this fatal error stating that source_response is already defined (which is, indeed).
So what can i do then ?
I have also read that i can't override an entity unless the source extends MappedSuperclass, in my case, it doesn't.
But i am doomed if i only want to override its repository ? Is it possible ? If then, what is the right way ?
If i remove altogether the annotation for the override entity, i have :
Class "OurVendor\OverrideBundle\Entity\Response" sub class of "SomeVendor\SourceBundle\Entity\Response" is not a valid entity or mapped super class.
500 Internal Server Error - MappingException
Doctrine mapping in another bundles can be overriden on entity class metadata loading.
EventListener
<?php
namespace Lol\RandomBundle\EventListener;
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
class ClassMetadataListener
{
/**
* Run when Doctrine ORM metadata is loaded.
*
* #param LoadClassMetadataEventArgs $eventArgs
*/
public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
{
$classMetadata = $eventArgs->getClassMetadata();
if ('AnotherLol\AnotherRandomBundle\Entity\Response' === $classMetadata->name) {
// Do whatever you want...
$classMetadata->customRepositoryClassName = 'ThirdLol\SomeBundle\Repository\NewResponseRepository';
}
}
}
Service configuration
services:
lol.random.listener.class_metadata:
class: Lol\RandomBundle\EventListener\ClassMetadataListener
tags:
- { name: doctrine.event_listener, event: loadClassMetadata }
I just want to get the last record so I cannot call my custom query in custom build repo. Error is below. Do you know what it could be?
Note: Normal find(), findBy() like queries work fine. If there is a easier way of doing it then please tell me how to do it.
Thanks
ERROR:
Undefined method 'getLastRecord'. The method name must start with either findBy or findOneBy!
MAIN CLASS:
namespace Booking\AdminBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Table(name="CashQuery")
* #ORM\Entity(repositoryClass="Booking\AdminBundle\Entity\CashQueryRepository")
* #ORM\Entity
*/
class CashQuery
{
}
REPO:
namespace Booking\AdminBundle\Entity;
use Doctrine\ORM\EntityRepository;
class CashQueryRepository extends EntityRepository
{
public function getLastRecord()
{
return $this->getEntityManager()
->createQuery(
'SELECT d FROM AdminBundle:CashQuery d ORDER BY d.id DESC LIMIT 1'
)
->getResult();
}
}
TRYING TO USE WITH THIS:
$dcq = $em->getRepository('BookingAdminBundle:CashQuery')->getLastRecord();
if (! $dcq) {
echo 'CashQuery could not found';
return false;
}
echo $dcq->getCreatedAt();
It seems like you're overwriting the Entity annotation. Try removing the second one:
namespace Booking\AdminBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Table(name="CashQuery")
* #ORM\Entity(repositoryClass="Booking\AdminBundle\Entity\CashQueryRepository")
*/
class CashQuery
{
}
I have entities
<?php
namespace Proj\Bundle\MyBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* ClassTop
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="Proj\Bundle\MyBundle\Repository\ClassTopRepository")
* #ORM\InheritanceType("SINGLE_TABLE")
* #ORM\DiscriminatorColumn(name="aVal", type="string")
* #ORM\DiscriminatorMap({ "ValOne" = "ClassSubOne", "ValTwo" = "ClassSubTwo", "ValThree" = "ClassSubThree" })
*
*/
class ClassTop
{
.....
}
class ClassSubOne extends ClassTop
{
....
}
class ClassSubTwo extends ClassTop
{
....
}
class ClassSubThree extends ClassTop
{
....
}
My problem is when I call the find() (or findOneBy, findAll, etc) method, I have an Oracle error.
$entityManager->getRepository('ProjMyBundle:ClassSubOne')->findAll()
In the query I have
SELECT field, field2 FROM CLASSTOP WHERE aVAL IN ()
The discriminator value "ValOne" is not mapping and not passed in Doctrine.
Any help would be much appreciated.
You need to repository classes for the subclasses not the top one. You treat your subclasses as full defined classes and that's all. Then when you need to query for your entities you don't go for the top entity but the subclasses. I don't know if I made myself clear here!
In your case:
#ORM\Entity(repositoryClass="path\to\your\repository")
The repository line goes for the subclasses not the top superclass. so you do
/**
*
* #ORM\Entity(repositoryClass="path\to\your\ClassSubOneRepository")
*/
class ClassSubOne extends ClassTop
{
...
}
instead of
/**
*
* #ORM\Entity(repositoryClass="Proj\Bundle\MyBundle\Repository\ClassTopRepository")
*/
class ClassTop
{
...
}
I've a brand new Symfony2 application and I'm 100% sure I've followed http://symfony.com/doc/current/book/doctrine.html#custom-repository-classes to the letter. However I'm getting the following error;
Entity 'Core\MainBundle\Entity\LandingPromotions' has no field 'promotionType'. You can therefore not call 'findByPromotionType' on the entities' repository
I've no clue as to what I've missed. Code as follows;
LandingPromotions
<?php
namespace Core\MainBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Core\MainBundle\Entity\LandingPromotions
*
* #ORM\Entity(repositoryClass="Core\MainBundle\Entity\PromotionRepository")
* #ORM\Table(name="landing_promotions")
*/
class LandingPromotions
{
//Normal entity stuff
}
PromotionRepository.php
namespace Core\MainBundle\Entity;
use Doctrine\ORM\EntityRepository;
class PromotionRepository extends EntityRepository
{
public function findByPromotionType($typeId)
{
}
}
Controller
namespace Core\MainBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Core\MainBundle\Entity\Game;
use Core\MainBundle\Entity\LandingPromotions as LandingPromotions;
class GameController extends Controller
{
/**
* #Route("{_locale}/games/")
* #Template()
*/
public function indexAction($_locale)
{
$lp_repo = $this->getDoctrine()->getRepository('CoreMainBundle:LandingPromotions');
$lp_repo->findByPromotionType(2);
}
}
After much hair pulling and cursing I discovered that the entities are not using PHP annotations, but rather the XML versions. How this got mixed up I've no idea, but that'll be tomorrow's issue.