how to define variable in yaml symfony2 - symfony

I'm not really familiar with YAML so I open parameters.yml and config.yml files to see example how to use parameters or variable in YAML.
parameters.yml:
parameters:
database_driver: pdo_mysql
database_host: 127.0.0.1
database_port: 3306
database_name: homlist
config.yml:
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
But when I tried it with doctrine mapping yaml file like this:
parameters:
table_name: test
Mockizart\Bundle\BlogBundle\Entity\MockblogTag:
type: entity
table: "%table_name%"
it's error like this:
An exception occurred while executing 'SELECT count(DISTINCT %0_.id) AS sclr0 FROM %table_name% %0_':
this is my mapping file Resources\Config\Entity\MockblogTag
Mockizart\Bundle\BlogBundle\Entity\MockblogTag:
type: entity
table: mockblog_tag
indexes:
user_id:
columns:
- user_id
name:
columns:
- name
slug:
columns:
- slug
id:
id:
type: integer
nullable: false
unsigned: false
comment: ''
id: true
generator:
strategy: IDENTITY
fields:
dateCreated:
type: integer
nullable: false
unsigned: false
comment: ''
column: date_created
name:
type: string
nullable: false
length: 60
fixed: false
comment: ''
slug:
type: string
nullable: false
length: 100
fixed: false
comment: ''
totalPost:
type: integer
nullable: false
unsigned: false
comment: ''
column: total_post
manyToOne:
user:
targetEntity: ORD\UserBundle\Entity\User
joinColumn:
referencedColumnName: id
type: integer
nullable: false
unsigned: false
lifecycleCallbacks:
How to define variable in yaml symfony2 ?

The way of defining parameters it's correct, however I see from comments that your purpose is to configure the class used for User object:
As Cerad said you can't do that. But if you want to configure the class you use for the User, you can have a manager service class.
<?php
namespace YourNamespace\UserBundle\Manager;
use Doctrine\Common\Persistence\ObjectManager;
class UserManager
{
/**
* #var ObjectManager
*/
protected $em;
/**
* Your user class
*
* #var string
*/
protected $className;
public function __construct(ObjectManager $em, $class)
{
$this->em = $em;
$this->className = $class;
}
public function createInstance()
{
return new $this->className;
}
public function getRepository()
{
return $this->em->getRepository($this->className);
}
}
And the services definitions will be like this:
services:
your_user.manager:
class: YourNamespace\UserBundle\Manager\UserManager
arguments: ['#doctrine.orm.entity_manager', 'YourNamespace\UserBundle\Entity\User']
In your controller you can use this manager class like this:
$userManager = $this->get('your_user.manager');
$user = $userManager->createInstance();
I think this is a good way to have a central point when dealing with user object. And if someday for whatever reason you decide to use a different class for user you just modify the argument 'YourNamespace\UserBundle\Entity\User'.
Also in this way you can use 'YourNamespace\UserBundle\Entity\User' argument as parameter, so the definition will change to:
services:
your_user.manager:
class: Moneytablet\UserBundle\Manager\UserManager
arguments: ['#doctrine.orm.entity_manager', '%user_class%']
and in you parameters.yml you can have:
parameters:
user_class: YouNamespace\UserBundle\Entity\User
I really like working this way, you can create save(), remove() methods on manager class and so on. Also later on when creating new services you can inject this manager like a regular service if it's a dependency.
And if you want a new manager for a different entity, you can create a new service definition with different construct arguments, but with the same service class.

Related

Symfony - log to multiple log files in a service

I'm trying to inject multiple monolog handler into a service. Right now my parent class injects a logger and the children class injects another logger. My goal is it to be able to log specific actions to specific log files.
My service.yaml:
App\Services\PrinterManager:
arguments: ['#doctrine.orm.entity_manager','#logger', '', '', '', '','']
tags:
- { name: monolog.logger, channel: printerface}
App\Services\Printer\Printer:
autowire: true
autoconfigure: false
public: false
parent: App\Services\PrinterManager
arguments:
index_2: '#logger'
index_3: '#oneup_flysystem.printer_invoice_filesystem'
index_4: '#oneup_flysystem.printerface_content_filesystem'
index_5: '#oneup_flysystem.sftp_filesystem'
index_6: '#App\Services\PrinterApiService'
tags:
- { name: monolog.logger, channel: printerlog}
My monolog.yaml:
monolog:
handlers:
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
channels: ["!event, !printerface", "!printerlog"]
printerface:
type: stream
level: debug
channels: ["printerface"]
path: "%kernel.logs_dir%/printerface.log"
printerlog:
type: stream
level: debug
channels: ["printerlog"]
path: "%kernel.logs_dir%/printerlog.log"
But it seems that the current service configuration breaks the constructor and I get the following error:
The argument must be an existing index or the name of a constructor's parameter.
Is there any way to use two log files in a service?
I've not done it with a parent/child class, but with something a little simpler I'm using named parameters, this is what I have (with three different loggers):
# App/Subscribers/WebhookLoggingListener.php file
public function __construct(
LoggerInterface $logger,
LoggerInterface $mailgunLog,
LoggerInterface $dripLog) {
}
# services.yml
App\Subscribers\WebhookLoggingListener:
arguments:
$logger: "#logger"
$mailgunLog: "#monolog.logger.mailgun"
$dripLog: "#monolog.logger.drip"
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
If I was using the other loggers elsewhere I could also bind them to specific variable names:
services:
_defaults:
# ... other config
bind:
$dripLog: "#?monolog.logger.drip"
This is the method that Symfony is using to replace parent's arguments in a child service:
/**
* You should always use this method when overwriting existing arguments
* of the parent definition.
*
* If you directly call setArguments() keep in mind that you must follow
* certain conventions when you want to overwrite the arguments of the
* parent definition, otherwise your arguments will only be appended.
*
* #param int|string $index
* #param mixed $value
*
* #return self the current instance
*
* #throws InvalidArgumentException when $index isn't an integer
*/
public function replaceArgument($index, $value)
{
if (\is_int($index)) {
$this->arguments['index_'.$index] = $value;
} elseif (0 === strpos($index, '$')) {
$this->arguments[$index] = $value;
} else {
throw new InvalidArgumentException('The argument must be an existing index or the name of a constructor\'s parameter.');
}
return $this;
}
As you can see, the indexes must be eighter same the argument variable names in the parent's constructor with a prefixed $ or an integer indicating the related argument.
So I think you must define your child service as below:
App\Services\Printer\Printer:
autowire: true
autoconfigure: false
public: false
parent: App\Services\PrinterManager
arguments:
2: '#logger'
3: '#oneup_flysystem.printer_invoice_filesystem'
4: '#oneup_flysystem.printerface_content_filesystem'
5: '#oneup_flysystem.sftp_filesystem'
6: '#App\Services\PrinterApiService'
tags:
- { name: monolog.logger, channel: printerlog}
Update:
After I reproduced your problem, I figured out that the solution is as below. With this solution, the Symfony autowiring will work for the child service.
App\Services\Printer\Printer:
autowire: true
autoconfigure: false
public: false
parent: App\Services\PrinterManager
arguments:
$arg2: '#logger'
$arg3: '#oneup_flysystem.printer_invoice_filesystem'
$arg4: '#oneup_flysystem.printerface_content_filesystem'
$arg5: '#oneup_flysystem.sftp_filesystem'
$arg6: '#App\Services\PrinterApiService'
tags:
- { name: monolog.logger, channel: printerlog}
$arg2, $arg3, $arg4, $arg5 and $arg6 must be replaced by your class constructor's argument names.

Doctrine. Cache. Entity relations

I have two Entities Company and Storage with One-To-Many Bidirectional relationship. Entities and their relations are cached (doctrine second level cache). The issue is that, when i create a new Storage entity, Company storages collection doesn't have this new entity until I clear the cache manually.
AppBundle\Entity\Main\Company:
type: entity
table: main.company
cache:
usage: NONSTRICT_READ_WRITE
id:
id:
type: integer
nullable: false
id: true
generator:
strategy: IDENTITY
fields:
legalName:
type: string
nullable: false
length: 255
options:
fixed: false
column: legal_name
oneToMany:
storages:
targetEntity: AppBundle\Entity\Main\Storage
mappedBy: company
cascade: ["all"]
orphanRemoval: true
cache:
usage: NONSTRICT_READ_WRITE
AppBundle\Entity\Main\Storage:
type: entity
table: main.storage
cache:
usage: NONSTRICT_READ_WRITE
id:
id:
type: integer
nullable: false
options:
unsigned: false
id: true
generator:
strategy: IDENTITY
fields:
storageName:
type: string
nullable: true
length: 255
options:
fixed: false
column: storage_name
manyToOne:
company:
targetEntity: AppBundle\Entity\Main\Company
cascade: ["all"]
fetch: LAZY
mappedBy: null
inversedBy: storages
joinColumns:
company_id:
referencedColumnName: id
orphanRemoval: false
cache:
usage: NONSTRICT_READ_WRITE
This is action where Storage is created. There is nothing unusual.
public function addAction(Request $request)
{
$form = $this->createForm(StorageAddType::class, null);
$form->handleRequest($request);
if (!$form->isSubmitted()) {
throw new \RuntimeException('Некорректный запрос');
}
if (!$form->isValid()) {
throw new \Symfony\Component\Validator\Exception\ValidatorException((string)$form->getErrors(true));
}
$doctrine = $this->getDoctrine();
/**
* #var Storage $storage
*/
$storage = $form->getData();
$manager = $doctrine->getManager();
$manager->persist($storage);
$manager->flush();
return $this->createAjaxDataResponse($this->createSuccessMessage('Storage successfully added'));
}
Such behavior is watched only when i try to create new Entity (Storage). Then on update/delete actions - Storages collection of Company are updated.
You are clearly wrong with persisting data. You try to persist unserialized object from form into uknown repository via manager.
Try this:
public function addAction(Request $request)
{
$form = $this->createForm(StorageAddType::class, null);
$form->handleRequest($request);
$em = $this->getDoctrine()->getManager();
if($form->isSubmitted() && $form->isValid())
{
$storage = new Storage();
$storage->setVal1($form->get('Val1'));
$storage->setVal2($form->get('Val2'));
$em->persist($storage);
$em->flush();
return $this->createAjaxDataResponse($this->createSuccessMessage('Storage successfully added'));
}
return $this->render('YOUR_TWIG_LAYOUT', [
'form' => $form->createView()
]);
}
You can also try to persist whole object, if form is seriaized properly by serializing data into entity. Write method like setValsFromForm($data) and serialize vars from $data form.
Then change these lines:
$storage->setVal1($form->get('Val1'));
$storage->setVal2($form->get('Val2'));
into
$storage->setValsFromForm($form->getData());
Also:
Exceptions and form validations should be handled by Form Validator in form class, not in controller. Exception is when you create form via formbuilderinterface in the controller, but you add logic there, not outside $form class.

Symfony2 JMS serializer add custom property

As the title states, I am trying to add a custom property to the serialized object I return.
Let's take a User with the following methods:
getFirstname, setFirstname
getLastname, setLastname
getUsername, setUsername
...
Now in the serialization I would like to add a property fullName: Firstname + Lastname.
I have a getter method in my entity like so:
/**
* get name
*
* #return string
*/
public function getName()
{
return $this->getFirstname()." ".$this->getLastname();
}
My serialization file looks something like this:
AppBundle\Entity\User:
exclusion_policy: ALL
properties:
id:
expose: false
username:
expose: true
groups: [list, details]
email:
expose: true
groups: [details]
name:
expose: true
groups: [list, details]
I have tried with
name:
expose: true
groups: [list, details]
access_type: public_method
type: string
serialized_name: fullName
accessor:
getter: getName
and other variants but I can't seem to get it right.
Note: Yes I've cleared my cache and tried it again.
Anyone able to tell me what I am missing ?
Thanks in advance !
Since your full name is not a property at all you have to define a virtual property:
AppBundle\Entity\User:
exclusion_policy: ALL
properties:
# All properties but not name
virtual_properties:
getName:
groups: [list, details]
serialized_name: fullName

Symfony classNotFoundInNamespaces exception

I am working with a bundle called rss-atom-bundle. Through this bundle I should be able to fetch the RSS feeds and save them to the database.
I can get the bundle to work up till getting the feeds from the URL but when I try to presist I get the following error and I just cant figure it out why I am getting it.
The class 'Debril\RssAtomBundle\Protocol\Parser\Item' was not found in the chain configured namespaces Symfony\Bundle\AsseticBundle\Entity, AppBundle\Entity`
Looking online and at stackoverflow i came across questions that were related to this but all of them are talking about by default the Entity should be inside the Entity folder of the Bundle and if they are not then this error appears but my Entity files are inside Entity folder and I followed the instructions given by the bundle author but still i cant get passed this error
This is what my Controller looks like
$em = $this->getDoctrine()->getManager();
// fetch the FeedReader
$reader = $this->container->get('debril.reader');
// this date is used to fetch only the latest items
$unmodifiedSince = '01/01/2000';
$date = new \DateTime($unmodifiedSince);
// the feed you want to read
$url = 'http://example.com/feed/';
$feeds = $reader->getFeedContent($url, $date);
$items = $feeds->getItems();
dump($items);
foreach ( $items as $item ) {
$em->persist($item);
}
$em->flush();
As per bundle instructions I did implement FeedInterface to my Feed Entity and ItemInInterface, ItemOutInterface to my Item Entity
This is what my orm.yml looks like for Feed and Item Entity
Feed
AppBundle\Entity\Feed:
type: entity
table: null
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
title:
type: string
length: 255
description:
type: text
link:
type: text
publicId:
type: text
lastModified:
type: datetime
lastViewed:
type: datetime
oneToMany:
items:
targetEntity: AppBundle\Entity\Item
mappedBy: feed
cascade: ["persist"]
lifecycleCallbacks: { }
Item
AppBundle\Entity\Item:
type: entity
table: null
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
title:
type: string
length: 255
title:
type: text
nullable: true
summary:
type: text
nullable: true
description:
type: text
nullable: true
medias:
type: text
nullable: true
updated:
type: string
nullable: true
publicId:
type: string
nullable: true
link:
type: string
nullable: true
comment:
type: string
nullable: true
author:
type: string
nullable: true
manyToOne:
feed:
targetEntity: AppBundle\Entity\Feed
inversedBy: items
joinColumn:
name: feed_id
referencedColumnName: id
Any help will be really appreciated as I am clueless why i am getting the error?
Try to add RssAtomBundle to the list with mappings in app/config/config.yml
orm:
entity_managers:
default:
connection: default
mappings:
AppBundle: ~
AsseticBundle: ~
RssAtomBundle: ~

Doctrine 2.4 Association Mapping Failure referenced column name primary key ignored

When running:
php app/console doctrine:schema:validate
I receive the error:
[Mapping] FAIL - The entity-class 'Path\ToBundle\Entity\Variant' mapping is invalid:
* The referenced column name 'localsku' has to be a primary key column on the target entity class 'Path\ToBundle\Entity\Inventory'.
The killer here is that 'localsku' is indeed a primary key. Am I missing something major here?
Thanks in advance for any assistance, and I apologize if I've missed some key piece of information.
Variant Entity is defined as:
Path\ToBundle\Entity\Variant:
type: entity
table: variant
uniqueConstraints:
sku:
columns:
- sku
id:
variantId:
type: integer
nullable: false
unsigned: false
comment: ''
id: true
column: variant_id
generator:
strategy: IDENTITY
fields:
name:
type: string
nullable: false
length: 255
fixed: false
comment: ''
sku:
type: string
nullable: false
length: 255
fixed: false
comment: ''
oneToOne:
inventory:
targetEntity: Inventory
cascade: { }
mappedBy: null
inversedBy: variant
joinColumns:
sku:
referencedColumnName: localsku
orphanRemoval: false
lifecycleCallbacks: { }
Inventory Entity is defined as:
Path\ToBundle\Entity\Inventory:
type: entity
table: Inventory
id:
localsku:
type: string
nullable: false
length: 255
fixed: false
comment: ''
id: true
column: LocalSKU
generator:
strategy: IDENTITY
fields:
itemname:
type: string
nullable: true
length: 255
fixed: false
comment: ''
column: ItemName
qoh:
type: integer
nullable: true
unsigned: false
comment: ''
column: QOH
location:
type: string
nullable: true
length: 250
fixed: false
comment: ''
column: Location
barcode:
type: string
nullable: true
length: 25
fixed: true
comment: ''
column: Barcode
oneToOne:
variant:
targetEntity: Variant
cascade: { }
mappedBy: inventory
inversedBy: null
joinColumns:
localsku:
referencedColumnName: sku
orphanRemoval: false
lifecycleCallbacks: { }
Variant Entity reference to Inventory:
/**
* Inventory
*
* #var \Path\ToBundle\Entity\Inventory
*
* #ORM\OneToOne(targetEntity="Path\ToBundle\Entity\Inventory", inversedBy="variant")
* #ORM\JoinColumn(name="sku", referencedColumnName="localsku")
*/
protected $inventory;
Inventory Entity reference to Variant:
/**
* Variant
*
* #var \Path\ToBundle\Entity\Variant
*
* #ORM\OneToOne(targetEntity="Path\ToBundle\Entity\Variant", inversedBy="inventory")
* #ORM\JoinColumn(name="localsku", referencedColumnName="sku")
*/
protected $variant;
Your association mapping is wrong.
It should be something like that, assuming Variant table have a "sku" field.
Variant Entity :
/**
* Inventory
*
* #var \Path\ToBundle\Entity\Inventory
*
* #ORM\OneToOne(targetEntity="Path\ToBundle\Entity\Inventory", inversedBy="variant")
* #ORM\JoinColumn(name="sku", referencedColumnName="localsku")
*/
protected $inventory;
Inventory Entity :
/**
* Variant
*
* #var \Path\ToBundle\Entity\Variant
*
* #ORM\OneToOne(targetEntity="Path\ToBundle\Entity\Variant", mappedBy="inventory")
*/
protected $variant;
http://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html#one-to-one-bidirectional
P.S : I'm curious. You are using both yml configuration and annotation for the same entities ? I'm not sure that doctrine is able to merge them, does it works ?

Resources