symfony The file was found but the class was not in it - php

when I run this command on my terminal
php app/console doctrine:schema:validate
I get this error
The autoloader expected class "tuto\testBundle\Entity\product" to be defined in file "../src/tuto/testBundle/Entity/product.php". The file was found but the class was not in it, the class name or names
pace probably has a typo.
I using symfony 2.8 and my code like the example page so what's the wrong ?
the product.php file code
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="product")
*/
class product
{
/**
*#ORM\Column(type="integer")
*#ORM\Id
*#ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/** #ORM\Column(length=140) */
public function getUserId(){
return $this->UserId;
}
public function setUserId($id){
return $this->UserId=$id;
}
}
and the product.orm.yml file
AppBundle\Entity\product:
type: entity
table: product
id:
id:
type: integer
generator:
strategy: AUTO

You need to capitalize your class name:
class Product
See symfony coding standards here. Specifically, PSR-1 specifies using StudlyCaps for class names.

Related

symfony 5 , Doctrine Lifecycle Listeners not running

I try a Doctrine Lifecycle Listeners create but he don't execute the dump.
i have created a entity, a listener and update service.yml
it's crazy ;-)
i have:
in services.yaml
App\EventListener\TestOrmListener:
tags:
- { name: doctrineormtester,event: postPersist, entity: 'App\Entity\Galery'}
in src/EventListener/TestOrmListener.php
<?php
namespace App\EventListener;
use App\Entity\Galery;
use Doctrine\Persistence\Event\LifecycleEventArgs;
class TestOrmListener
{
// the listener methods receive an argument which gives you access to
// both the entity object of the event and the entity manager itself
public function postPersist(LifecycleEventArgs $args): void
{
$entity = $args->getObject();
// if this listener only applies to certain entity types,
// add some code to check the entity type as early as possible
if (!$entity instanceof Galery) {
return;
}
$entityManager = $args->getObjectManager();
dump($entityManager);
die('mick');
// ... do something with the Product entity
}
}
an in the Entity Galery
<?php
namespace App\Entity;
use App\Repository\GaleryRepository;
use Doctrine\ORM\Mapping as ORM;
use App\Entity\Traits\Timestampable;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity(repositoryClass=GaleryRepository::class)
* #ORM\HasLifecycleCallbacks
*/
class Galery
{
use Timestampable;
/**
* #ORM\Id
* #Assert\NotBlank
* #ORM\Column(type="string", length=255)
*/
private $nom;
Do you have a idea?
You have a problem in your tag section.
According to the official documentation:
The next step is to enable the Doctrine listener in the Symfony application by creating a new service for it and tagging it with the doctrine.event_listener tag
So your service definition should look like this:
App\EventListener\TestOrmListener:
tags:
- { name: 'doctrine.event_listener', event: 'postPersist', entity: 'App\Entity\Galery' }

Symfony / Doctrine EntityListener doesn't work

I've a problem with an EntityListener... My EntityListener is not called, he doesn't work and I don't know why ! I use Symfony 3.4 and Doctrine 2.5.
So, my Entity :
<?php
namespace TelecomlineBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* LigneTelecom
*
* #ORM\Table(name="LIGNETELECOM")
* #ORM\Entity(repositoryClass="TelecomlineBundle\Repository\LigneTelecomRepository")
* #ORM\EntityListeners({"TelecomlineBundle\Doctrine\Listener\LigneTelecomListener"})
*/
class LigneTelecom
{
// etc ...
}
My EntityListener :
<?php
namespace TelecomlineBundle\Doctrine\Listener;
use TelecomlineBundle\Entity\LigneTelecom;
use TelecomlineBundle\Service\myService;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Mapping as ORM;
class LigneTelecomListener
{
/** #var myService $myService */
private $myService;
public function __construct(myService $myService)
{
$this->myService = $myService;
}
/**
* #ORM\PostUpdate()
*
* #param LigneTelecom $ligneTelecom
* #param LifecycleEventArgs $eventArgs
*/
public function postUpdate(LigneTelecom $ligneTelecom, LifecycleEventArgs $eventArgs)
{
$this->myService->calculate($ligneTelecom);
}
// etc ...
My service.yml :
telecomline.doctrine.lignetelecom_listener:
class: "TelecomlineBundle\Entity\LigneTelecom"
arguments: ["#telecomline.porte"]
tags:
- { name: doctrine.orm.entity_listener }
If anyone have a solution, I block on that since 3 hours :'(
you should specify which events subscriber wants to listen to
you use long form of definition or inherit from EventSubscriber and override getSubscribedEvents
The Symfony doc is not correct. You don't need a config in services.yaml. Just put an annotation to the entity class like shown on Doctrine docs.
<?php
namespace MyProject\Entity;
use App\EventListener\UserListener;
#[Entity]
#[EntityListeners([UserListener::class])]
class User
{
// ....
}

Sylius customizing model dose not working

It's my class file Country.php
<?php
namespace App\Entity;
use Sylius\Component\Addressing\Model\Country as BaseCountry;
class Country extends BaseCountry {
/**
* #var bool
*/
private $flag;
/**
* #return bool|null
*/
public function getFlag(): ?bool {
return $this->flag;
}
/**
* #param bool $flag
*/
public function setFlag(bool $flag): void {
$this->flag = $flag;
}
}
It's my orm file. AppBundle/Resources/config/doctrine/Country.orm.yml
App\Entity\Country:
type: entity
table: sylius_country
fields:
flag:
type: boolean
nullable: true
It's my config file. config/_sylius.yml
sylius_addressing:
resources:
country:
classes:
model: App\Entity\Country
It's all ok like sylius customizing model but it's not working.
It's my orm file. AppBundle/Resources/config/doctrine/Country.orm.yml
The orm file should be placed at src\Resources\config\doctrine for v1.3
Also, what's the result of php bin/console debug:config sylius_addressing?
//2018-12-18
Maybe try:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Sylius\Component\Addressing\Model\Country as BaseCountry;
/**
* Class Country
* #package App\Entity
* #ORM\Table(name="sylius_country")
* #ORM\Entity
*/
class Country extends BaseCountry {
type: mappedSuperclass in doctrine mapping
Make sure AppBundle extends AbstractResourceBundle
Make sure AppBundle has protected $mappingFormat = ResourceBundleInterface::MAPPING_YAML;

Symfony2 Undefined method. The method name must start with either findBy or findOneBy

I am currently working with Symfony2's Part 4 OF The SymBlog project
I am getting this ERROR message:
Undefined method 'getLatestPosts'. The method name must start with either findBy
or findOneBy!500 Internal Server Error - BadMethodCallException
This is my PostRepository Class:
<?php
namespace BLog\BlogBundle\Entity; use Doctrine\ORM\EntityRepository;
class PostRepository extends EntityRepository {
public function getLatestPosts($limit = null) {
$qp = $this->createQueryBuilder('p')
->select('p')
->addOrderBy('p.created', 'DESC');
if (false === is_null($limit)) {
$qp->setMaxResults($limit);
}
return $qp->getQuery()
->getResult();
}
}
This is the Controller's page Action method:
<?php
namespace Blog\BlogBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class DefaultController extends Controller {
public function indexAction() {
$em = $this->getDoctrine()
->getEntityManager();
$posts = $em->getRepository('BlogBundle:Post')
->getLatestPosts();
return $this->render('BlogBundle:Default:home.html.twig', > >array(
'posts' => $posts
));
}
...
}
This is a sample of my ../../../Entity/Post code:
<?php
namespace Blog\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* #ORM\Entity(repositoryClass="Blog\BlogBundle\Entity\PostRepository")
* #ORM\Table(name="post")
* #ORM\HasLifecycleCallbacks
*/
class Post {
....
...
..
/**
* #ORM\Column(type="text")
*/
protected $post;
...
...
I also tried all solutions in this post by ScoRpion
What is THE PROBLEM here ???
Check out this:
$posts = $em->getRepository('BlogBundle:Post')
->getLatestPosts();
It must be
$posts = $em->getRepository('BlogBlogBundle:Post')
->getLatestPosts();
Look up your namespace.
Removing the full path to Repository from the Entity class annotation works for me :
#ORM\Entity(repositoryClass="AdvertRepository")
I don't understand why though...
For me the solution was to put only the name of the repository, without the full path to it.
Was (error):
* #ORM\Entity(repositoryClass="OC\PlatformBundle\Repository\AdvertRepository")
Should be (working):
* #ORM\Entity(repositoryClass="AdvertRepository")
The solution was to put the repository class inside the Entity directory next to the Post Entity and this is the Entity class now:
<?php
namespace Blog\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* #ORM\Entity(repositoryClass="PostRepository")
* #ORM\Table(name="post")
* #ORM\HasLifecycleCallbacks
*/
class Post {
....
...
..
/**
* #ORM\Column(type="text")
*/
protected $post;
...
...

Extending a supermapped class in Symfony2 does not add the columns in MySQL

I am extending FOSAuthServerBundle and it has entities that are supermapped class and I need to extend them. Let's take one, AccessToken. When I extend AccessToken, I need all of its parent properties to be mapped in the database, such as $expiresAt and $scope but currently, they are not. Clearly, there is something I don't understand or am doing wrong.
Here's what I have in my own AccessToken yml:
OSC\OAuthServerBundle\Entity\AccessToken:
type: entity
id:
id:
type: integer
id: true
generator:
strategy: AUTO
manyToOne:
client:
targetEntity: Client
user:
targetEntity: Application\Sonata\UserBundle\Entity\User
lifecycleCallbacks: { }
Here's my Accesstoken php file:
<?php
namespace OSC\OAuthServerBundle\Entity;
use FOS\OAuthServerBundle\Document\AccessToken as BaseAccessToken;
use FOS\OAuthServerBundle\Model\ClientInterface;
use Symfony\Component\Security\Core\User\UserInterface;
class AccessToken extends BaseAccessToken
{
/**
* #var integer
*/
protected $id;
protected $client;
protected $user;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
public function getClient()
{
return $this->client;
}
public function setClient(ClientInterface $client)
{
$this->client = $client;
}
public function getUser()
{
return $this->user;
}
public function setUser(UserInterface $user)
{
$this->user = $user;
}
}
When I run php app/console doctrine:schema:update --force, the only fields that I have in my database are id, user_id and client_id. But I also need expiresAt and scope...
Here's what I have verified:
I am properly extending the FOSAuthServerBundle:
namespace OSC\OAuthServerBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class OSCOAuthServerBundle extends Bundle
{
public function getParent()
{
return 'FOSOAuthServerBundle';
}
}
The FOSOAuthServerBundle and OSCOAuthServerBundle are in the list of doctrine mappings in config.yml
I cleared the cache.
Is it normal that the fields are not mapped automatically ? Do I have to rewrite them all in the yml config? If yes, in my head, it defeats the purpose of a mapped superclass. Maybe I have to write in the yml that it inherits from a mapped superclass ?
Edit 1 : Maybe this does not work since the base class Token is not abstract ?
Solution:
I had to point to Entity and not Model as a tutorial was telling me. This is so since in the ORM mapping in the bundle files, the mapping is done for the entity and not the model !
<?php
namespace OSC\OAuthServerBundle\Entity;
use FOS\OAuthServerBundle\Entity\AccessToken as BaseAccessToken; //Entity here not model !
use FOS\OAuthServerBundle\Model\ClientInterface;
use Symfony\Component\Security\Core\User\UserInterface;
class AccessToken extends BaseAccessToken
{
}
Did you try to extend this class : FOS\OAuthServerBundle\Entity\AccessToken as BaseAccessToken instead of this one FOS\OAuthServerBundle\Document\AccessToken ?
You don't have to declare your properties twice (in super and sub class).
Do not redeclare $id, $client and $user

Categories