StofDoctrineExtensionsBundle checking the uniqueness of slug by the two fields - php

I use Knp\DoctrineBehaviors for Translation and StofDoctrineExtensionsBundle for Sluggable. How to make checking the uniqueness of slug by the sluggable and locale? I want to get the slug look like this:
for EN: /contacts
for PT: /pt/contacts
for PT (if duplicate): /pt/contacts-1
for ES: /es/contacts
But now, i have this database filter_node_translation
Entity\FilterNode.php:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
/**
* #ORM\Table(name="filter_node")
* #ORM\Entity()
*/
class FilterNode
{
use ORMBehaviors\Translatable\Translatable;
/**
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\Column(name="id", type="integer")
*/
protected $id;
/**
* #return mixed
*/
public function getId()
{
return $this->id;
}
}
FilterNodeTranslation:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* #ORM\Table(name="filter_node_translation")
* #ORM\Entity()
*/
class FilterNodeTranslation
{
use ORMBehaviors\Translatable\Translation;
/**
* #var string
*
* #ORM\Column(type="string", nullable=true)
*/
protected $sluggable;
/**
* #var string
*
* #Gedmo\Slug(fields={"sluggable"})
* #ORM\Column(type="string", nullable=true)
*/
protected $slug;
/**
* #return string
*/
public function getSluggable()
{
return $this->sluggable;
}
/**
* #param string $sluggable
*/
public function setSluggable($sluggable)
{
$this->sluggable = $sluggable;
}
}

I found solution. Gedmo sluggable have other configuration option "unique_base".
It looks like this:
class FilterNodeTranslation
{
/**
* #var string
*
* #Gedmo\Slug(updatable=true, unique=true, unique_base="locale", fields={"sluggable"})
* #ORM\Column(type="string")
*/
protected $slug;
}

Related

Symfony / php : avoid query to give nested json objects from one to many relation

I have entities; Command, User, GiftCheck, and GiftCheckType :
GiftCheck
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="App\Repository\GiftCheckRepository")
*/
class GiftCheck
{
/**
* #ORM\Id()
* #ORM\Column(type="integer", length=255)
*#ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="string", length=255, columnDefinition="ENUM('Cadeau', 'Anniversaire', ' NĂ´el', 'Saint Valentain ')")
*/
private $theme;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="giftChecks")
*/
private $User;
/**
* #ORM\Column(type="string", length=255)
*/
private $uniqueSerialId;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Command", inversedBy="giftChecks")
* #ORM\JoinColumn(nullable=false)
*/
private $command;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\GiftCheckType", inversedBy="gift")
*/
private $giftCheckType;
User
<?php
namespace App\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\UserInterface;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* #ORM\Entity
* #ORM\Table(name="fos_user")
* #UniqueEntity("email")
* #UniqueEntity("username")
* #UniqueEntity("tel")
*/
class User extends BaseUser
{
/**
* #ORM\Id
*#ORM\GeneratedValue(strategy="AUTO")
* #ORM\Column(type="integer")
*/
protected $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $firstname;
/**
* #ORM\OneToMany(targetEntity="App\Entity\Command", mappedBy="user")
*/
private $commands;
/**
* #ORM\OneToMany(targetEntity="App\Entity\GiftCheck", mappedBy="User")
*/
private $giftChecks;
Command
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="App\Repository\CommandRepository")
*/
class Command
{
/**
* #ORM\Id()
* #ORM\Column(type="string", length=255)
*/
private $id;
/**
* #ORM\Column(type="float")
*/
private $price;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="commands")
* #ORM\JoinColumn(nullable=true, onDelete="CASCADE")
*/
private $user;
/**
* #ORM\OneToMany(targetEntity="App\Entity\CommandLine", mappedBy="command")
*/
private $commandLines;
/**
* #ORM\Column(type="string", length=255)
*/
private $status;
/**
* #ORM\OneToMany(targetEntity="App\Entity\GiftCheck", mappedBy="command")
*/
private $giftChecks;
GiftCheckType
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="App\Repository\GiftCheckTypeRepository")
*/
class GiftCheckType
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $designation;
/**
* #ORM\OneToMany(targetEntity="App\Entity\GiftCheck", mappedBy="giftCheckType")
*/
private $gift;
/**
* #ORM\Column(type="string", length=255)
*/
private $type;
I found my self faced to a problem, When I try to fetch my command data I got a nested json object ( like children / parent / children structure of the same object ) which contain giftchecks array, every giftcheck contain user object which have the same giftchecks list, the two images bellow describe the status of this nested JSON in the browser console log when a fetch the result with angular :
is that problem about missing serialization on my entities or coming from other side ? are my entities relations correctly coded ?
please change User entity as
/**
* #ORM\OneToMany(targetEntity="App\Entity\Command", mappedBy="user", fetch="LAZY")
*/
private $commands;
/**
* #ORM\OneToMany(targetEntity="App\Entity\GiftCheck", mappedBy="User", fetch="LAZY")
*/
private $giftChecks;
you can change other entities too if you wish
https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/reference/annotations-reference.html#onetomany
also see EXTRA_LAZY for performance
https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/tutorials/extra-lazy-associations.html#extra-lazy-associations

Symfony Doctrine ManyToOne association migration No reaction

I have two tables one is entry.php and user.php
I Want to associate $balance of user with the $balance of entry
but when I try php bin/console doctrine:migrations:diff
I get
No changes detected in your mapping information.
this is my user.php
<?php
namespace BankBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Table(name="user")
*
* #ORM\Entity(repositoryClass="BankBundle\Entity\user")
**/
class user
{
/**
* #ORM\Id
* #ORM\Column(name="id",type="integer")
* #ORM\GeneratedValue
*/
private $id;
/**
* #ORM\OneToMany(targetEntity="entry", mappedBy="balance")
* #var entry[] An ArrayCollection of entry objects.
*/
private $balance;
public function getId()
{
return $this->id;
}
public function getBalance()
{
return $this->balance;
}
}
and my entry.php
<?php
namespace BankBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Table(name="entry")
*
* #ORM\Entity(repositoryClass="BankBundle\Repository\BankRepository")
**/
class entry
{
/**
* #ORM\Id
* #ORM\Column(name="id",type="integer")
* #ORM\GeneratedValue
*/
private $id;
/**
* #ORM\Column(name="amount",type="integer")
*/
private $amount;
/**
* #ORM\Column(name="balance",type="integer")
* #ORM\ManyToOne(targetEntity="user", inversedBy="balance")
*/
private $balance;
/**
* #ORM\Column(name="created_at",type="datetime")
*/
private $created_at;
}

Different behaviour with slightly similar entity

I made a web application with Symfony2. There are 2 kind of user, a UserOperator and a UserGroundStation. Both of them extend User.
USER GROUND STATION
namespace Acme\ManagementBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use PUGX\MultiUserBundle\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity
* #ORM\HasLifecycleCallbacks()
* #ORM\Table(name="user_GroundStation")
* #UniqueEntity(fields = "username", targetClass = "Acme\ManagementBundle\Entity\User",
message="Username already_used")
* #UniqueEntity(fields = "email", targetClass = "Acme\ManagementBundle\Entity\User",
message="Email already_used")
*/
class UserGroundStation extends User
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="string", length=60)
* #var String
*/
protected $name;
/**
* #var float
*
* #ORM\Column(name="latitude", type="float")
*/
private $latitude;
/**
* #var float
*
* #ORM\Column(name="longitude", type="float")
*/
private $longitude;
/**
* Set latitude
*
* #param string $latitude
* #return UserGroundStation
*/
public function setLatitude($latitude)
{
$this->latitude = $latitude;
return $this;
}
/**
* Get latitude
*
* #return string
*/
public function getLatitude()
{
return $this->latitude;
}
/**
* Set longitude
*
* #param float $longitude
* #return UserGroundStation
*/
public function setLongitude($longitude)
{
$this->longitude = $longitude;
return $this;
}
/**
* Get longitude
*
* #return float
*/
public function getLongitude()
{
return $this->longitude;
}
/**
* #ORM\PrePersist
*/
public function setCreatedAtValue()
{
$this->addRole('ROLE_GS');
}
}
USER OPERATOR
namespace Acme\ManagementBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use PUGX\MultiUserBundle\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity
* #ORM\HasLifecycleCallbacks()
* #ORM\Table(name="user_Operator")
* #UniqueEntity(fields = "username", targetClass = "Acme\ManagementBundle\Entity\User",
message="Username already_used")
* #UniqueEntity(fields = "email", targetClass = "Acme\ManagementBundle\Entity\User",
message="Email already_used")
*/
class UserOperator extends User
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="string", length=60)
* #var String
*/
protected $name;
/**
* #ORM\PrePersist
*/
public function setCreatedAtValue()
{
$this->addRole('ROLE_OPERATOR');
}
}
They extend User.
When I call the registration page for UserOperator, it works, when I call registration page for UserGroundStation, Symfony alerts me:
Neither the property "mission" nor one of the methods "getMission()", "isMission()", "hasMission()", "__get()" exist and have public access in class "Acme\ManagementBundle\Entity\UserGroundStation".
How is it possible? Where can I find the error?
USER
namespace Acme\ManagementBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
/**
* #ORM\Entity
* #ORM\Table(name="user")
* #ORM\InheritanceType("JOINED")
* #ORM\DiscriminatorColumn(name="type", type="string")
* #ORM\DiscriminatorMap({"user_one" = "UserGroundStation", "user_two" = "UserOperator"})
* #ORM\MappedSuperclass()
*
*/
abstract class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\ManyToMany(targetEntity="Acme\ManagementBundle\Entity\Mission", mappedBy="users")
*/
protected $missions;
public function __construct(){
$this -> missions = new ArrayCollection();
}
public function setMissions(Collection $missions){
$this -> missions = $missions;
return $this;
}
public function addMission($mission){
$this -> missions -> add($mission);
return $this;
}
public function getMissions(){
return $this -> missions;
}
It was really a dummy problem.
They have different FormType:
RegistrationUserOperatorFormType is different to RegistrationUserGroundStationFormType and
ProfileUserGroundStationFormType is different to ProfileUserOperatorFormType.

Symfony forms for Translatable Doctrine entities

I have a Doctrine entity that has been translated using the Translatable Doctrine extension:
<?php
namespace Myapp\ProductBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\Translatable\Translatable;
/**
* #ORM\Table(name="product_property")
* #ORM\Entity()
* #Gedmo\TranslationEntity()
*/
class Property implements Translatable
{
/**
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #Gedmo\Translatable
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* #ORM\OneToMany(
* targetEntity="PropertyTranslation",
* mappedBy="object",
* cascade={"persist", "remove"}
* )
*/
private $translations;
/**
* #Gedmo\Locale
* Used locale to override Translation listener`s locale
* this is not a mapped field of entity metadata, just a simple property
*/
private $locale;
public function setTranslatableLocale($locale)
{
$this->locale = $locale;
}
public function getTranslations()
{
return $this->translations;
}
public function addTranslation(PropertyTranslation $t)
{
if (!$this->translations->contains($t)) {
$this->translations[] = $t;
$t->setObject($this);
}
}
}
Now, I'd like to render a form with an input field for the "name" property in each of the languages available in my translation.
How is that best done?
It seems that the following bundle is able to do exactly what I was looking for: https://github.com/a2lix/TranslationFormBundle

Class Doctrine\Common\Collections\ArrayCollection is not a valid entity or mapped super class

I have Three entities:
FeatureValue.php
<?php
namespace Webmuch\ProductBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
*/
class FeatureValue
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="string", length="100")
*/
protected $name;
/**
* #ORM\OneToMany(targetEntity="FeatureType", mappedBy="name")
*/
private $featuretype;
public function __construct()
{
$this->featuretype = new \Doctrine\Common\Collections\ArrayCollection();
}
FeatureType.php
<?php
namespace Webmuch\ProductBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Webmuch\ProductBundle\Entity\FeatureValue;
/**
* #ORM\Entity
*/
class FeatureType
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="string", length=255, nullable=true)
*/
protected $title;
/**
* #ORM\ManyToOne(targetEntity="FeatureValue", inversedBy="featuretype",cascade={"persist"})
* #ORM\JoinColumn(name="feature_value_id", referencedColumnName="id")
*/
protected $name;
public function __construct()
{
$this->name = new \Doctrine\Common\Collections\ArrayCollection();
}
Product.php
<?php
namespace Webmuch\ProductBundle\Entity;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection as ArrayCollection;
/**
* #ORM\Entity
* #ORM\Table()
* #ORM\HasLifecycleCallbacks
*/
class Product
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\ManyToMany(targetEntity="Store")
*/
protected $store;
/**
* #ORM\ManyToMany(targetEntity="Webmuch\CategoryBundle\Entity\Category")
*/
protected $category;
/**
* #Gedmo\Sluggable
* #ORM\Column(type="string", length=255)
*/
protected $title;
/**
* #Gedmo\Slug(updatable=false, unique=true)
* #ORM\Column(type="string", length=255)
*/
protected $slug;
/**
* #ORM\Column(type="integer")
*/
protected $quantity;
/**
* #ORM\Column(type="boolean")
*/
protected $active;
/**
* #ORM\Column(type="text", length="4000", nullable="true")
*/
protected $preview;
/**
* #ORM\ManyToMany(targetEntity="FeatureType")
* #ORM\JoinColumn(name="feature_type_id", referencedColumnName="id")
*/
protected $features;
public function __toString()
{
return $this->getTitle();
}
}
My problem is that when i add FeatureValue in FeatureType then show me error Class "Doctrine\Common\Collections\ArrayCollection is not a valid entity or mapped super class."
In my project i want FeatureType along with FeatureValue in my Product entity.
suppose in FeatureType two property Size and Colour.Now in FeatureType Size- Three FeatueValue Small,Medium,Large have been given and also suppose in FeatureType Colour- Three FeatureValue Red,Green,Yello have been given.
Now i want to show both the FeatureType along with their FeatureValue in my Product Entity.
So any one tell me how it can be done.I have tried a lot of but not succeed.
In Webmuch\ProductBundle\Entity\FeatureType::__construct you assign an ArrayCollection to $this->name, this is wrong, as this is a ToOne and not a ToMany.
Just remove this line.

Categories