I've created an Entity to store some data i'm pulling via an api
When i try to findByOne to see if the entry already exists, i get an error saying the field is not recognised.
I've done a php app/console doctrine:schema:update --force
I can see the table in my mySQL manager with the right fieldname 'StadiumID'
So why cant doctrine find it when i do findByOne();
My Entity Class:
<?php
namespace FantasyPro\DataBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Stadium
*
* #ORM\Table("fp_stadium")
* #ORM\Entity(repositoryClass="FantasyPro\DataBundle\Entity\StadiumRepository")
*/
class Stadium
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var integer
*
* #ORM\Column(name="StadiumID", type="integer", length=2, nullable=false, unique=true)
*/
private $stadiumID;
/**
* #var string
*
* #ORM\Column(name="Name", type="string", length=100, nullable=false)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="City", type="string", length=50, nullable=false)
*/
private $city;
/**
* #var string
*
* #ORM\Column(name="State", type="string", length=10, nullable=true)
*/
private $state;
/**
* #var string
*
* #ORM\Column(name="Country", type="string", length=2, nullable=false)
*/
private $country;
/**
* #var integer
*
* #ORM\Column(name="Capacity", type="integer", length=32, nullable=true)
*/
private $capacity;
/**
* #var string
*
* #ORM\Column(name="PlayingSurface", type="string", length=50, nullable=true)
*/
private $playingSurface;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set stadiumID
*
* #param integer $stadiumID
* #return Stadium
*/
public function setStadiumID($stadiumID)
{
$this->stadiumID = $stadiumID;
return $this;
}
/**
* Get stadiumID
*
* #return integer
*/
public function getStadiumID()
{
return $this->stadiumID;
}
/**
* Set name
*
* #param string $name
* #return Stadium
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set city
*
* #param string $city
* #return Stadium
*/
public function setCity($city)
{
$this->city = $city;
return $this;
}
/**
* Get city
*
* #return string
*/
public function getCity()
{
return $this->city;
}
/**
* Set state
*
* #param string $state
* #return Stadium
*/
public function setState($state)
{
$this->state = $state;
return $this;
}
/**
* Get state
*
* #return string
*/
public function getState()
{
return $this->state;
}
/**
* Set country
*
* #param string $country
* #return Stadium
*/
public function setCountry($country)
{
$this->country = $country;
return $this;
}
/**
* Get country
*
* #return string
*/
public function getCountry()
{
return $this->country;
}
/**
* Set capacity
*
* #param integer $capacity
* #return Stadium
*/
public function setCapacity($capacity)
{
$this->capacity = $capacity;
return $this;
}
/**
* Get capacity
*
* #return integer
*/
public function getCapacity()
{
return $this->capacity;
}
/**
* Set playingSurface
*
* #param string $playingSurface
* #return Stadium
*/
public function setPlayingSurface($playingSurface)
{
$this->playingSurface = $playingSurface;
return $this;
}
/**
* Get playingSurface
*
* #return string
*/
public function getPlayingSurface()
{
return $this->playingSurface;
}
}
The code i'm using to add the data if it does not exist and to update it if it does exist:
<?php
namespace FantasyPro\DataBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use FantasyPro\DataBundle\Entity\Stadium;
class DefaultController extends Controller
{
public function indexAction($name)
{
return $this->render('DataBundle:Default:index.html.twig', array('name' => $name));
}
public function updateStadiumAction(){
//get list of stadiums
$client = $this->container->get('fantasyapi');
$stadiumData = $client->Stadiums();
//var_dump($stadiumData);
//get the entity manager
$em = $this->getDoctrine()->getManager();
$repo = $em->getRepository('DataBundle:Stadium');
$log = $stadiumData;
foreach($stadiumData as $stadium){
// Get the current stadium in the list
$criteria = array("StadiumID" =>$stadium['StadiumID']);
var_dump($criteria);
$storedStadium = $repo->FindOneBy($criteria);
if (!$storedStadium) {
/* throw $this->createNotFoundException(
'No product found for id '.$stadium['StadiumID']
);*/
//no stadium exists with the StadiumID passed
//create a new entry
$entry = new Stadium();
$entry->setStadiumID($stadium['StadiumID']);
$entry->setName($stadium['Name']);
$entry->setCity($stadium['City']);
$entry->setState($stadium['State']);
$entry->setCountry($stadium['Country']);
$entry->setCapacity($stadium['Capacity']);
$entry->setPlayingSurface($stadium['PlayingSurface']);
$em->persist($entry);
$log .= 'Added New Stadium: '.$stadium['StadiumID'].' : '.$stadium['Name'].'<br>';
}else{
$storedStadium->setStadiumID($stadium['StadiumID']);
$storedStadium->setName($stadium['Name']);
$storedStadium->setCity($stadium['City']);
$storedStadium->setState($stadium['State']);
$storedStadium->setCountry($stadium['Country']);
$storedStadium->setCapacity($stadium['Capacity']);
$storedStadium->setPlayingSurface($stadium['PlayingSurface']);
$em->persist($storedStadium);
$log .= 'Updated Stadium: '.$stadium['StadiumID'].' : '.$stadium['Name'].'<br>';
}
}
//$em->flush();
return $this->render('DataBundle:Default:index.html.twig', array('log' => $log));
}
}
The property name is actually:
private $stadiumID;
So you should do :
$repo->findOneBy(array('stadiumID' => $value));
so change this row:
$criteria = array("StadiumID" =>$stadium['StadiumID']);
to this:
$criteria = array("stadiumID" =>$stadium['StadiumID']);
Doctrine's main role is to connect your application layer(entites) to the database layer(tables). So doctrine tries to translate request comming from the application layer to the database. Even if you named your field in the database "StadiumID"or "table_name_snake_case", when you call findOneBy(array($property => $value)), doctrine expects $property to refer to the property name, and it will translate this to SQL to something like 'SELECT FROM .... where StadiumID = :value'
For others who still have the problem,
make sur that you have puted the sign '=>' instead of ',' when calling the function findOneBy(...)
So instead of :
findOneBy(["code" , "Code-Example"]);
do that :
findOneBy(["code" => "Code-Example"]);
Also, make sure the doctrine naming strategy is specified:
doctrine:
orm:
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
this is the doctrine's default naming strategy. This way doctrine knows how to map Entity property names to column names.
Note that, naming strategy configuration might be in different levels of doctrine configuration. For example, in another repo it is configured like this:
doctrine:
orm:
default_entity_manager: shopping
entity_managers:
shopping:
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
Related
I have two entities in my code: Session and SessionPicture.
There is a OneToOne relationship between Session and SessionPicture. SessionPicture is the mapping entity and Session is the inverse one.
Session has an attribute called "Slug", which is generated thanks to the StofDoctrineExtensionsBundle https://github.com/stof/StofDoctrineExtensionsBundle, and using the Gedmo\Sluggable annotation.
I have a form to create a Session (so a SessionType), and this SessionType embeds a SessionPictureType. The SessionPicture class contains a file attribute (UploadedFile class) and I have used LifecycleEvents (PostPersist/PostUpdate) to upload the file of the SessionPicture, and give it the "slug" property of the related Session when I save it.
My issue is that when I try to change another property of my SessionPicture (let's say "alt") and give it the value of the "slug" of the session , I cannot do it in the PostPersist Lifecycle and then save it to my database, because this event takes place after the onFlush, which is too late. I have to do it in the PrePersist Lifecyle. But then the "slug" of my session is not available yet, because it is generated during the onFlush by the extension.
So how can I get the slug of the related session in the prePersist lifecyle?
(PS: I have a found a solution, which is to persist the session in my controller, flush with the Manager, and then to persist again and redo a flush() but it is not very elegant).
Please find my code below:
/**
* Session
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="MyBundle\Entity\SessionRepository")
* #ORM\HasLifecycleCallbacks()
*/
class Session
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, nullable=false)
*/
private $name;
/**
* #ORM\OneToOne(targetEntity="MyBundle\Entity\SessionPicture", cascade={"persist", "remove"}, mappedBy="session")
* #ORM\JoinColumn(name="session_picture_id", referencedColumnName="id", nullable=true)
*/
private $sessionPicture;
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set sessionPicture
*
* #param \MyBundle\Entity\SessionPicture $sessionPicture
*
* #return Session
*/
public function setSessionPicture(MyBundle\Entity\SessionPicture $sessionPicture = null)
{
$this->sessionPicture = $sessionPicture;
return $this;
}
/**
* Get sessionPicture
*
* #return \MyBundle\Session\SessionPicture
*/
public function getSessionPicture()
{
return $this->sessionPicture;
}
And my SessionPicture code:
/**
* #var string
*
* #ORM\Column(name="alt", type="string", length=255, nullable=true)
*/
private $alt;
/**
* #var string
*
* #ORM\Column(name="path", type="string", length=255, nullable=true)
*/
private $path;
/**
* #ORM\OneToOne(targetEntity="MyBundle\Entity\Session", inversedBy="sessionPicture")
*/
private $session;
public $file;
private $fileNameForRemove;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set extension
*
* #param string $extension
*
* #return SessionPicture
*/
public function setExtension($extension)
{
$this->extension = $extension;
return $this;
}
/**
* Get extension
*
* #return string
*/
public function getExtension()
{
return $this->extension;
}
/**
* Set alt
*
* #param string $alt
*
* #return SessionPicture
*/
public function setAlt($alt)
{
$this->alt = $alt;
return $this;
}
/**
* Get alt
*
* #return string
*/
public function getAlt()
{
return $this->alt;
}
/**
* Set path
*
* #param string $path
*
* #return SessionPicture
*/
public function setPath($path)
{
$this->path = $path;
return $this;
}
/**
* Get path
*
* #return string
*/
public function getPath()
{
return $this->path;
}
/**
* Set session
*
* #param \MyBundle\Entity\Session $session
*
* #return SessionPicture
*/
public function setSession(\NyBundle\Entity\Session $session = null)
{
$this->session = $session;
return $this;
}
/**
* Get session
*
* #return \MyBundle\Entity\Session
*/
public function getSession()
{
return $this->session;
}
/**
* #ORM\PrePersist()
* #ORM\PreUpdate()
*/
public function preUpload()
{
if($this->file === null)
return ;
$extension = $this->file->guessExtension();
$this->extension = (string)$extension;
My issue is below, I cannot get the slug before the flush of the session
$this->alt = $this->getSession()->getSlug();
}
/**
* #ORM\PostPersist()
* #ORM\PostUpdate()
*/
public function upload()
{
But here, I can get the slug because my session has already been flushed and the slug generated
$this->alt = $this->getSession()->getSlug();
$this->path = $this->alt.'.'.$this->extension;
if ($this->file === null)
return ;
$this->file->move($this->getUploadRootDir(), $this->path);
unset($this->file);
}
Finally, here is an extract my controller code:
$form->handlerequest($request);
$validator = $this->get('validator');
$errorList = $validator->validate($session);
if (count($errorList) == 0)
{
$em = $this->getDoctrine()->getManager();
$em->persist($session);
$em->flush();
$em->persist($session);
$em->flush();
As you can see, I had to persist the $session and to flush it once, so that the slug is generated at the flush. Then persist again so that the slug is now available for the "alt" property of the SessionPicture at the prePersist n2, and flush again to save the alt property. But it is not very clean.
Thanks for any hel or advice. Thanks.
I am new to Symfony2 and Doctrine, i am stuck a little bit. Error receiving while saving to the database.
An exception occurred while executing 'INSERT INTO tho_provider (provid_name, provid_logo, provid_logo_path, created_time, provider_created_by) VALUES (?, ?, ?, ?, ?)' with params ["test", {}, "qwwwww", "2015-08-11 16:03:15", null]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'provider_created_by' cannot be null
I have the form where i am only getting provider name input and logo, rest is i am handling in the controller, Including provider_created_by which gets null just after the flush, before the flush command its showing the value.
public function createAction(Request $request)
{
$user = $this->getUser();
$entity = new Provider();
$entity->setProvidLogoPath("qwwwww");
$entity->setProviderCreatedBy($user);
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
if ($form->isValid()) {
//$entity->setCreatedBy($user);
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('provider_show', array('id' => $entity->getId())));
}
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
I have entities, PROVIDER and USER.
User's can create many provider and providers only belongs to one user. One to many relationship.
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* Provider
*
* #ORM\Table(name="tho_provider")
* #ORM\Entity(repositoryClass="AppBundle\Entity\ProviderRepository")
* #Vich\Uploadable
* #ORM\HasLifecycleCallbacks()
*/
class Provider
{
/**
* #ORM\ManyToOne(targetEntity="User", inversedBy="providers")
* #ORM\JoinColumn(name="provider_created_by", referencedColumnName="id")
*/
protected $users;
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
* #Assert\NotBlank()
* #ORM\Column(name="provid_name", type="string", length=255)
*/
private $providName;
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* #Vich\UploadableField(mapping="general_image", fileNameProperty="provid_logo")
*
* #var File
*/
private $imageFile;
/**
* #var string
*
* #ORM\Column(name="provid_logo", type="string", length=255)
*/
private $providLogo;
/**
* #var string
*
* #ORM\Column(name="provid_logo_path", type="string", length=255)
*/
private $providLogoPath;
/**
* #var \DateTime
* #Assert\Type("\DateTime")
* #ORM\Column(name="created_time", type="datetime")
*/
private $createdTime;
/**
* #var integer
*
* #ORM\Column(name="provider_created_by", type="integer")
*/
public $providerCreatedBy;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set providName
*
* #param string $providName
* #return Provider
*/
public function setProvidName($providName)
{
$this->providName = $providName;
return $this;
}
/**
* Get providName
*
* #return string
*/
public function getProvidName()
{
return $this->providName;
}
/**
* Set providLogo
*
* #param string $providLogo
* #return Provider
*/
public function setProvidLogo($providLogo)
{
$this->providLogo = $providLogo;
return $this;
}
/**
* Get providLogo
*
* #return string
*/
public function getProvidLogo()
{
return $this->providLogo;
}
/**
* Set providLogoPath
*
* #param string $providLogoPath
* #return Provider
*/
public function setProvidLogoPath($providLogoPath)
{
$this->providLogoPath = $providLogoPath;
return $this;
}
/**
* Get providLogoPath
*
* #return string
*/
public function getProvidLogoPath()
{
return $this->providLogoPath;
}
/**
* Set createdTime
*
* #param \DateTime $createdTime
* #return Provider
*/
public function setCreatedTime($createdTime)
{
$this->createdTime = $createdTime;
return $this;
}
/**
* Get createdTime
*
* #return \DateTime
*/
public function getCreatedTime()
{
return $this->createdTime;
}
/**
* Set users
*
* #param \AppBundle\Entity\User $users
* #return Provider
*/
public function setUsers(\AppBundle\Entity\User $users = null)
{
$this->users = $users;
return $this;
}
/**
* Get users
*
* #return \AppBundle\Entity\User
*/
public function getUsers()
{
return $this->users;
}
/**
* If manually uploading a file (i.e. not using Symfony Form) ensure an instance
* of 'UploadedFile' is injected into this setter to trigger the update. If this
* bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
* must be able to accept an instance of 'File' as the bundle will inject one here
* during Doctrine hydration.
*
* #param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
*/
public function setImageFile(File $image = null)
{
$this->imageFile = $image;
if ($image) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
$this->updatedAt = new \DateTime('now');
}
}
/**
* #return File
*/
public function getImageFile()
{
return $this->imageFile;
}
/**
* #ORM\PrePersist
*/
public function setCreatedTimeValue()
{
$this->createdTime = new \DateTime();
}
/**
* Set providerCreatedBy
*
* #param integer $providerCreatedBy
* #return Provider
*/
public function setProviderCreatedBy($providerCreatedBy)
{
$this->providerCreatedBy = $providerCreatedBy;
return $this;
}
/**
* Get providerCreatedBy
*
* #return integer
*/
public function getProviderCreatedBy()
{
return $this->providerCreatedBy;
}
}
USER:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Doctrine\Common\Collections\ArrayCollection;
/**
* User
*
* #ORM\Table(name="tho_user")
* #ORM\Entity(repositoryClass="AppBundle\Entity\UserRepository")
* #UniqueEntity(fields="email",message = "This email already exist.")
* #UniqueEntity(fields = "username",message = "This username already taken")
*/
class User implements AdvancedUserInterface, \Serializable
{
/**
* #ORM\OneToMany(targetEntity="Provider", mappedBy="user")
*/
protected $providers;
public function __construct()
{
$this->providers = new ArrayCollection();
}
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*#Assert\NotBlank(message="Username is required!")
* #Assert\Length(min=3,minMessage="Minimum 3 characters long.")
* #ORM\Column(name="username", type="string", length=255)
*/
protected $username;
/**
* #var string
*
*
* #ORM\Column(name="password", type="string", length=255)
*/
private $password;
/**
* #var array
*
* #ORM\Column(name="roles",type="json_array")
*/
private $roles = array();
/**
* #var bool
*
* #ORM\Column(name="is_active",type="boolean")
*/
private $isActive = true;
/**
* #var string
*#Assert\NotBlank()
* #Assert\Email
* #ORM\Column(name="email",type="string", length=255)
*/
private $email;
/**
* #var string
*#Assert\NotBlank()
*
* #ORM\Column(name="first_name",type="string", length=255)
*/
private $firstName;
/**
* #var string
*#Assert\NotBlank()
* #ORM\Column(name="last_name",type="string", length=255)
*/
private $lastName;
/**
* #var string
*
* #ORM\Column(name="iata",type="string", length=255)
*/
private $iata;
/**
* #var string
*
* #ORM\Column(name="job_title",type="string", length=255)
*/
private $jobTitle;
/**
* #var string
*
* #ORM\Column(name="agency_phone",type="string", length=255)
*/
private $agencyPhone;
/**
* #var string
*
* #ORM\Column(name="emergency_mobile",type="string", length=255)
*/
private $emergencyMobile;
/**
* Storing password temporarily
* #Assert\NotBlank()
* #Assert\Length(min=6,minMessage="Minimum 6 characters long.")
* #var string
*/
private $plainPassword;
/**
* Get id
*
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set username
*
* #param string $username
* #return User
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Get username
*
* #return string
*/
public function getUsername()
{
return $this->username;
}
/**
* Set password
*
* #param string $password
* #return User
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* #return string
*/
public function getPassword()
{
return $this->password;
}
public function getRoles()
{
$roles = $this->roles;
$roles[] = 'ROLE_ADMIN';
return array_unique($roles);
}
public function setRoles(array $roles)
{
$this->roles = $roles;
return $this;
}
public function eraseCredentials()
{
$this->setPlainPassword(null);
}
public function getSalt()
{
return null;
}
/**
* #return boolean
*/
public function isIsActive()
{
return $this->isActive;
}
/**
* #param boolean $isActive
*/
public function setIsActive($isActive)
{
$this->isActive = $isActive;
}
/**
* Checks whether the user's account has expired.
*
* Internally, if this method returns false, the authentication system
* will throw an AccountExpiredException and prevent login.
*
* #return bool true if the user's account is non expired, false otherwise
*
* #see AccountExpiredException
*/
public function isAccountNonExpired()
{
return true;
}
/**
* Checks whether the user is locked.
*
* Internally, if this method returns false, the authentication system
* will throw a LockedException and prevent login.
*
* #return bool true if the user is not locked, false otherwise
*
* #see LockedException
*/
public function isAccountNonLocked()
{
return true;
}
/**
* Checks whether the user's credentials (password) has expired.
*
* Internally, if this method returns false, the authentication system
* will throw a CredentialsExpiredException and prevent login.
*
* #return bool true if the user's credentials are non expired, false otherwise
*
* #see CredentialsExpiredException
*/
public function isCredentialsNonExpired()
{
return true;
}
/**
* Checks whether the user is enabled.
*
* Internally, if this method returns false, the authentication system
* will throw a DisabledException and prevent login.
*
* #return bool true if the user is enabled, false otherwise
*
* #see DisabledException
*/
public function isEnabled()
{
return true;
}
/**
* Get isActive
*
* #return boolean
*/
public function getIsActive()
{
return $this->isActive;
}
/**
* Set email
*
* #param string $email
* #return User
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* #return string
*/
public function getEmail()
{
return $this->email;
}
/**
* (PHP 5 >= 5.1.0)<br/>
* String representation of object
* #link http://php.net/manual/en/serializable.serialize.php
* #return string the string representation of the object or null
*/
public function serialize()
{
return serialize(array(
$this->id,
$this->username,
$this->password
));
}
/**
* (PHP 5 >= 5.1.0)<br/>
* Constructs the object
* #link http://php.net/manual/en/serializable.unserialize.php
* #param string $serialized <p>
* The string representation of the object.
* </p>
* #return void
*/
public function unserialize($serialized)
{
list(
$this->id,
$this->username,
$this->password
) = unserialize($serialized);
}
/**
* #return string
*/
public function getFirstName()
{
return $this->firstName;
}
/**
* #param string $firstName
*/
public function setFirstName($firstName)
{
$this->firstName = $firstName;
}
/**
* #return string
*/
public function getLastName()
{
return $this->lastName;
}
/**
* #param string $lastName
*/
public function setLastName($lastName)
{
$this->lastName = $lastName;
}
/**
* #return string
*/
public function getIata()
{
return $this->iata;
}
/**
* #param string $iata
*/
public function setIata($iata)
{
$this->iata = $iata;
}
/**
* #return string
*/
public function getJobTitle()
{
return $this->jobTitle;
}
/**
* #param string $jobTitle
*/
public function setJobTitle($jobTitle)
{
$this->jobTitle = $jobTitle;
}
/**
* #return string
*/
public function getAgencyPhone()
{
return $this->agencyPhone;
}
/**
* #param string $agencyPhone
*/
public function setAgencyPhone($agencyPhone)
{
$this->agencyPhone = $agencyPhone;
}
/**
* #return string
*/
public function getEmergencyMobile()
{
return $this->emergencyMobile;
}
/**
* #param string $emergencyMobile
*/
public function setEmergencyMobile($emergencyMobile)
{
$this->emergencyMobile = $emergencyMobile;
}
/**
* #return string
*/
public function getPlainPassword()
{
return $this->plainPassword;
}
/**
* #param string $plainPassword
*/
public function setPlainPassword($plainPassword)
{
$this->plainPassword = $plainPassword;
}
/**
* Set provider
*
* #param \AppBundle\Entity\Provider $provider
* #return User
*/
public function setProvider(\AppBundle\Entity\Provider $provider = null)
{
$this->provider = $provider;
return $this;
}
/**
* Get provider
*
* #return \AppBundle\Entity\Provider
*/
public function getProvider()
{
return $this->provider;
}
/**
* Add providers
*
* #param \AppBundle\Entity\Provider $providers
* #return User
*/
public function addProvider(\AppBundle\Entity\Provider $providers)
{
$this->providers[] = $providers;
return $this;
}
/**
* Remove providers
*
* #param \AppBundle\Entity\Provider $providers
*/
public function removeProvider(\AppBundle\Entity\Provider $providers)
{
$this->providers->removeElement($providers);
}
/**
* Get providers
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getProviders()
{
return $this->providers;
}
}
Your metadata is
/**
* #var integer
*
* #ORM\Column(name="provider_created_by", type="integer")
*/
public $providerCreatedBy;
but you are passing an object (probably null):
$user = $this->getUser();
/* ... */
$entity->setProviderCreatedBy($user);
And the setter has no type hint:
public function setProviderCreatedBy($providerCreatedBy)
{
$this->providerCreatedBy = $providerCreatedBy;
return $this;
}
You must change your mapping of $providerCreatedBy to ManyToOne with User entity.
Check if your user has been logged in, $this->getUser() returns null if user is anonymous, also I think you should change your mapping info for providerCreatedBy field, from column type integer to ManyToOne relationship with User class..
I try to filter my elastica query by language. My query works fine, but when i add the filter, i get 0 result.
My entity :
<?php
namespace Youmiam\RecipeBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\Expose;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* Product
*
* #ORM\Table(name="product")
* #ExclusionPolicy("all")
* #ORM\Entity(repositoryClass="Youmiam\RecipeBundle\Entity\ProductRepository")
*/
class Product
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var \DateTime
*
* #ORM\Column(name="createdAt", type="datetime")
*/
private $createdAt;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, nullable=true)
*/
private $name;
/**
* #ORM\ManyToOne(targetEntity="Youmiam\RecipeBundle\Entity\Brand", inversedBy="products")
* #ORM\JoinColumn(name="brand_id", referencedColumnName="id")
*/
private $brand;
/**
* #ORM\ManyToOne(targetEntity="Youmiam\RecipeBundle\Entity\Ingredient", inversedBy="products")
* #ORM\JoinColumn(name="ingredient_id", referencedColumnName="id")
* #Expose
*/
private $ingredient;
/**
* #ORM\ManyToMany(targetEntity="Youmiam\RecipeBundle\Entity\Recipe", inversedBy="products")
* #ORM\JoinTable(name="products__recipes")
*/
private $recipes;
/**
* #ORM\OneToMany(targetEntity="Youmiam\RecipeBundle\Entity\Quantity", mappedBy="product", orphanRemoval=true, cascade={"all"})
*/
private $quantities;
/**
* #var \stdClass
*
* #ORM\Column(name="photo", type="string", length=255, nullable=true)
* #Expose
*/
private $photo;
/**
* #Assert\File(maxSize="6000000")
*/
private $file;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set createdAt
*
* #param \DateTime $createdAt
* #return Product
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* #return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set name
*
* #param string $name
* #return Product
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set ingredient
*
* #param \Youmiam\RecipeBundle\Entity\Ingredient $ingredient
* #return Product
*/
public function setIngredient(\Youmiam\RecipeBundle\Entity\Ingredient $ingredient = null)
{
$this->ingredient = $ingredient;
return $this;
}
/**
* Get ingredient
*
* #return \Youmiam\RecipeBundle\Entity\Ingredient
*/
public function getIngredient()
{
return $this->ingredient;
}
/**
* Set brand
*
* #param \Youmiam\RecipeBundle\Entity\Brand $brand
* #return Product
*/
public function setBrand(\Youmiam\RecipeBundle\Entity\Brand $brand = null)
{
$this->brand = $brand;
return $this;
}
/**
* Get brand
*
* #return \Youmiam\RecipeBundle\Entity\Brand
*/
public function getBrand()
{
return $this->brand;
}
/**
* Constructor
*/
public function __construct()
{
$this->recipes = new \Doctrine\Common\Collections\ArrayCollection();
$this->quantities = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Set photo
*
* #param string $photo
* #return Product
*/
public function setPhoto($photo)
{
$this->photo = $photo;
return $this;
}
/**
* Get photo
*
* #return string
*/
public function getPhoto()
{
return $this->photo;
}
/**
* Sets file.
*
* #param UploadedFile $file
*/
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
}
/**
* Get file.
*
* #return UploadedFile
*/
public function getFile()
{
return $this->file;
}
/**
* Add recipes
*
* #param \Youmiam\RecipeBundle\Entity\Recipe $recipes
* #return Product
*/
public function addRecipe(\Youmiam\RecipeBundle\Entity\Recipe $recipes)
{
$this->recipes[] = $recipes;
return $this;
}
/**
* Remove recipes
*
* #param \Youmiam\RecipeBundle\Entity\Recipe $recipes
*/
public function removeRecipe(\Youmiam\RecipeBundle\Entity\Recipe $recipes)
{
$this->recipes->removeElement($recipes);
}
/**
* Get recipes
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getRecipes()
{
return $this->recipes;
}
/**
* Add quantities
*
* #param \Youmiam\RecipeBundle\Entity\Quantity $quantities
* #return Product
*/
public function addQuantity(\Youmiam\RecipeBundle\Entity\Quantity $quantities)
{
$this->quantities[] = $quantities;
return $this;
}
/**
* Remove quantities
*
* #param \Youmiam\RecipeBundle\Entity\Quantity $quantities
*/
public function removeQuantity(\Youmiam\RecipeBundle\Entity\Quantity $quantities)
{
$this->quantities->removeElement($quantities);
}
/**
* Get quantities
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getQuantities()
{
return $this->quantities;
}
/**
* get the class Name
* #return string $className
*/
public function getClass()
{
return "Product";
}
/**
* #return Language
*/
public function getBrandLanguage()
{
return $this->brand->getLanguage();
}
}
My Config.yml
fos_elastica:
clients:
default: { host: localhost, port: 9200 }
indexes:
youmiam:
settings:
index:
analysis:
analyzer:
keyword_analyser:
type: custom
tokenizer: keyword
classic_analyser:
type: custom
tokenizer: lowercase
filter : [my_snow,asciifolding]
ingr_analyser:
type: custom
tokenizer: lowercase
filter : [my_ing_ngram,asciifolding]
recipe_analyser:
type: custom
tokenizer: lowercase
filter : [my_recipe_ngram,asciifolding]
testfollower:
type: stop
stopwords : [',']
filter:
my_snow:
type : "snowball"
language : "French"
my_ing_ngram:
type: "nGram"
min_gram: 3
max_gram: 8
my_recipe_ngram:
type: "nGram"
min_gram: 4
max_gram: 10
char_filter:
my_whtoa :
type : mapping
mappings : ["' '=>a",]
client: default
finder: ~
types:
product:
mappings:
name: { boost: 10, analyzer: classic_analyser }
brand: { boost: 10, analyzer: classic_analyser }
ingredient: { boost: 10, analyzer: classic_analyser }
brandLanguage: { boost: 10 }
persistence:
driver: orm
model: Youmiam\RecipeBundle\Entity\Product
provider: ~
listener: ~
finder: ~
repository: Youmiam\RecipeBundle\SearchRepository\ProductRepository
And then, in my fos elastica repository, i have this code :
$boolQuery = new \Elastica\Query\Bool();
$query = new \Elastica\Query;
$queryString = new \Elastica\Query\QueryString();
$queryString->setQuery($searchText);
$queryString->setAnalyzer('classic_analyser');
$queryString->setFields(array('product.name', 'product.brand', 'product.ingredient'));
$boolQuery->addMust($queryString);
$query->setQuery($boolQuery);
$filter = new \Elastica\Filter\Term();
$filter->setTerm('brandLanguage', 'fr');
$query->setPostFilter($filter);
return $this->find($query);
I tried to put my query directly in a controller, but same result. I really don't know why my filter return no result.
Hope someone could help, cause i'm really don't see the answer
First thing to do is testing your query outside Elastica, you can get the JSON from the Sf2 Profiler or from the logs.
Next, make sure your documents are sent in Elasticsearch the way you want (perform a simple GET /youmiam/product/_search request to see them, in a tool like Sense).
I see that your brandLanguage field is using the standard analyzer, you can see how Elasticsearch index it with a query like this: GET /youmiam/_analyze?field=brandLanguage&text=FR - is there a token with the exact value fr? If not, there will be no match.
Best practice is to set this kind of field as "not_analyzed" too.
Beside that there is no issue with your code (you could use a FilteredQuery instead of postFilter but that's not the issue), you should look more closely at what is indexed and how to query it directly via Sense, and then translate it with Elastica.
In my class I've got a UniqueEntity limitation on the "user" field (originally on the combination of "user" and "name"), which is derived from the fosuserbundle, if I put the constraint on "name" for example the constraint does its job and I get an error saying it's not unique. This does not happen on the user field however, I get no error and the entity is added to my database. Could someone point out the mistake I made?
namespace CB\DefaultBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
/**
* CustomList
*
* #ORM\Table()
* #ORM\Entity
* #UniqueEntity("user")
*/
class CustomList
{
/**
* #var integer
*
* #ORM\Column(name="Id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
* #Assert\NotBlank()
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="description", type="string", length=255, nullable=true)
*/
private $description;
/**
* #ORM\ManyToOne(targetEntity="User", inversedBy="customlists")
* #ORM\JoinColumn(name="user_id", referencedColumnName="Id")
*/
private $user;
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\ManyToMany(targetEntity="CB\DefaultBundle\Entity\Site", inversedBy="customlists")
* #ORM\JoinTable(name="customlist_site",
* joinColumns={
* #ORM\JoinColumn(name="customlist_id", referencedColumnName="Id")
* },
* inverseJoinColumns={
* #ORM\JoinColumn(name="site_id", referencedColumnName="Id")
* }
* )
*/
private $sites;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return CustomList
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set description
*
* #param string $description
* #return CustomList
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* #return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set category
*
* #param \CB\DefaultBundle\Entity\Category $category
* #return CustomList
*/
public function setCategory(\CB\DefaultBundle\Entity\Category $category = null)
{
$this->category = $category;
return $this;
}
/**
* Get category
*
* #return \CB\DefaultBundle\Entity\Category
*/
public function getCategory()
{
return $this->category;
}
/**
* Set user
*
* #param \CB\DefaultBundle\Entity\User $user
* #return CustomList
*/
public function setUser(\CB\DefaultBundle\Entity\User $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return \CB\DefaultBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
/**
* Constructor
*/
public function __construct()
{
$this->sites = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add sites
*
* #param \CB\DefaultBundle\Entity\Site $sites
* #return CustomList
*/
public function addSite(\CB\DefaultBundle\Entity\Site $sites)
{
$this->sites[] = $sites;
return $this;
}
/**
* Remove sites
*
* #param \CB\DefaultBundle\Entity\Site $sites
*/
public function removeSite(\CB\DefaultBundle\Entity\Site $sites)
{
$this->sites->removeElement($sites);
}
/**
* Get sites
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getSites()
{
return $this->sites;
}
}
Ok, I found my error, It was in my controller logic:
...
$list = new CustomList();
$form = $this->createForm(new CustomListType(), $list);
$form->submit($request);
$list = $form->getData();
$validator = $this->get('validator');
$errors = $validator->validate($list);
var_dump((string) $errors);
if ($form->isValid()){
$em = $this->getDoctrine()->getManager();
//set current user as user-value of this list
$list->setUser($this->getUser());
$em->persist($list);
$em->flush();
//prepare the response, e.g.
$response = array("code" => 100, "success" => true);
//you can return result as JSON , remember to 'use' Response!
return new Response(json_encode($response));
...
As you can see I set the user AFTER the form has been validated, meaning it would always validate the null values, as seen in the documentation here: UniqueEntity the default value of ignoreNull is true, meaning it would always pass the test.
Moving the $list->setUser($this->getUser()); line before my validation of the form fixed this problem!
I develop store using SyliusSandbox bundle.
Sylius uses xml files to store ORM schema for entities.
I've copied its xml definitions to my Bundle and use it there.
But for my own entities, I'd like to use annotations. So, basically I need to mix two types of definitions in one bundle.
If I try to persist an entity which is using annotations, I get an error that xml file for this entity was not found:
No mapping file found named
'ShopBundle\Resources\config\doctrine/ProductLocalized.orm.xml' for
class '\ShopBundle\Entity\ProductLocalized'.
My entity looks like this:
<?php
namespace Pixeljets\ShopBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Pixeljets\ShopBundle\Entity\ProductLocalized
*
* #ORM\Table(name="product_localized")
* #ORM\Entity
*/
class ProductLocalized
{
/**
* #var integer $id
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="Product", inversedBy="localized")
* #ORM\JoinColumn(name="product_id", referencedColumnName="id")
*/
protected $product;
/**
* #var integer $product_id
*
* #ORM\Column(name="product_id", type="integer")
*/
private $product_id;
/**
* #var string $title
*
* #ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* #var string $url
*
* #ORM\Column(name="url", type="string", length=255)
*/
private $url;
/**
* #var string $keywords
*
* #ORM\Column(name="keywords", type="string", length=255, nullable=true)
*/
private $keywords;
/**
* #var string $description
*
* #ORM\Column(name="description", type="string", length=255, nullable=true)
*/
private $description;
/**
* #var string $body
*
* #ORM\Column(name="body", type="text", nullable=true)
*/
private $body;
/**
* #var boolean $published
*
* #ORM\Column(name="published", type="boolean")
*/
private $published;
/**
* #var string $lang
*
* #ORM\Column(name="lang", type="string", length=2)
*/
private $lang;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set product_id
*
* #param integer $productId
* #return ProductLocalized
*/
public function setProductId($productId)
{
$this->product_id = $productId;
return $this;
}
/**
* Get product_id
*
* #return integer
*/
public function getProductId()
{
return $this->product_id;
}
/**
* Set title
*
* #param string $title
* #return ProductLocalized
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set url
*
* #param string $url
* #return ProductLocalized
*/
public function setUrl($url)
{
$this->url = $url;
return $this;
}
/**
* Get url
*
* #return string
*/
public function getUrl()
{
return $this->url;
}
/**
* Set keywords
*
* #param string $keywords
* #return ProductLocalized
*/
public function setKeywords($keywords)
{
$this->keywords = $keywords;
return $this;
}
/**
* Get keywords
*
* #return string
*/
public function getKeywords()
{
return $this->keywords;
}
/**
* Set description
*
* #param string $description
* #return ProductLocalized
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* #return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set body
*
* #param string $body
* #return ProductLocalized
*/
public function setBody($body)
{
$this->body = $body;
return $this;
}
/**
* Get body
*
* #return string
*/
public function getBody()
{
return $this->body;
}
/**
* Set published
*
* #param boolean $published
* #return ProductLocalized
*/
public function setPublished($published)
{
$this->published = $published;
return $this;
}
/**
* Get published
*
* #return boolean
*/
public function getPublished()
{
return $this->published;
}
/**
* Set lang
*
* #param string $lang
* #return ProductLocalized
*/
public function setLang($lang)
{
$this->lang = $lang;
return $this;
}
/**
* Get lang
*
* #return string
*/
public function getLang()
{
return $this->lang;
}
/**
* Set product
*
* #param Pixeljets\ShopBundle\Entity\Product $product
* #return ProductLocalized
*/
public function setProduct(\Pixeljets\ShopBundle\Entity\Product $product = null)
{
$this->product = $product;
return $this;
}
/**
* Get product
*
* #return Pixeljets\ShopBundle\Entity\Product
*/
public function getProduct()
{
return $this->product;
}
}
?>
How can I 'tell' symfony to use annotation approach for schema?
You cannot mix metadata formats inside of a bundle:
From: http://symfony.com/doc/current/book/doctrine.html
A bundle can accept only one metadata definition format. For example, it's not
possible to mix YAML metadata definitions with annotated PHP entity class definitions.
You will need to use two bundles or stick with one format.
You should think about using Translatable behavior from DoctrineExtensions to make your products support i18n. Should be much easier than creating another entity for that.