Vich uploader bundle: mapping doesn't exist - doctrine-orm

I need help to figure out why a VichUploaderBundle mapping doesn't exist.
I'm using VichUploaderBundle with EasyAdmin and ORM doctrine.
My vich_uploader.yaml file
vich_uploader:
db_driver: orm
mappings:
products:
#uri_prefix: '%app.path.node_images%'
uri_prefix: '%app.path.product_images%'
#uri_prefix: /products
#upload_destination: '%kernel.root_dir%/../web%app.path.node_images%'
upload_destination: '%kernel.root_dir%/../web/%app.path.product_images%'
Class debugging says the mapping is there
root#92d9f528832e:/app# php bin/console vich:mapping:debug-class
App\\Entity\\Node
Introspecting class App\Entity\Node:
Found field "imageFile", storing file name in "image" and using mapping
"product_images"
But mapping debugger can't find it
root#92d9f528832e:/app# php bin/console vich:mapping:debug
product_images
In MappingDebugCommand.php line 37:
Mapping "product_images" does not exist.
Here's my class
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* #ORM\Entity
* #ORM\Table(name="nodes")
* #ORM\Entity(repositoryClass="App\Repository\NodeRepository")
* #Vich\Uploadable
*/
class Node
{
/**
* #Vich\UploadableField(mapping="product_images", fileNameProperty="image")
*
* #var File
*/
private $imageFile;
/**
* #ORM\Column(type="datetime")
* #var \DateTime
*/
Here is my doctrine installation output:
doctrine/annotations v1.6.0 Docblock Annotations Parser
doctrine/cache v1.8.0 Caching library offering an object-oriented API for many cache backends
doctrine/collections v1.5.0 Collections Abstraction library
doctrine/common v2.10.0 PHP Doctrine Common project is a library that provides additional functi...
doctrine/dbal v2.8.0 Database Abstraction Layer
doctrine/doctrine-bundle 1.10.0 Symfony DoctrineBundle
doctrine/doctrine-cache-bundle 1.3.5 Symfony Bundle for Doctrine Cache
doctrine/doctrine-migrations-bundle v1.3.1 Symfony DoctrineMigrationsBundle
doctrine/event-manager v1.0.0 Doctrine Event Manager component
doctrine/inflector v1.3.0 Common String Manipulations with regard to casing and singular/plural ru...
doctrine/instantiator 1.1.0 A small, lightweight utility to instantiate objects in PHP without invok...
doctrine/lexer v1.0.1 Base library for a lexer that can be used in Top-Down, Recursive Descent...
doctrine/migrations v1.8.1 Database Schema migrations using Doctrine DBAL
doctrine/orm v2.6.3 Object-Relational-Mapper for PHP
doctrine/persistence v1.1.0 The Doctrine Persistence project is a set of shared interfaces and funct...
doctrine/reflection v1.0.0 Doctrine Reflection component
opsway/doctrine-dbal-postgresql v0.8.1 Extensions for support Postgres in Doctrine DBAL & DQL
symfony/doctrine-bridge v4.2.0 Symfony Doctrine Bridge
vich/uploader-bundle 1.8.5

I mistakenly named mapping "products" instead of "product_images".

Related

How to use PhpUnit/DbUnit with a real Doctrine EntityManager for functional testing

I'm using PhpUnit/DbUnit to create a set of 5 to 10 fixture records. I use in-memory sqlite.
I successfully can use a Doctrine\DBAL\Connection to access it, so I can use methods like ->insert( $tableName, $data ); for my tests.
Now I want to consume the Doctrine EntityManager so I can create an entity and call ->persist().
In unit-testing I used to mock the EntityManager and asserted an expectation to call persist.
But for functional-testing I want to check the final result written to the DB, even go further and re-use the result of the writing.
I therefore need to create a real EntityManager but consuming the DbUnit connection.
I've seen that creting a new EntityManager takes 3 arguments, but still the creator is protected:
/**
* Creates a new EntityManager that operates on the given database connection
* and uses the given Configuration and EventManager implementations.
*
* #param \Doctrine\DBAL\Connection $conn
* #param \Doctrine\ORM\Configuration $config
* #param \Doctrine\Common\EventManager $eventManager
*/
protected function __construct(Connection $conn, Configuration $config, EventManager $eventManager)
{
[...]
I don't know if I'm expected to create a subclass, or maybe use an already subclassed EntityManager or how should I proceed.
So question:
How can I setup a real Doctrine\ORM\EntityManager within a PhpUnit/DbUnit test class using the low level Doctrine\Dbal\Connection that is already bound to the DbUnit connection?
Note: I'm inside a symfony3 project, so mapping files are in the expected places of symfony.

#DataJpaTest not loading required classes

I'm attempting to set up some Spring Boot JPA tests using:
package biz.ianw.coindatabase.test;
#RunWith(SpringRunner.class)
#DataJpaTest
public class DbTests {
#Test
public void test1() { ... }
}
I have a service class:
package biz.ianw.coindatabase.database;
//#Service
#Repository
#Transactional(readOnly=false)
public class CoinDbService { ... }
and an application:
package biz.ianw.coindatabase;
#SpringBootApplication
#Slf4j
#Configuration
#EnableEncryptableProperties
public class Main {
#Autowired
CoinDbService dbService;
... }
Starting the test gives the error:
Field dbService in biz.ianw.coindatabase.Main required a bean of type 'biz.ianw.coindatabase.database.CoinDbService' that could not be found.
This is, I assumed, something to do with the type of beans #DataJpaTest loads during startup:
#DataJpaTest can be used if you want to test JPA applications. By
default it will configure an in-memory embedded database, scan for
#Entity classes and configure Spring Data JPA repositories. Regular
#Component beans will not be loaded into the ApplicationContext.
I'm not quite sure why #Service classes should be considered as not required for JPA testing, but I tried to force the matter by annotating it as #Repository instead, but to no avail.
I can manually load the class with #Import(...) but it seems a tad hooky. Is there a better way to tell DataJpaTest what I need for testing?
I am quoting from DataJpaTest documentation where you can find the answer to your question.
Annotation that can be used in combination with
#RunWith(SpringRunner.class) for a typical JPA test. Can be used when
a test focuses only on JPA components.
Using this annotation will disable full auto-configuration and instead
apply only configuration relevant to JPA tests.
By default, tests annotated with #DataJpaTest will use an embedded
in-memory database (replacing any explicit or usually auto-configured
DataSource). The #AutoConfigureTestDatabase annotation can be used to
override these settings.
If you are looking to load your full application configuration, but
use an embedded database, you should consider #SpringBootTest combined
with #AutoConfigureTestDatabase rather than this annotation.

Symfony2 Doctrine Mapped superclass annotations not found for different bundle

Symfony 2.8 + doctrine
I have two Bundles: CoreBundle and BonusBundle
CoreBundle have folder Model containing abstract class ClassA:
use Doctrine\ORM\Mapping as ORM;
//[...]
abstract class ClassA
{
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
protected $name;
}
and in another directory Entity - ClassB:
//[...]
/**
* ClassB
*
* #ORM\Table()
* #ORM\Entity
*/
class ClassB extends ClassA
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
}
Currently this working well, ClassB inherit name property and this way we have mapped Entity ClassB with id and name.
I added in Model directory of BonusBundle (second one) other abstract class ClassC. Its the same as ClassA but in other bundle.
And now last key thing to my example - I added ClassD in CoreBundle (Entity directory) extending ClassC from BonusBundle (the same way as above).
My problem
In result I recived ClassB with properties id, name and ClassD only with id property.
I cannot understand why this happen. Its looks like doctrine ignore annotations if superclass lives in other Bundle. I checked some docs and other questions but I can't find understand what happening.
I see in config docs here that auto_mapping option checking Entity directory:
Doctrine's auto_mapping feature loads annotation configuration from
the Entity/ directory of each bundle and looks for other formats (e.g.
YAML, XML) in the Resources/config/doctrine directory.
and
If it wasn't possible to determine a configuration format for a
bundle, the DoctrineBundle will check if there is an Entity folder in
the bundle's root directory. If the folder exist, Doctrine will fall
back to using an annotation driver.
in my project this folder exist in both Bundles. And If symfony check only Entity folder then why first example (Model) working well?
How this mechanism works?
I tested some possibilities and I found solution to fix it but still no idea why this happen.
If I add annotation to superclass #ORM\MappedSuperclass() then mappings are loaded correctly but only if class is located in Entity directory.
use Doctrine\ORM\Mapping as ORM;
/**
* ClassA
*
* #ORM\MappedSuperclass()
*/
abstract class ClassA
{
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
protected $name;
}
This way my annotations are visible in other Bundles and my ClassA is not Entity itself.
Still I dont understand why it works if class exist in the same Bundle but in other directory. In addition inside FoSUserBundle exist similar example using MappedSuperclass() - FOSUserBundle/Model/User.php but its located in Model directory and still working.
//EDIT
Completion to #Boulzy answer
Probably i found answer for second part - why it can see mapping from other directory but the same bundle. After analysing other working example I found that mapped property with strict access private is not visible for child class but protected property is visible, mapped and don't need #ORM\MappedSuperclass() annotation as long as it used in the same Bundle.
It seems that #ORM\MappedSuperclass() give us possibility to map even private properties and this way they are visible and mapped for other classes in any other bundle (as long as it lives in Entity or other mapped directory).
In completion of #Griva answer about the MappedSuperClass, here is an simple example of how to tell Doctrine to look for mapped classes in other folders than Entity:
<?php
namespace AppBundle;
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class AppBundle extends Bundle
{
/**
* {#inheritDoc}
*/
public function build(ContainerBuilder $container)
{
parent::build($container);
// With Yaml configuration format
$container->addCompilerPass(DoctrineOrmMappingsPass::createYamlMappingDriver(array(
realpath(__DIR__ . '/Resources/config/doctrine-mapping') => 'AppBundle\Model'
)));
// With annotation configuration format
$container->addCompilerPass(DoctrineOrmMappingsPass::createAnnotationMappingDriver(array(
'AppBundle\Model'
)));
}
}
There are Doctrine MappingsPass available for MongoDB and CouchDB too.

call to undefind function GetEntityManager() when running doctrine commands

I am trying to run doctrine commands on my windows OS. i am using following command,
"vendor/bin/doctrine.bat" orm:schema-tool:create
Following is my cli-config.php
<?php
// cli-config.php
require_once 'bootstrap.php';
// Any way to access the EntityManager from your application
$em = GetMyEntityManager();
$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)
));
i am getting this error
call to undefind function GetEnttyManager()
i am following http://doctrine-orm.readthedocs.org/en/latest/reference/tools.html for refrence
EDIT I reset the tags and now it is for ZF2
Call doctrine update like this:
$ path/to/your/php your/zf2/public/index.php orm:schema-tool:create
Note: don't use standard doctrine documentation for zf2 project, there are tutorials and documentations special for doctrine for zf2. Doctrine behaviour in zf2 is not quite the same as in Symfony.

How can I change/mock services for my UnitTests?

I'm using typo3 6 with extbase and some dependency injection features.
MyClass is injected with a service. The property which holds the service is protected.
class MyClass {
/**
*
* #var \X\Y\Z\MyService
* #inject
*/
protected $myService;
}
How can I change (or mock) the service in my UnitTest by?
I use reflection api to inject mock object to a protected field. See http://php.net/manual/en/class.reflectionobject.php