I have been following this tutorial https://symfony.com/doc/3.3/doctrine/multiple_entity_managers.html (but it's more for different connections/em on different Bundles) and then a few more links around the web to try and get different Entities in different databases WITHIN THE SAME BUNDLE, that interract together, I tried everything but nothing seem to be working...
At first I thought that the fact that all my entities are in the ModelBundle/Entity/ folder was the problem, so I actually moved all my default Entities in /ModelBundle/EntityInternal/ and the other entities I want to move to another database in the /ModelBundle/EntityRest/
(At the moment I'm just trying to make it work with 2 separate mysql databases, but the idea after that is to use the CircleOfNice/DoctrineRestDriver for the secondary EM)
This is my current config :
doctrine:
dbal:
default_connection: default
connections:
default:
driver: pdo_mysql
host: '%database_host%'
port: '%database_port%'
dbname: '%database_name%'
user: '%database_user%'
password: '%database_password%'
charset: UTF8
server_version: '5.6'
secondary:
driver: pdo_mysql
host: '%database_host2%'
port: '%database_port2%'
dbname: '%database_name2%'
user: '%database_user2%'
password: '%database_password2%'
charset: UTF8
server_version: '5.6'
orm:
#auto_generate_proxy_classes: '%kernel.debug%'
#naming_strategy: doctrine.orm.naming_strategy.underscore
#auto_mapping: true
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
ModelBundle:
type: "annotation"
dir: "EntityInternal"
prefix: "ModelBundle\\EntityInternal"
secondary:
connection: secondary
mappings:
ModelBundle:
type: "annotation"
dir: "EntityRest"
prefix: "ModelBundle\\EntityRest"
The first Entity I tried to move in the secondary connection/EM :
namespace ModelBundle\EntityRest;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
use ModelBundle\Model\ModelTrait\SeoTrait;
use ModelBundle\Model\ModelTrait\NameTrait;
/**
* ModelBundle\EntityRest\Level2
*
* #ORM\Table(name="level2")
* #ORM\Entity(repositoryClass="ModelBundle\Repository\LevelRepository")
*/
class Level2
{
use SeoTrait;
use NameTrait;
/**
* #var integer
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToMany(targetEntity="\ModelBundle\EntityInternal\Offer", mappedBy="levels2")
* #ORM\JoinColumn(name="offer_id", referencedColumnName="id", nullable=true)
*/
private $offers;
/**
* #ORM\ManyToMany(targetEntity="\ModelBundle\EntityInternal\User", mappedBy="levels2")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=true)
*/
private $users;
and the linked entity that remains in the default EM/Connection
/**
* ModelBundle\EntityInternal\Offer
*
* #ORM\Table(name="offer")
* #ORM\Entity(repositoryClass="ModelBundle\Repository\OfferRepository")
* #ORM\HasLifecycleCallbacks
*/
class Offer
{
use SeoTrait;
use DateTrait;
use ValidTrait;
use PremiumTrait;
use NameTrait;
/**
* #var integer
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
(....)
/**
* #ORM\ManyToMany(targetEntity="Level", inversedBy="offers")
* #ORM\JoinColumn(nullable=false)
*/
private $levels;
/**
* #ORM\ManyToMany(targetEntity="\ModelBundle\EntityRest\Level2", inversedBy="offers")
* #ORM\JoinColumn(nullable=false)
*/
private $levels2;
The doctrine:mapping:info outputs the right entities :
$ bin/console doctrine:mapping:info --em secondary
Found 1 mapped entities:
[OK] ModelBundle\EntityRest\Level2
$ bin/console doctrine:mapping:info
Found 17 mapped entities:
[OK] ModelBundle\EntityInternal\Domain
[OK] ModelBundle\EntityInternal\Application
[OK] ModelBundle\EntityInternal\Region
[OK] ModelBundle\EntityInternal\Offer
[OK] ModelBundle\EntityInternal\Diploma
[OK] ModelBundle\EntityInternal\ArticleCategory
[OK] ModelBundle\EntityInternal\City
[OK] ModelBundle\EntityInternal\County
[OK] ModelBundle\EntityInternal\User
[OK] ModelBundle\EntityInternal\Sector
[OK] ModelBundle\EntityInternal\Level
[OK] ModelBundle\EntityInternal\Company
[OK] ModelBundle\EntityInternal\SectorGroup
[OK] ModelBundle\EntityInternal\DomainGroup
[OK] ModelBundle\EntityInternal\Media
[OK] ModelBundle\EntityInternal\Article
[OK] ModelBundle\EntityInternal\Contract
But the schema:update just doesn't want to work
$ bin/console doctrine:schema:update --em secondary
[Doctrine\Common\Persistence\Mapping\MappingException]
The class 'ModelBundle\EntityInternal\Offer' was not found in the chain configured namespaces ModelBundle\EntityRest
$ bin/console doctrine:schema:update
[Doctrine\Common\Persistence\Mapping\MappingException]
The class 'ModelBundle\EntityRest\Level2' was not found in the chain configured namespaces ModelBundle\EntityInternal
I've been trying about everything for a few days now and that's always the error I fall back to... Do I have to move these entities in another bundle ? Is it just impossible for doctrine to link entities from different namespaces or connections ?
Related
Do all database tables have to have the same charset when using Symfony and Doctrine ORM or is there a possibility to set the charset for each table individually? How?
I just changed the charset for one database table from utf8 to utf8mb4 but it only seems to work properly if I also configure doctrine.dbal.charset to utf8mb4 in config.yaml.
I also tried to solve my problem by using this annotation in the Entity class:
/**
* #ORM\Entity
* #ORM\Table(
* name="my_table_name",
* options={
* "charset": "utf8mb4",
* "collate": "utf8mb4_general_ci"
* }
* )
*/
I want to use nelmio for symfony-project, but it doesn't work.
It always says: No operations defined in spec!
I also try the example on https://symfony.com/doc/current/bundles/NelmioApiDocBundle/index.html
Whats's wrong? Any ideas?
routing.yml
app.swagger_ui:
path: /api/doc
methods: GET
defaults: { _controller: nelmio_api_doc.controller.swagger_ui }
config.yml
nelmio_api_doc:
areas:
path_patterns: # an array of regexps
- ^/api(?!/doc$)
host_patterns:
- ^api\.
Controller
/**
* #Route("/api/test", methods={"GET"})
* #SWG\Response(
* response=200,
* description="Returns the rewards of an user"
* )
* #SWG\Parameter(
* name="order",
* in="query",
* type="string",
* description="The field used to order rewards"
* )
*/
public function testAction()
{
}
composer.json
"symfony/symfony": "3.4.*",
"nelmio/api-doc-bundle": "3.2.1",
Just remove
host_patterns:
- ^api\.
and set your virtual host in
documentation:
host: symfony.localhost
The assets normally are installed by composer if any command event (usually post-install-cmd or post-update-cmd) triggers the ScriptHandler::installAssets script. If you have not set up this script, you can manually execute this command:
php bin/console assets:install --symlink
The problem is in config.yml path patterns. If you remove the config(all nelmio_api_doc) or change the path patterns will work. Example:
nelmio_api_doc:
areas:
default:
path_patterns: [ /api/ ]
Running bin/console doctrine:schema:update --force again and again, it always output 39 queries executed even after clearing cache/restarting php-fpm. So it always execute the same SQL requests again and again...
The SQL looks likes (from --dump-sql)
ALTER TABLE apply_queue CHANGE cv_id cv_id INT DEFAULT NULL, CHANGE team_id team_id INT DEFAULT NULL;
....
And a lot of lines similar to this.
The ApplyQueue class look like:
/**
* AppBundle\Entity\ApplyQueue.
*
* #ORM\Entity(repositoryClass="AppBundle\Entity\ApplyQueueRepository")
* #ORM\Table(name="apply_queue")
*/
class ApplyQueue
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Cv")
* #ORM\JoinColumn(name="cv_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $cv;
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Team")
* #ORM\JoinColumn(name="team_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $team;
...
}
DBAL config :
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
types:
json: Sonata\Doctrine\Types\JsonType
review_status: AppBundle\Model\Enum\ReviewStatusEnumType
mapping_types:
review_status: string
The structure of the table:
This is an "old" project that has been upgraded to Symfony 3.4 and MariaDB 10.2.
Thanks
MariaDB 10.2 is not yet supported by Doctrine, causing this problem.
See linked PR : https://github.com/doctrine/dbal/pull/2825
I had similar problem few weeks ago. In my case it was that after update
--force
it shows some column must be set not null on
--dump-sql.
Again and again. I don't remember so good what was problem but it was something like this:
I had for same column set to be null and to have default value. Without doctrine that could even work somehow, but doctrine expects tables to be designed good.
What I want to say check your table. There is probably some error of this type. Goodluck!
check your database table type - i had a problem where the database type didnt support the feature doctrine trying to set.
Maybe run the sql on the database server and see if it 'works' / changes anything, then re-run
In your app/config/config.yml, check if doctrine cache is enabled as below :
doctrine:
orm:
metadata_cache_driver: redis
If so, you will have to flush redis cache:
php bin/console redis:flushdb --client=CLIENT_NAME -n
I'm using Symfony 2.7. I followed documentation on Symfony2 about How to Work with multiple Entity Managers and Connections. I tried every solutions on this site but without success. I have always the same error :
[Doctrine\Common\Persistence\Mapping\MappingException] The class
'Spot\OfferBundle\Entity\Offer' was not found in the chain configured
namespaces Dashboard\ProjectBundle\Entity
In my project, I have 2 bundles. Every bundle works with a different entity manager. Here my config file :
# Doctrine Configuration
doctrine:
dbal:
default_connection: default
connections:
default:
driver: %database2_driver%
host: %database2_host%
port: %database2_port%
dbname: %database2_name%
user: %database2_user%
password: %database2_password%
charset: UTF8
spot:
driver: %database3_driver%
host: %database3_host%
port: %database3_port%
dbname: %database3_name%
user: %database3_user%
password: %database3_password%
charset: UTF8
orm:
auto_generate_proxy_classes: %kernel.debug%
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
DashboardProjectBundle: ~
spot:
connection: spot
mappings:
SpotOfferBundle: ~
I have two entities with relations across bundles
The first :
namespace Dashboard\ProjectBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Project
*
* #ORM\Table(name="project")
* #ORM\Entity
*/
class Project
{
/**
* #var Spot\OfferBundle\Entity\Offer
*
* #ORM\ManyToOne(targetEntity="Spot\OfferBundle\Entity\Offer", inversedBy="projects")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="offer_id", referencedColumnName="id")
* })
*/
private $offer;
And the second :
namespace Spot\OfferBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Offer
*
* #ORM\Table(name="offer")
* #ORM\Entity(repositoryClass="Spot\OfferBundle\Entity\OfferRepository")
*/
class Offer {
/**
*
* #ORM\OneToMany(targetEntity="Dashboard\ProjectBundle\Entity\Project", mappedBy="offer")
*/
private $projects;
I try with use statement, I check AppKernel and Bundles are defined. I try with leading backslashes. But nothig works.
Make sure that you have set this in the config file:
assetic:
...
bundles: [ DashboardProjectBundle, SportOfferBundle]
UPDATE:
Also, try to set orm config like this:
orm:
auto_generate_proxy_classes: %kernel.debug%
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
DashboardProjectBundle: ~
SpotOfferBundle: ~
spot:
connection: spot
mappings:
SpotOfferBundle: ~
After working for several months, suddenly I'm getting this error when I try and run a command from the terminal. The "X" is a User entity, extending the FOSUserBundle.
Looking at some of the similar questions on here, the common errors seem to be incorrect annotations, not registering the bundle in the AppKernel, or not having auto_mapping enabled in the config.
The user class starts (I don't think the properties are necessary?) like this:
<?php
namespace Acme\UserBundle\Entity;
use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity
* #ORM\Table()
*/
class User extends BaseUser
{
// ...
}
The bundle is registered in the AppKernel:
$bundles = array(
// ...
new Acme\UserBundle\UserBundle(),
// ...
);
Auto mapping is enabled (and not disabled in either the _prod or _dev configs) as you can see here:
doctrine:
dbal:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
orm:
auto_generate_proxy_classes: %kernel.debug%
auto_mapping: true
Plus the FOSUserBundle is being told the correct entity to use:
fos_user:
db_driver: orm
firewall_name: main
user_class: Acme\UserBundle\Entity\User
Doesn't work?
I stumbled upon one question on here where it was solved by disabling eAccelerator. We do have eAccelerator running on the server, but after clearing the cache I've verified the local value in the phpinfo() output is disabled when running the command, as was already expected:
eaccelerator.enable => 0 => 1
eaccelerator.optimizer => 0 => 1
When I run the doctrine:mapping:info command I can see that my UserBundle is missing. I'm at a complete loss here as to what is missing or incorrect. Does anybody have any suggestions? It's worth mentioning that the front-end of the website works fine, which makes use of the User entity.
I got this issue and it was a sleepy mistake to remove the EntityRepository with the whole #Entity line, causing the "not a valid entity" error.
I spent a lot searching for the issue with the FOSUserBundle:User superclass map but the problem was on my class, I had to know that when it didnt appear on doctrine:mapping:info.