Ask your Symfony questions! Pay money and get answers fast! (more info)

What path do I use for an entity? Symfony

This is my first time using Symfony2, so I'm still rusty on the syntax.

In a unit test, this code works fine:

$repo = $this->getEntityManager('data_source')->getRepository( 'Magnum\Bundle\DataSourceBundle\Entity\Venue' );
$venue = $repo->find(1);
$this->assertTrue( $venue instanceof Venue );
$this->assertEquals( $venue->getForeignUid(), 21985 );
$this->assertEquals( $venue->getPostcode(), '10128' );


but if I do this:

$supplierPriorityRepo = $this->getEntityManager('data_source')->getRepository( 'Magnum\Bundle\TalentBundle\Entity\SupplierPriority' );
$pp = $supplierPriorityRepo->find(1);
echo "\n\n getTalentSupplier: ";
echo $pp->getTalentSupplier();


I get:

Magnum\Bundle\DataSourceBundle\Tests\Unit\Importer\TalentDotCom\TalentDotComFeedImporterNewYorkTest::testImportPhotograph
Doctrine\ORM\Mapping\MappingException: Class Magnum\Bundle\TalentBundle\Entity\SupplierPriority is not a valid entity or mapped super class.

But the class I want looks like this:

<?php

namespace Magnum\Bundle\TalentBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* Priority of a Talent Supplier for a specific profile
*
* @ORM\Entity(repositoryClass="Magnum\Bundle\TalentBundle\Entity\Repository\SupplierPriorityRepository")
* @ORM\Table(name="supplier_priority")
* @package TalentBundle
*/
class SupplierPriority
{

/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="PriorityProfile", inversedBy="priorities")
* @ORM\JoinColumn(name="profile", referencedColumnName="id")
* @var PriorityProfile
*/
protected $profile;

/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="TalentSupplier")
* @ORM\JoinColumn(name="supplier_id", referencedColumnName="id")
* @var TalentSupplier
*/
protected $talentSupplier;

/**
* @ORM\Column(type="smallint")
* @var int
*/
protected $priority;



So I have copied the namespace. So why did I get this wrong?


Answers (2)

2013-07-11

Luis Cordova answers:

it is a different bundle

$supplierPriorityRepo = $this->getEntityManager('data_source')->getRepository( 'Magnum\Bundle\TalentBundle\Entity\SupplierPriority' );


why not just getEntityManager()->getRepository('MagnumTalentBundle:SupplierPriority') ?


Lawrence Krubner comments:

I see. Right, different bundle. What is the syntax for getting different bundles? I tried your example but it doesn't work.


Luis Cordova comments:

I am suspicious the DataSourceBundle has configured some mapping in which it uses a particular connection

and the other bundle TalentBundle has a different configuration for a secondary connection

Doctrine does what you tell it to, so if you happen to use a different connection since they map with different connections you should use the correct handler manager/connection, if you fail to do this then it will not find the mapping obviously


Luis Cordova comments:

contact me in private :) please, maybe we can do some good stuff together


Lawrence Krubner comments:


Physically, the unit test is here:

/var/www/magnum.com/releases/lawrence_trout/internal-web-service/src/Magnum/Bundle/DataSourceBundle/Tests/Unit/Importer/TalentDotCom/TalentDotComFeedImporterNewYorkTest.php

and the venue class is here:

/var/www/magnum.com/releases/lawrence_trout/internal-web-service/src/Magnum/Bundle/ContentBundle/Entity/Venue.php

and the SupplierPriority class is here:

/var/www/magnum.com/releases/lawrence_trout/internal-web-service/src/Magnum/Bundle/TalentBundle/Entity/SupplierPriority.php


Luis Cordova comments:

it has nothing to do with paths, it has to do with mapping and doctrine configuration

doctrine.yml

or config.yml

or such, check under doctrine key and dbal connections mapping


Lawrence Krubner comments:

The venue class starts off as:

<?php

namespace Magnum\Bundle\ContentBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Gedmo\Mapping\Annotation as Gedmo;
use Magnum\DBAL\Enum\StatusEnum;
use Magnum\Component\Content\EditorialWorkflowInterface;

use Doctrine\Common\Collections\ArrayCollection;
use Magnum\Bundle\SearchIndexerBundle\Component\IndexableInterface;
use Magnum\Bundle\SearchIndexerBundle\Entity\ServiceCall;

/**
* Venue Entity
*
* @ORM\Table( name="content_venues" )
* @ORM\Entity(repositoryClass="Magnum\Bundle\ContentBundle\Entity\Repository\VenueRepository" )
*
* @package ContentBundle
*/
class Venue extends VenueAbstract implements EditorialWorkflowInterface, IndexableInterface
{

const SOFT_DELETE_STATUS = StatusEnum::STATUS_DELETED;

/**
* @ORM\OneToMany(targetEntity="Magnum\Bundle\ContentBundle\Entity\Translation\VenueTranslation", mappedBy="venue", cascade={"all"} )
* @ORM\JoinColumn(name="id", referencedColumnName="venue_id")
*/
protected $translations;

/**
* @ORM\OneToMany(targetEntity="Magnum\Bundle\ContentBundle\Entity\Occurrence", mappedBy="venue", cascade={"all"} )
* @ORM\JoinColumn(name="id", referencedColumnName="venue_id", onDelete="cascade")
*/
protected $occurrences;

/**
* @ORM\ManyToOne(targetEntity="Magnum\Bundle\ContentBundle\Entity\Venue", cascade={"persist"} )
* @ORM\JoinColumn(name="umbrella_venue_id", referencedColumnName="id", onDelete="set null")
*/
protected $umbrellaVenue;

/** @ORM\Column(type="status") **/
protected $status = StatusEnum::STATUS_NEW;

/**
* @ORM\OneToMany(targetEntity="Magnum\Bundle\ContentBundle\Entity\ShowTime", mappedBy="venue", cascade={"all"})
* @ORM\JoinColumn(name="id", referencedColumnName="venue_id", onDelete="cascade")
*/
protected $showTimes;


Luis Cordova comments:

again, it is not this, it is in your configuration yml files

i never said annotations


Lawrence Krubner comments:

app/config looks like this:


-rw-r--r-- 1 nginx nginx 2861 Jun 12 20:57 categoriser_test.yml
-rw-r--r-- 1 nginx nginx 5258 Jun 12 20:57 categoriser.yml
-rw-r--r-- 1 nginx nginx 16690 Jun 12 20:57 comment.yml
-rw-r--r-- 1 nginx nginx 1670 Jun 12 20:57 config_common_dev.yml
-rw-r--r-- 1 nginx nginx 340 Jun 12 20:57 config_dev.yml
-rw-r--r-- 1 nginx nginx 483 Jun 12 20:57 config_prod.yml
-rw-r--r-- 1 nginx nginx 4176 Jun 29 04:13 config_test.yml
-rw-r--r-- 1 nginx nginx 5975 Jun 12 20:57 config.yml
-rw-r--r-- 1 nginx nginx 2236 Jul 11 15:59 contact.yml
-rw-r--r-- 1 nginx nginx 2883 Jul 11 15:59 data_source.yml
-rw-r--r-- 1 nginx nginx 12829 Jun 12 20:57 doctrine.yml
-rw-r--r-- 1 nginx nginx 43456 Jun 12 20:57 folder_rules_test.yml
-rw-r--r-- 1 nginx nginx 48458 Jun 12 20:57 folder_rules.yml
-rw-r--r-- 1 nginx nginx 369 Jun 12 20:57 iws.yml
-rw-r--r-- 1 nginx nginx 223 Jun 12 20:57 local_vars
-rw-r--r-- 1 nginx nginx 788 Jun 12 20:57 newsletter_test.yml
-rw-r--r-- 1 nginx nginx 3196 Jun 12 20:57 newsletter.yml
-rw-r--r-- 1 nginx nginx 3519 Jun 12 20:57 parameters.ini
-rw-r--r-- 1 nginx nginx 3623 Jun 12 20:57 parameters.ini.dist
-rw-r--r-- 1 nginx nginx 1999 Jun 12 20:57 parameters.ini.hudson
-rw-r--r-- 1 nginx nginx 717 Jun 27 22:04 parameters_test.ini
-rw-r--r-- 1 nginx nginx 717 Jun 12 20:57 parameters_test.ini.dist
-rw-r--r-- 1 nginx nginx 287 Jun 12 20:57 routing_dev.yml
-rw-r--r-- 1 nginx nginx 774 Jun 12 20:57 routing_rules_test.yml
-rw-r--r-- 1 nginx nginx 12432 Jun 12 20:57 routing_rules.yml
-rw-r--r-- 1 nginx nginx 8140 Jun 12 20:57 routing.yml
-rw-r--r-- 1 nginx nginx 1106 Jun 12 20:57 search_dev.yml
-rw-r--r-- 1 nginx nginx 1551 Jun 12 20:57 search_indexer.yml
-rw-r--r-- 1 nginx nginx 4666 Jul 11 15:59 search_service.yml
-rw-r--r-- 1 nginx nginx 2582 Jun 12 20:57 search_test.yml
-rw-r--r-- 1 nginx nginx 3575 Jun 12 20:57 search.yml
-rw-r--r-- 1 nginx nginx 1191 Jun 12 20:57 security.yml
-rw-r--r-- 1 nginx nginx 2688 Jun 12 20:57 uk_postcode_locator.yml
-rw-r--r-- 1 nginx nginx 0 Jun 12 20:57 url_config.yml
-rw-r--r-- 1 nginx nginx 44 Jun 12 20:57 varnish_dev.yml
-rw-r--r-- 1 nginx nginx 44 Jun 12 20:57 varnish.yml



What should I be looking for? There is a lot of config to look through.


Luis Cordova comments:

-rw-r--r-- 1 nginx nginx 12829 Jun 12 20:57 doctrine.yml


Lawrence Krubner comments:


doctrine.yml:




doctrine:
dbal:
types:
json_multi_value: Magnum\DBAL\JsonMultipleValueType
default_connection: default
connections:
default:
driver: %database_driver%
host: %database_host%
user: %database_user%
password: %database_password%
charset: %database_charset%
wrapper_class: Magnum\DBAL\ReopeningConnectionWrapper
comment:
driver: %database_driver%
host: %database_host%
dbname: %comment_database_name%
user: %database_user%
password: %database_password%
charset: %database_charset%
wrapper_class: Magnum\DBAL\ReopeningConnectionWrapper
content:
driver: %database_driver%
host: %database_host%
dbname: %content_database_name%
user: %database_user%
password: %database_password%
charset: %database_charset%
wrapper_class: Magnum\DBAL\ReopeningConnectionWrapper
data_source:
driver: %database_driver%
host: %database_host%
dbname: %content_database_name%
user: %database_user%
password: %database_password%
charset: %database_charset%
wrapper_class: Magnum\DBAL\ReopeningConnectionWrapper
image:
driver: %database_driver%
host: %database_host%
dbname: %image_database_name%
user: %database_user%
password: %database_password%
charset: %database_charset%
wrapper_class: Magnum\DBAL\ReopeningConnectionWrapper
migration:
driver: %database_driver%
host: %database_host%
dbname: %migration_database_name%
user: %database_user%
password: %database_password%
charset: %database_charset%
wrapper_class: Magnum\DBAL\ReopeningConnectionWrapper
routing:
driver: %database_driver%
host: %database_host%
dbname: %routing_database_name%
user: %database_user%
password: %database_password%
charset: %database_charset%
wrapper_class: Magnum\DBAL\ReopeningConnectionWrapper
superlists:
driver: %database_driver%
host: %database_host%
dbname: %superlists_database_name%
user: %database_user%
password: %database_password%
charset: %database_charset%
wrapper_class: Magnum\DBAL\ReopeningConnectionWrapper
taxonomy:
driver: %database_driver%
host: %database_host%
dbname: %taxonomy_database_name%
user: %database_user%
password: %database_password%
charset: %database_charset%
wrapper_class: Magnum\DBAL\ReopeningConnectionWrapper
slideshow:
driver: %database_driver%
host: %database_host%
dbname: %slideshow_database_name%
user: %database_user%
password: %database_password%
charset: %database_charset%
wrapper_class: Magnum\DBAL\ReopeningConnectionWrapper
talent:
driver: %database_driver%
host: %database_host%
dbname: %content_database_name%
user: %database_user%
password: %database_password%
charset: %database_charset%
wrapper_class: Magnum\DBAL\ReopeningConnectionWrapper
facebook:
driver: %database_driver%
host: %database_host%
dbname: %facebook_database_name%
user: %database_user%
password: %database_password%
charset: %database_charset%
wrapper_class: Magnum\DBAL\ReopeningConnectionWrapper
log:
driver: %database_driver%
host: %database_host%
dbname: %log_database_name%
user: %database_user%
password: %database_password%
charset: %database_charset%
wrapper_class: Magnum\DBAL\ReopeningConnectionWrapper
pagebuilder:
driver: %database_driver%
host: %database_host%
dbname: %pagebuilder_database_name%
user: %database_user%
password: %database_password%
charset: %database_charset%
wrapper_class: Magnum\DBAL\ReopeningConnectionWrapper
user:
driver: %database_driver%
host: %database_host%
dbname: %user_database_name%
user: %database_user%
password: %database_password%
charset: %database_charset%
wrapper_class: Magnum\DBAL\ReopeningConnectionWrapper
advertising:
driver: %database_driver%
host: %database_host%
dbname: %advertising_database_name%
user: %database_user%
password: %database_password%
charset: %database_charset%
wrapper_class: Magnum\DBAL\ReopeningConnectionWrapper
old_london_searchlight:
driver: %database_driver%
host: %legacy_database_host%
dbname: %london_searchlight_database_name%
user: %legacy_database_user%
password: %legacy_database_password%
charset: %database_charset%
wrapper_class: Magnum\DBAL\ReopeningConnectionWrapper

orm:
auto_generate_proxy_classes: %kernel.debug%
entity_managers:
comment:
connection: comment
metadata_cache_driver:
type: %orm_local_cache_type%
query_cache_driver:
type: %orm_local_cache_type%
mappings:
MagnumCommentBundle:
type: annotation
dir: Entity/
content:
connection: content
metadata_cache_driver:
type: %orm_local_cache_type%
query_cache_driver:
type: %orm_local_cache_type%
mappings:
MagnumContentBundle:
type: annotation
dir: Entity/
MagnumNewsletterBundle:
type: annotation
dir: Entity/
MagnumRelationatorBundle:
type: annotation
dir: Entity/
MagnumSearchNamedAreasBundle:
type: annotation
dir: Entity/
gedmo_tree:
type: annotation
prefix: Gedmo\Tree\Entity
dir: "%kernel.root_dir%/../../vendor/gedmo/doctrine-extensions/lib/Gedmo/Tree/Entity"
alias: GedmoTree
is_bundle: false
hydrators:
single_column: Magnum\Component\Hydration\SingleColumnHydrator
data_source:
connection: content
metadata_cache_driver:
type: %orm_local_cache_type%
query_cache_driver:
type: %orm_local_cache_type%
mappings:
MagnumDataSourceBundle:
type: annotation
dir: Entity/
MagnumMatchMakerBundle:
type: annotation
dir: Entity/
MagnumLondonLegacyBundle:
type: annotation
dir: Entity/
image:
connection: image
metadata_cache_driver:
type: %orm_local_cache_type%
query_cache_driver:
type: %orm_local_cache_type%
mappings:
MagnumImageBundle:
type: annotation
dir: Entity/
migration:
connection: migration
metadata_cache_driver:
type: %orm_local_cache_type%
query_cache_driver:
type: %orm_local_cache_type%
mappings:
MagnumMigrationBundle:
type: annotation
dir: Entity/
routing:
connection: routing
metadata_cache_driver:
type: %orm_local_cache_type%
query_cache_driver:
type: %orm_local_cache_type%
mappings:
MagnumRoutingBundle:
type: annotation
dir: Entity/
superlists:
connection: superlists
metadata_cache_driver:
type: %orm_local_cache_type%
query_cache_driver:
type: %orm_local_cache_type%
mappings:
MagnumSuperlistsBundle:
type: annotation
dir: Entity/
taxonomy:
connection: taxonomy
metadata_cache_driver:
type: %orm_local_cache_type%
query_cache_driver:
type: %orm_local_cache_type%
mappings:
MagnumTaxonomyBundle:
type: annotation
dir: Entity/
result_cache_driver:
type: %orm_cache_type%
host: %orm_cache_host%
port: %orm_cache_port%
instance_class: %orm_cache_instance_class%
class: %orm_cache_class%
slideshow:
connection: slideshow
metadata_cache_driver:
type: %orm_local_cache_type%
query_cache_driver:
type: %orm_local_cache_type%
mappings:
MagnumSlideshowBundle:
type: annotation
dir: Entity/
talent:
connection: content
mappings:
MagnumContentBundle:
type: annotation
dir: Entity/
MagnumTalentBundle:
type: annotation
dir: Entity/
pagebuilder:
connection: pagebuilder

facebook:
connection: facebook
mappings:
MagnumFacebookBundle:
type: annotation
dir: Entity/
change_log:
connection: log
metadata_cache_driver:
type: %orm_local_cache_type%
query_cache_driver:
type: %orm_local_cache_type%
mappings:
MagnumChangeLogBundle:
type: annotation
dir: Entity/
user:
connection: user
mappings:
MagnumUserBundle:
type: annotation
dir: Entity/
advertising:
connection: advertising
mappings:
MagnumAdvertisingBundle:
type: annotation
dir: Entity/
MagnumAdUnitBundle:
type: annotation


Luis Cordova comments:

exactly so you see each manager has its connection

if you use the wrong connection or the wrong manager on when doing getManager(X)

then the mapping is not recognized for that bundle under mappings key

2013-07-12

Florian Klein answers:

Hey,

It seems you use the entity manager named "data_source".

This manager has simply no mapping to your bundle.

Just add


mappings:
MagnumTalentBundle:
type: annotation
dir: Entity/


under the config of this entity manager to enable detection of this bundle's entities. (in doctrine.yml).

OR use another entityManager that has mapping registered for this entity (f.e "talent")

By the way, is it necessary to have that much amount of EMs ?