Symfony - can't get multiple file upload to work - symfony

I'm using VichUploaderBundle to upload files for a gallery. I have two entities - the first one is Gallery, and the second is GalleryImage.
THis is the code:
http://pastebin.com/2nWAK2iH - Gallery Entity
http://pastebin.com/JWzWzZp2 - GalleryImage Entity
http://pastebin.com/TnZr24Sp - GalleryImageType Form
http://pastebin.com/9C8H25sn - GalleryType Form
http://tny.cz/26e859f6 - The Controller (part of it)
No matter what I do, I always get
"An exception occurred while executing 'INSERT INTO GalleryImage (image_name, gallery_id) VALUES (?, ?)' with params [null, 8]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'image_name' cannot be null"
Any ideas what I'm doing wrong?
Thanks
UPDATE
The new GalleryImage file: http://pastebin.com/uKtqg4HE

Looks like the #Uploadable class annotation is missing in the GalleryImage class.

Related

How to override Product model in Sylius?

I am working on Sylius 1.2.8. I have overridden Product model and it is working fine but when I add a new product with attributes:
<?php
...
$em = $this->container->get('sylius.manager.product');
/**
* #var \Sylius\Component\Product\Model\ProductAttributeValueInterface $attribute
* #var \Sylius\Component\Core\Model\ProductInterface $product
*/
$product->addAttribute($attribute);
$em->persist($product);
$em->flush();
It throws this error:
An exception occurred while executing 'INSERT INTO
sylius_product_attribute_value (locale_code, text_value,
boolean_value, integer_value, float_value, datetime_value, date_value,
json_value, product_id, attribute_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?,
?, ?)' with params ["en_US", null, null, null, null, null, null,
"[\"013ea12a-1aff-4050-8107-20b53ada73ce\"]", null, 28]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'product_id' cannot be null
My custom Product Model looks like this:
namespace AppBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Sylius\Component\Core\Model\Product as BaseProduct;
class Product extends BaseProduct implements ProductInterface
{
public function __construct()
{
parent::__construct();
}
}
And my config file:
sylius_product:
driver: doctrine/orm
resources:
product:
classes:
repository: AppBundle\Doctrine\ORM\ProductRepository
model: AppBundle\Entity\Product
I have found a similar question asked :stackoverflow.com/q/22919004/6248367. But this does not solve my problem neither does it answer why application fails. This answer is also 4 years old. Can someone help me fix this?
Edit: This error is also thrown in admin whenever I create a new product and add attributes to it at the same time. Workaround is to first create product, then add attributes.
I have figured out the problem. As #czende mentioned properly check the configuration.
In my case I had set the wrong class in subject while overriding the attribute:
sylius_product:
driver: doctrine/orm
resources:
product:
classes:
repository: AppBundle\Doctrine\ORM\ProductRepository
model: AppBundle\Entity\Product
sylius_attribute:
driver: doctrine/orm
resources:
product:
# Make sure to provide the correct Product class in the subject.
subject: AppBundle\Entity\Product
attribute:
classes:
model: AppBundle\Entity\ProductAttribute
repository: AppBundle\Doctrine\ORM\ProductAttributeRepository
Also make sure to use mappedSuperclass in the mapping config instead of entity as commented by MichaƂ Marcinkowski in issue #3997
AppBundle\Entity\ProductAttribute:
type: mappedSuperclass
table: sylius_product_attribute
EDIT: To debug what's wrong, start by checking warnings in symfony debug bar. It can narrow down your search area for bug. It can also hint you if something is wrong in your configuration file.

can't update database symfony 4 with sonataClassificationBundle

im using Symfony 4.1
When you install SonataMediaBundle and SonataClassificationBundle after configure the both of them.
and you want execute the commande
php bin/console doctrine:schema:update --force
you got this Error
The table with name 'shop.classification__collection' already exists.
same for all other table of classification like category and tag
if you try to change the name of table in the entity of classification__collection to classification_collection
you will get a second error like
An exception occurred while executing 'CREATE UNIQUE INDEX tag_collection ON classification__collection (slug, context):
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
I run in this problem recently and find the solution.
The issue is address here
https://github.com/sonata-project/SonataClassificationBundle/issues/407
You just need to remove the entity created in App/Entity by classification or remapped the class to the App/Entity in sonata.yaml and sonata_classification.yaml
If you deleted the entities in App/Entity add the following cofiguration to your sonata.yaml and sonata_classification.yaml
/** sonata_classification.yaml**/
sonata_classification:
class:
tag: App\Application\Sonata\ClassificationBundle\Entity\Tag
category: App\Application\Sonata\ClassificationBundle\Entity\Category
media: App\Application\Sonata\MediaBundle\Entity\Media
collection: App\Application\Sonata\ClassificationBundle\Entity\Collection
context: App\Application\Sonata\ClassificationBundle\Entity\Context
/** sonata.yaml**/
class:
media: App\Application\Sonata\MediaBundle\Entity\Media
gallery: App\Application\Sonata\MediaBundle\Entity\Gallery
gallery_has_media: App\Application\Sonata\MediaBundle\Entity\GalleryHasMedia
category: App\Application\Sonata\ClassificationBundle\Entity\Category

Laravel sqlite migration - SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

I have setup in-momory testing in laravel 4 using sqlite. using setUp and tearDown methods, I get an integrity constraint violation error for this migration (guess its because of not being able to reset):
public function up()
{
Schema::create('test', function(Blueprint $table)
{
$table->integer('id', true);
$table->integer('other_id')->nullable()->index('other_id_idx');
$table->timestamps();
});
}
Error occurs coz of the ->index('other_id_idx').
The error:
[Illuminate\Database\QueryException]
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fa
ils

Symfony2 SyliusRbacBundle Setup

I am exploring the option of using SyliusRbacBundle for my app. I manage to install it along with FosUserBundle. When I got to the setup part I got the following error on running the command 'sylius:rbac:initialize' following the instructions found here: http://docs.sylius.org/en/latest/bundles/SyliusRbacBundle/setup.html#setup-roles-and-permissions-in-the-database
Initializing Sylius RBAC roles and permissions.
Adding permission "Update product". (app.product_update)
Adding permission "Create product". (app.product_create)
Adding permission "Manage product". (app.product_manage)
[Doctrine\DBAL\DBALException]
An exception occurred while executing 'INSERT INTO sylius_role (code, name,
description, security_roles, tree_left, tree_right, tree_level, created_at
, updated_at, parent_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' with params
["root", "Root", null, "a:0:{}", null, null, null, "2015-05-11 16:45:34",
"2015-05-11 16:45:34", null]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'tree_left' ca
nnot be null
[PDOException]
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'tree_left' ca
nnot be null
sylius:rbac:initialize
This is how my config.yml looks like:
sylius_rbac:
driver: doctrine/orm
security_roles:
ROLE_ADMINISTRATION_ACCESS: Can access backend
roles:
app.admin:
name: Administrator
description: Administrator
app.product_manager:
name: Product Manager
description: muchos productos
permissions: [app.product_update, app.product_create]
roles_hierarchy:
app.admin: [app.product_manager]
permissions:
app.product_update: Update product
app.product_create: Create product
app.product_manage: Manage product
permissions_hierarchy:
app.product_manage: [app.product_create, app.product_update]
Any one has a clue how to solve it?
Thanks!
The Sylius/Rbac uses the tree behaviour from the Atlantic18/DoctrineExtensions.
You can use this in a Symfony system using the stof/StofDoctrineExtensionsBundle.
Once it has been installed you would need to enable the tree behaviour in your app/config/config.yml like..
stof_doctrine_extensions:
orm:
default:
tree: true
This should make the listener register correctly and then your fields to be auto-populated.

How to generate entities from database view with doctrine and symfony2

I am trying to generate entities from database using standard console commands as described in Symfony2 documentation here: http://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html.
php app/console doctrine:mapping:convert --from-database --force yml "src/My/HomeBundle/Resources/config/doctrine/metadata/orm"
php app/console doctrine:mapping:import MyHomeBundle yml
php app/console doctrine:generate:entities MyHomeBundle
After this, all tables are generated correctly. The problem is that this won't generate entities for database views. When I add yml files myself into src/My/HomeBundle/Resources/config/doctrine/metadata/orm for example:
UserInGroup:
type: entity
table: user_in_group_view
fields:
id:
id: true
type: integer
unsigned: false
nullable: false
generator:
strategy: IDENTITY
userId:
type: integer
unsigned: false
nullable: false
column: user_id
userGroupId:
type: integer
unsigned: false
nullable: false
column: user_group_id
lifecycleCallbacks: { }
I get this exception when running php app/console doctrine:generate:entities MyHomeBundle:
Notice: Undefined index: My\HomeBundle\Entity\UserInGroup in C:\Users\ThisIsMe\Projects\SymfonyTestProject\vendor\doctrine\lib\Doctrine\ORM\Mapping\Driver\AbstractFileDriver.php line 121
Similar question was posted here: How to set up entity (doctrine) for database view in Symfony 2
I know I can create Entity class, but I was hoping that I could get this generated so if I change my view, I could just regenerate entity classes. Any suggestions?
Now you create your orm files only. You need to follow 2 more steps. I will give you the complete steps from begining.
Before doing this delete all yml files in your orm directory that you had created early.
I hope MyHomeBundle is your bundle name
1).php app/console doctrine:mapping:convert yml ./src/My/HomeBundle/Resources/config/doctrine --from-database --force
Symfony2 generate entity from Database
2).php app/console doctrine:mapping:import MyHomeBundle yml
3).php app/console doctrine:generate:entities MyHomeBundle
Hope this helps you.
Got the same issue, i use xml instead of yml but must be the same.
Check in your orm entity if the name include the correct route, exemple:
<entity name="Myapp\MyrBundle\Entity\MyEntity" table="myentity">
Because when i generate my orm from database the name was like that:
<entity name="MyEntity" table="myentity">
So doctrine didn't understand the right path.
Hope i'm clear and this will help you!
I know it's an old question but I found the trick (Symfony 6) to generate entities from SQL view (I use a SQL server database).
So I modified two methods of the SQLServerPlatform.php file of the doctrine bundle.
public function getListTablesSQL()
{
// "sysdiagrams" table must be ignored as it's internal SQL Server table for Database Diagrams
// Category 2 must be ignored as it is "MS SQL Server 'pseudo-system' object[s]" for replication
return 'SELECT name, SCHEMA_NAME (uid) AS schema_name FROM sysobjects'
. " WHERE type = 'V' AND name != 'sysdiagrams' AND category != 2 ORDER BY name";
}
I changed the where condition parameter 'U' by 'V' for view.
Same operation for the method getListTableColumnsSQL(), we change here the 'U' parameter of the where condition by 'V'.
public function getListTableColumnsSQL($table, $database = null)
{
return "SELECT col.name,
type.name AS type,
col.max_length AS length,
~col.is_nullable AS notnull,
def.definition AS [default],
col.scale,
col.precision,
col.is_identity AS autoincrement,
col.collation_name AS collation,
CAST(prop.value AS NVARCHAR(MAX)) AS comment -- CAST avoids driver error for sql_variant type
FROM sys.columns AS col
JOIN sys.types AS type
ON col.user_type_id = type.user_type_id
JOIN sys.objects AS obj
ON col.object_id = obj.object_id
JOIN sys.schemas AS scm
ON obj.schema_id = scm.schema_id
LEFT JOIN sys.default_constraints def
ON col.default_object_id = def.object_id
AND col.object_id = def.parent_object_id
LEFT JOIN sys.extended_properties AS prop
ON obj.object_id = prop.major_id
AND col.column_id = prop.minor_id
AND prop.name = 'MS_Description'
WHERE obj.type = 'V'
AND " . $this->getTableWhereClause($table, 'scm.name', 'obj.name');
}
Maybe this will help someone.
As you can see here:
http://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html
the reverse engineering process from db to entity is not fully implemented yet:
"As the Doctrine tools documentation says, reverse engineering is a one-time process to get started on a project. Doctrine is able to convert approximately 70-80% of the necessary mapping information based on fields, indexes and foreign key constraints. Doctrine can't discover inverse associations, inheritance types, entities with foreign keys as primary keys or semantical operations on associations such as cascade or lifecycle events. Some additional work on the generated entities will be necessary afterwards to design each to fit your domain model specificities."

Resources