I am using Symfony 2.8, FOSUserBundle 1.3 and FOSOAuthServerBundle 1.5
For all of the class needed to those Bundles to work, I ended up with doctrine not updating my schema properly. I mean it doesn't take into account the fields from the Base Class.
CREATE TABLE oauh2_access_tokens (id INT NOT NULL, client_id INT NOT NULL, user_
id INT DEFAULT NULL, PRIMARY KEY(id));
CREATE TABLE oauth2_auth_codes (id INT NOT NULL, client_id INT NOT NULL, user_id
INT DEFAULT NULL, PRIMARY KEY(id));
CREATE TABLE oauth2_clients (id INT NOT NULL, PRIMARY KEY(id));
CREATE TABLE oauth2_refresh_tokens (id INT NOT NULL, client_id INT NOT NULL, use
r_id INT DEFAULT NULL, PRIMARY KEY(id));
Here's my config:
doctrine:
orm:
#auto_generate_proxy_classes: "%kernel.debug%"
default_entity_manager: default
entity_managers:
default:
connection: default
naming_strategy: doctrine.orm.naming_strategy.underscore
mappings:
COMPANYAuthBundle: ~
fos_user:
db_driver: orm
firewall_name: api
user_class: COMPANY\AuthBundle\Entity\User
#FOSOAuthBundle Configuration
fos_oauth_server:
db_driver: orm
client_class: COMPANY\AuthBundle\Entity\Client
access_token_class: COMPANY\AuthBundle\Entity\AccessToken
refresh_token_class: COMPANY\AuthBundle\Entity\RefreshToken
auth_code_class: COMPANY\AuthBundle\Entity\AuthCode
service:
user_provider: fos_user.user_manager
And here's my class User
<?php
namespace COMPANY\AuthBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Entity\User as BaseUser;
/**
* Utilisateur
*
* #ORM\Table(name="users")
* #ORM\Entity
*/
class User extends BaseUser
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
public function __construct()
{
parent::__construct();
// your own logic
}
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
}
So, yes I did put the right use and not FOS\UserBundle\Model\User as BaseUser;
Same thing for the class of OAuthServerBundle: (I'm just putting one here, they're all following the same pattern)
<?php
namespace COMPANY\AuthBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\OAuthServerBundle\Entity\Client as BaseClient;
/**
* Client
*
* #ORM\Table(name="oauth2_clients")
* #ORM\Entity(repositoryClass="COMPANY\AuthBundle\Repository\ClientRepository")
*/
class Client extends BaseClient
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
public function __construct()
{
parent::__construct();
}
}
Does anybody have an idea why base class' fields aren't put into my db? Thanks :)
Okay, I found the solution after 10 hours of search...
And the solution is to not forget to add FOSUserBundle and FOSOAuthServerBundle and all the base class bundles to your mapping.....
So this should be the config:
doctrine:
orm:
#auto_generate_proxy_classes: "%kernel.debug%"
default_entity_manager: default
entity_managers:
default:
connection: default
naming_strategy: doctrine.orm.naming_strategy.underscore
mappings:
COMPANYAuthBundle: ~
FOSUserBundle: ~
FOSOAuthBundle: ~
Also, you can't make a bundle inherit from two bundles if you want to use the routes of each. So, create one bundle for each. Then in each of the Bundle class, add the following function:
public function getParent()
{
return "FOSOAuthServerBundle"; //Or return "FOSUserBundle"; but you can't put both
}
Related
I've made some changes to my doctrine Entities and need to update the database and got the following error.
$ php bin/console doctrine:schema:update -vvv
[Doctrine\DBAL\Schema\SchemaException (30)]
There is no column with name 'fleet_no' on table 'fuelData'.
Exception trace:
() at /home/sarah/workspace/telematics_tracker/vendor/doctrine/dbal/lib/Doctrine/DBAL/Schema/SchemaException.php:86
Doctrine\DBAL\Schema\SchemaException::columnDoesNotExist() at /home/sarah/workspace/telematics_tracker/vendor/doctrine/dbal/lib/Doctrine/DBAL/Schema/Table.php:671
Doctrine\DBAL\Schema\Table->getColumn() at /home/sarah/workspace/telematics_tracker/vendor/doctrine/dbal/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php:711
Doctrine\DBAL\Platforms\MySqlPlatform->getPreAlterTableAlterPrimaryKeySQL() at /home/sarah/workspace/telematics_tracker/vendor/doctrine/dbal/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php:639
Doctrine\DBAL\Platforms\MySqlPlatform->getPreAlterTableIndexForeignKeySQL() at /home/sarah/workspace/telematics_tracker/vendor/doctrine/dbal/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php:621
Doctrine\DBAL\Platforms\MySqlPlatform->getAlterTableSQL() at /home/sarah/workspace/telematics_tracker/vendor/doctrine/dbal/lib/Doctrine/DBAL/Schema/SchemaDiff.php:199
Doctrine\DBAL\Schema\SchemaDiff->_toSql() at /home/sarah/workspace/telematics_tracker/vendor/doctrine/dbal/lib/Doctrine/DBAL/Schema/SchemaDiff.php:126
Doctrine\DBAL\Schema\SchemaDiff->toSaveSql() at /home/sarah/workspace/telematics_tracker/vendor/doctrine/orm/lib/Doctrine/ORM/Tools/SchemaTool.php:883
Doctrine\ORM\Tools\SchemaTool->getUpdateSchemaSql() at /home/sarah/workspace/telematics_tracker/vendor/doctrine/orm/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/UpdateCommand.php:115
Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand->executeSchemaCommand() at /home/sarah/workspace/telematics_tracker/vendor/doctrine/orm/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/AbstractCommand.php:65
Doctrine\ORM\Tools\Console\Command\SchemaTool\AbstractCommand->execute() at /home/sarah/workspace/telematics_tracker/vendor/doctrine/doctrine-bundle/Command/Proxy/UpdateSchemaDoctrineCommand.php:50
Doctrine\Bundle\DoctrineBundle\Command\Proxy\UpdateSchemaDoctrineCommand->execute() at /home/sarah/workspace/telematics_tracker/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php:261
Symfony\Component\Console\Command\Command->run() at /home/sarah/workspace/telematics_tracker/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:839
Symfony\Component\Console\Application->doRunCommand() at /home/sarah/workspace/telematics_tracker/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:185
Symfony\Component\Console\Application->doRun() at /home/sarah/workspace/telematics_tracker/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:80
Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at /home/sarah/workspace/telematics_tracker/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:116
Symfony\Component\Console\Application->run() at /home/sarah/workspace/telematics_tracker/bin/console:27
Which is correct as such, since I have a column called 'fleetNo' but not one called 'fleet_no' in the 'fuelData' table. This was not one of the changes that I made either.
I have searched the project directory for any other instance of 'fleet_no' but there aren't any.
Below is a copy of the fuelData entity.
<?php
// src/AppBundle/Entity/FuelData.php
namespace AppBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\Column;
/**
* #ORM\Entity
* #ORM\Table(name="fuelData")
*
*
*/
class FuelData
{
/**
* #Assert\NotBlank()
* #ORM\Column(type="string")
* #ORM\Id
*/
public $fleetNo;
/**
* #Assert\NotBlank()
* #ORM\Column(type="string")
*/
public $startDate;
/**
* #Assert\NotBlank()
* #ORM\Column(type="string")
*/
public $endDate;
/**
* Set fleetNo
*
* #param string $fleetNo
*
* #return FuelData
*/
public function setFleetNo($fleetNo)
{
$this->fleetNo = $fleetNo;
return $this;
}
/**
* Get fleetNo
*
* #return string
*/
public function getFleetNo()
{
return $this->fleetNo;
}
/**
* Set startDate
*
* #param string $startDate
*
* #return FuelData
*/
public function setStartDate($startDate)
{
$this->startDate = $startDate;
return $this;
}
/**
* Get startDate
*
* #return string
*/
public function getStartDate()
{
return $this->startDate;
}
/**
* Set endDate
*
* #param string $endDate
*
* #return FuelData
*/
public function setEndDate($endDate)
{
$this->endDate = $endDate;
return $this;
}
/**
* Get endDate
*
* #return string
*/
public function getEndDate()
{
return $this->endDate;
}
}
The only thing that I can think of is that the column name is being converted from camelCase to underscore by something.
Is there something that I have missed or should I be looking in a different place?
Below are the relevant parts of my config.yml:
doctrine:
dbal:
default_connection: maxdb
connections:
maxdb:
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
foxdb:
driver: pdo_mysql
host: "%database_host2%"
port: "%database_port2%"
dbname: "%database_name2%"
user: "%database_user2%"
password: "%database_password2%"
charset: UTF8
orm:
auto_generate_proxy_classes: "%kernel.debug%"
default_entity_manager: maxem
entity_managers:
maxem:
naming_strategy: doctrine.orm.naming_strategy.underscore
connection: maxdb
mappings:
AppBundle: ~
BWTCalendarBundle: ~
BWTFMBundle: ~
BWTHealthCheckBundle: ~
BWTSkytrackBundle: ~
BWTTelematicsBundle: ~
foxem:
naming_strategy: doctrine.orm.naming_strategy.underscore
connection: foxdb
mappings:
FoxBundle: ~
orm:
auto_generate_proxy_classes: "%kernel.debug%"
default_entity_manager: maxem
entity_managers:
maxem:
naming_strategy: doctrine.orm.naming_strategy.underscore
As you can see in your config you are using the UNDERSCORE naming strategy for the orm so that is what converts your field name.
You should do
app/console doctrine:schema:drop
to start off from clean then try changing the naming strategy.
In the end if you want to be sure to have the exact column name you want just add the name parameter to the colum annotation like so:
/**
* #Assert\NotBlank()
* #ORM\Column(type="string" name="fleetNo")
* #ORM\Id
*/
public $fleetNo;
Entity 1
<?php
namespace Operat\SystemBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Operat\AgentsBundle\Entity\Agents;
/**
* Liensao
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="Operat\SystemBundle\Entity\LiensaoRepository")
*/
class Liensao
{
// Liensao - Agents
/**
* #ORM\ManyToOne(targetEntity="Operat\AgentsBundle\Entity\Agents", inversedBy="liensao")
* #ORM\JoinColumn(name="colAgent_Code", referencedColumnName="CODE")
*/
protected $agent;
/*....*/
Entity 2
<?php
namespace Operat\AgentsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Operat\SystemBundle\Entity\Liensao;
/**
* Agents
*
* #ORM\Table(name="agents", indexes={#ORM\Index(name="Valide", columns={"Valide"}), #ORM\Index(name="GL", columns={"GL"}), #ORM\Index(name="NSOC", columns={"NSOC"})})
* #ORM\Entity
*/
class Agents
{
// Agents - Liensao
/**
* #ORM\OneToMany(targetEntity="\Operat\SystemBundle\Entity\Liensao", mappedBy="agent")
*/
protected $liensao;
Yet running
php app/console doctrine:schema:update --em=entities_system --dump-sql --force
gives the following error:
[Doctrine\Common\Persistence\Mapping\MappingException]
The class 'Operat\AgentsBundle\Entity\Agents' was not found in the chain configured namespaces Operat\SystemBundle\Entity, Operat\AdministrationBundle\Entity, FOS\UserBundle\Entity
Config.yml
orm:
auto_generate_proxy_classes: "%kernel.debug%"
default_entity_manager: entities_system
entity_managers:
entities_system:
connection: operat_system
mappings:
OperatSystemBundle: ~
FOSUserBundle: ~
entities_agents:
connection: operat_agents
mappings:
OperatAgentsBundle: ~
I'm using FOSMessageBundle, and I thought i followed the instructions pretty well, but i cant seem to get the database to generate properly...
Heres my Message entity:
<?php
namespace Acme\Bundle\DemoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;
use FOS\MessageBundle\Entity\Message as BaseMessage;
use FOS\MessageBundle\Model\ParticipantInterface;
/**
* Message
*
* #ORM\Entity()
* #JMS\ExclusionPolicy("All")
*/
class Message extends BaseMessage implements EntityInterface
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
* #JMS\Groups({"list", "default"})
* #JMS\Expose()
*/
protected $id;
/**
* #var Thread
*
* #ORM\ManyToOne(targetEntity="Thread", inversedBy="messages", cascade={"persist"})
* #ORM\JoinColumn(name="thread_id")
* #JMS\Groups({"default"})
*/
protected $thread;
/**
* #ORM\ManyToOne(targetEntity="User")
* #var ParticipantInterface
*/
protected $sender;
/**
* #ORM\OneToMany(targetEntity="MessageMetadata", mappedBy="message", cascade={"all"})
* #var MessageMetadata
*/
protected $metadata;
}
And my config.yml
fos_message:
db_driver: orm
thread_class: Acme\Bundle\DemoBundle\Entity\Thread
message_class: Acme\Bundle\DemoBundle\Entity\Message
The issue is, my table ends up with only id, thread_id, and sender_id. Its missing the rest of the fields.
What am i missing!
check if all classes are correcly mapped :
php app/console doctrine:mapping:info
if not you have to MessageMetadata to the config file
message_class: Acme\Bundle\DemoBundle\Entity\Message
I'm not sure but in your case it seems like you have two different configurations for this entity - your annotations and xml from FOSCommentBundle
Please change your configuration to XML format, like this https://github.com/FriendsOfSymfony/FOSMessageBundle/blob/master/Resources/config/doctrine/Message.orm.xml and check again.
regards,
Merk, one of the contributors to the project pointed me at setting auto_mapping to true under the entity manager.
Once i set this, it solved my issue!
For me the auto_mapping did not work, I got the message
Unrecognized option "auto_mapping" under "doctrine.orm"
I solved the issue by adding the FOSMessageBundle like this:
orm:
auto_generate_proxy_classes: "%kernel.debug%"
default_entity_manager: default
entity_managers:
default:
naming_strategy: doctrine.orm.naming_strategy.underscore
connection: default
second_level_cache:
enabled: true
mappings:
AppBundle: ~
UserBundle: ~
FOSMessageBundle: ~
My boss installed this bundle for the softdelete filter, but the documentation is beyond sparse. How do I use this in my delete queries?
Enable it in your config:
stof_doctrine_extensions:
orm:
default:
...
softdeleteable: true
doctrine:
...
orm:
filters:
softdeleteable:
class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
enabled: true
Then in your entity:
<?php
namespace Foo\BarBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* ...
* #Gedmo\SoftDeleteable(fieldName="deletedAt")
* #ORM\Entity
*/
class Foo
{
/**
* #var \DateTime $deletedAt
*
* #ORM\Column(name="deleted_at", type="datetime", nullable=true)
*/
private $deletedAt;
Then just delete entities like you normally would (the extension takes care of the rest):
$em = $this->getDoctrine()->getManager();
$em->remove($entity);
$em->flush();
I also needed another puzzle part: The doctrine yaml config:
XYBundle\Entity\Adresse:
type: entity
table: adresse
gedmo:
soft_deleteable:
field_name: deleted_at
time_aware: false
id:
id:
type: integer
generator: { strategy: AUTO }
fields:
ort:
type: string
length: 100
plz:
type: string
columnDefinition: varchar(255) NOT NULL DEFAULT ''
deleted_at:
type: datetime
nullable: true
I have a pretty standard Entity with the correct imports:
/**
* Budhaz\aMailerBundle\Entity\Instance
*
* #ORM\Table()
* #ORM\Entity
*/
class Instance {
use TimestampableEntity;
/** #ORM\Id #ORM\GeneratedValue #ORM\Column(type="integer") */
private $id;
...
}
But I would like to remove the createdAt (and updatedAt) from my form so the user don't and can't set them, so I remove it from the InstanceForm:
class InstanceType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('startAt')
->add('endAt')
//->add('createdAt')
//->add('updatedAt')
->add('campaign')
;
}
...
}
But now I have this error:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'createdAt' cannot be null
createdAt and updatedAt should automaticaly be set by Doctrine, but it stays null, anyone know why?
You have to set the values within the class manually. Then you can tell doctrine to set the new value before every update:
public function __construct() {
$this->setCreatedAt(new \DateTime());
$this->setUpdatedAt(new \DateTime());
}
/**
* #ORM\PreUpdate
*/
public function setUpdatedAtValue() {
$this->setUpdatedAt(new \DateTime());
}
app/config/config.yml
stof_doctrine_extensions:
orm:
default:
timestampable: true
You need to check if the bundle is installed. If not, run:
composer require stof/doctrine-extensions-bundle
It should create file: app/config/packages/stof_doctrine_extensions.yaml with the content:
stof_doctrine_extensions:
default_locale: en_US
orm:
default:
timestampable: true