Doctrine can't generate relations - doctrine

Note: I did have done some searches before I ask.
Here is my problem:
I have two entities: One is Channel, and the other one is Plugin, a channel can have many plugins, below is the code,
Channel:
use Gedmo\Mapping\Annotation as Gedmo; // gedmo annotations
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Mikay\MikiBundle\Entity\Channel
*
* #ORM\Table(name="channels")
* #ORM\Entity(repositoryClass="Mikay\MikiBundle\Entity\ChannelRepository")
*/
<?php
class Channel
{
/**
* #ORM\OneToMany(targetEntity="Plugin", mappedBy="channel")
*/
private $plugins;
Plugin:
use Doctrine\ORM\Mapping as ORM;
/**
* Mikay\MikiBundle\Entity\Plugin
*
* #ORM\Table(name="plugins")
* #ORM\Entity(repositoryClass="Mikay\MikiBundle\Entity\PluginRepository")
*/
<?php
class Plugin
{
/**
* #ORM\ManyToOne(targetEntity="Channel", inversedBy="plugins")
* #ORM\JoinColumn(name="channel_id", referencedColumnName="id")
*/
private $channel;
I used this command to update the database, but it never created the relation between the two tables.
php app/console doctrine:schema:update --force
So, what's the problem, could it be some config errors?
EDIT:
Note:
Don't add
#ORM\Column(name="channel_id", type="integer")
this kind annonation in Plugin entity, or it will not generate the relations between two tables, that's the cause of my problem.

You need to tell doctrine that these are entities.
/**
* #ORM\Entity()
*/
class Channel
{
Here is a quick way to verify that doctrine can see your entities:
php app/console doctrine:mapping:info

I'll repeat it here again. Don't add
#ORM\Column(name="channel_id", type="integer")
this type of annonation on the property that references other entity, in my case, is the channel property of the Plugin entity, or it will not generate the relations between two tables, that's the cause of my problem.

Related

Symfony validation accept wrong mime-type application/postscript

I'm currently working on a basic form on symfony 2.8.
The user can upload a File through a classic filetype.
I'm using symfony validation assert to validate the mime-type of the object sent by the user : it should be only pdf or doc files. It's working great, it does not validate images for exemple.
BUT it's still possible to upload adobe illustrator file. The mime-type application/postscript is accepted by the validation process. I can't figure why. Do you have an idea?
My entity class :
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity
* #ORM\Table(name="app_candidature_file")
*/
class CandidatureFile extends AbstractFile
{
/**
* #var UploadedFile
*
* #Assert\File(
* maxSize="3500000",
* mimeTypes = {"application/pdf", "application/x-pdf", "application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
* mimeTypesMessage = "app.candidature_file.file.mime_types_message"
* )
*/
protected $file;
}
I should specify that I'm using cascade validation in the form.
Thank you in advance if you have an idea.

Many-to-Many Relations using non-"id" Primary Key in Doctrine

So I'd like to create two entities and make a many-to-many reference. I would love to make this association using a string primary key on one table. This seems to be really hard, at least it took me pretty much time trying without any results yet.
This is my approach:
First entity:
namespace Project\AdminBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="User")
*/
class User
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $Id;
/**
* #ORM\ManyToMany(targetEntity="Role", inversedBy="Users")
* #ORM\JoinTable(name="role_user",
* joinColumns={#ORM\JoinColumn(name="User_Id", referencedColumnName="Id")},
* inverseJoinColumns={#ORM\JoinColumn(name="Role_Name", referencedColumnName="Name")}
* )
*/
private $Roles;
}
And the second:
namespace Project\AdminBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="Role")
*/
class Role
{
/**
* #ORM\Id
* #ORM\Column(type="string", length=256)
*/
private $Name;
/**
* #ORM\ManyToMany(targetEntity="User", mappedBy="Roles")
* #ORM\JoinTable(name="role_user",
* joinColumns={#ORM\JoinColumn(name="Role_Name", referencedColumnName="Name")},
* inverseJoinColumns={#ORM\JoinColumn(name="User_Id", referencedColumnName="Id")}
* )
*/
private $Users;
}
Output of ./app/console doctrine:schema:validate:
[Mapping] FAIL - The entity-class 'Project\AdminBundle\Entity\User' mapping is invalid:
* The referenced column name 'Id' has to be a primary key column on the target entity class 'Project\AdminBundle\Entity\User'.
* The referenced column name 'Id' has to be a primary key column on the target entity class 'Project\AdminBundle\Entity\Role'.
What do I miss?
Attention upper/lowercase! Doctrine generates its columns as lowercase per default. This solves the issue:
* joinColumns={#ORM\JoinColumn(name="User_Id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="Role_Name", referencedColumnName="name")}
This it over-complicated; it's enough to put this into the Role entity:
/**
* #ORM\ManyToMany(targetEntity="User", mappedBy="Roles")
* #ORM\JoinColumn(name="role_user", referencedColumnName="name")
*/
private $Users;
I only thought about this when writing the question.. what a good method to think about the problem again from scratch.
Cheers

Symfony2 - UniqueEntity no action

I have an entity named Test with two fields: id and name.
I would like to have the name as unique.
What I did:
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
...
/**
* Company\AppBundle\Entity\Test
*
* #ORM\Table(name="test")
* #UniqueEntity("name")
* #ORM\Entity(repositoryClass="Company\AppBundle\Entity\TestRepository")
*
*/
class Test
{
....
/**
* #var string$name
*
* #ORM\Column(name="name", type="string", length=200, nullable=false, unique=true)
*/
private $name;
....
In my controller, I am using:
if ($form->isValid()) {
....
But the validation goes through. Am I missing something?
The unique annotation is for doctrine, it passes it to the database level and the error gets thrown from there. It will not know that the entity exists until you try to save it. To do the checks in php you have to query for the unique attribute yourself and check if it exists...

No relations updated in the DB after writing symfony/doctrine associations

So I am working on a project in symfony 3.0.1. I have generated entities (via doctrine:generate:entity with the following interactive setup questions) and then did the doctrine:schema:update --force to generate the tables in the DB from the Doctrine entities.
I then went in the entities and created associations.
After that I went ahead and did another doctrine:schema:update -- force and after checking in phpMyAdmin no relations were created by the schema update I forced.
Is there something else I am missing? See below my entities:
namespace MyAppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Logins
*
* #ORM\Table(name="logins")
* #ORM\Entity(repositoryClass="MyAppBundle\Repository\LoginsRepository")
*/
class Logins
{
/**
* #var int
*
* #ORM\Column(name="RoleID", type="integer")
* #ORM\ManyToOne(targetEntity="Roles", inversedBy="roleID")
* #ORM\JoinColumn(name="roleID", referencedColumnName="roleID")
*/
private $roleID;
'
'namespace MyAppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Roles
*
* #ORM\Table(name="roles")
* #ORM\Entity(repositoryClass="MyAppBundle\Repository\RolesRepository")
*/
class Roles
{
/**
* #var int
*
* #ORM\Column(name="roleID", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\OneToMany(targetEntity="Logins", mappedBy="roleID")
*/
private $roleID;
public function __construct() {
$this->roleID=new ArrayCollection();
Still not sure why there were conflicts, but I resolved my problem with Doctrine reverse engineering. I created the relations in PMA and then generated mapping from it. All worked out well.
EDIT: The guy in comments to my post was right. I cannot assign One-to-Many on an Identity property.
I have had frustrating problems with relations not updating on my production environment several times now. My code would execute as expected with no errors, but the table(s) simply wouldn't update.
I've discovered that if you are using APC and you have just added a new relationship, you need to restart the web server in order for old versions of the entity classes to be flushed out.

Symfony2 validation exception with unique constraint

I have an entity "Movie" which has a unique constraint through doctrine annotation. Based on the movie entity I have auto generated a CRUD layer. When I now try to add a new movie I get the following exception:
Only field names mapped by Doctrine can be validated for uniqueness.
When the constraint is removed everything works fine. Do somebody has an idea where the problem lays and how I can resolve it?
My guessing is the entity, because it is new, is not sync with the EntityManager and therefore could not check the constraint. Am I'close?
I'm using Symfony 2.0.1 with Doctrine 2.1.1, MySQL as Database.
Thanks,
-lony
The "Movie" Entity:
/**
* #ORM\Table()
* #ORM\Entity
* #ORM\InheritanceType("JOINED")
* #ORM\DiscriminatorColumn(name="type", type="string")
* #ORM\DiscriminatorMap({"movie" = "Movie", "series" = "Series"})
*
* #DoctrineAssert\UniqueEntity("title_orginal")
*/
class Movie {
/**
* #var integer $id
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string $titleOrginal
*
* #ORM\Column(name="title_orginal", type="string", length=255, unique="true")
*/
private $titleOrginal;
..
Your syntax is wrong.
Use this:
#DoctrineAssert\UniqueEntity(fields={"title_orginal"})
instead of
#DoctrineAssert\UniqueEntity("title_orginal")
You can then customize the violation message like this :
#DoctrineAssert\UniqueEntity(fields={"title_orginal", message="my.custom.message"})
and translate this message by using a validators.xliff file (it must be named like that).
I tell you this because I struggled the other day with it and was obliged to debug to find about this validators.xliff naming convention.
I think there is a small typo:
#DoctrineAssert\UniqueEntity(fields={"title_orginal", message="my.custom.message"})
should be:
#DoctrineAssert\UniqueEntity(fields={"title_orginal"}, message="my.custom.message")
and for several fields
#DoctrineAssert\UniqueEntity(fields={"title_orginal", "field2"}, message="my.custom.message")

Resources