I'm trying to save a datetime value using an input but I keep getting the error
Call to a member function format() on string
This is how I've been trying to save the value.
/**
* #var \DateTime
*
* #ORM\Column(name="end_date", type="datetime", nullable=true)
*/
private $endDate;
/**
* Set endDate
*
* #param \DateTime $endDate
*
* #return voorstellingen
*/
public function setEndDate($endDate)
{
$time = new \DateTime($this->endDate = $endDate);
$time->format('Y-m-d');
return $time;
}
I'm not sure where I'm going wrong. Can anyone help me?
edit: changing my setter to
/**
* Set endDate
*
* #param \DateTime $endDate
*
* #return voorstellingen
*/
public function setEndDate($endDate)
{
$this->endDate = new \DateTime($endDate);
return $this;
}
solved the issue.
Like jbafford said setters in Symfony Should return $this.
Related
I'm in a deadend and can't seem to find my way out of this apparently simple problem.
I'm working with Symfony2 and trying to create a database table from an Entity that I generated with the generate:doctrine:entity command. But when I run doctrine:schema:create I always get a "No Metadata Classes to process" message like Doctrine can't find my entity.
I checked the database, the namespace, the annotations, tried creating other entities manually instead of by using the generate:doctrine:entity command but I always get the same result.
Here is my entity :
namespace Blogger\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="blog")
*/
class Blog
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* #var string
*
* #ORM\Column(name="author", type="string", length=100)
*/
private $author;
/**
* #var string
*
* #ORM\Column(name="blog", type="text")
*/
private $blog;
/**
* #var string
*
* #ORM\Column(name="image", type="string", length=20)
*/
private $image;
/**
* #var string
*
* #ORM\Column(name="tags", type="text")
*/
private $tags;
/**
* #var \DateTime
*
* #ORM\Column(name="created", type="datetime")
*/
private $created;
/**
* #var \DateTime
*
* #ORM\Column(name="updated", type="datetime")
*/
private $updated;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* #param string $title
*
* #return Blog
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set author
*
* #param string $author
*
* #return Blog
*/
public function setAuthor($author)
{
$this->author = $author;
return $this;
}
/**
* Get author
*
* #return string
*/
public function getAuthor()
{
return $this->author;
}
/**
* Set blog
*
* #param string $blog
*
* #return Blog
*/
public function setBlog($blog)
{
$this->blog = $blog;
return $this;
}
/**
* Get blog
*
* #return string
*/
public function getBlog()
{
return $this->blog;
}
/**
* Set image
*
* #param string $image
*
* #return Blog
*/
public function setImage($image)
{
$this->image = $image;
return $this;
}
/**
* Get image
*
* #return string
*/
public function getImage()
{
return $this->image;
}
/**
* Set tags
*
* #param string $tags
*
* #return Blog
*/
public function setTags($tags)
{
$this->tags = $tags;
return $this;
}
/**
* Get tags
*
* #return string
*/
public function getTags()
{
return $this->tags;
}
/**
* Set created
*
* #param \DateTime $created
*
* #return Blog
*/
public function setCreated($created)
{
$this->created = $created;
return $this;
}
/**
* Get created
*
* #return \DateTime
*/
public function getCreated()
{
return $this->created;
}
/**
* Set updated
*
* #param \DateTime $updated
*
* #return Blog
*/
public function setUpdated($updated)
{
$this->updated = $updated;
return $this;
}
/**
* Get updated
*
* #return \DateTime
*/
public function getUpdated()
{
return $this->updated;
}
}
I'm getting out of ideas. I saw other similar questions but none that could help me. If someone knows what to do, I'd love your help ;)
Something weird happened to me when I put my entity class in a directory called Models, so my namespace was:
AppBundle\Models
But it didn't work.
So I removed that folder and create another folder exactly named Entity and it worked.
So try to remove BlogBundle and put your Entity folder right under you Appbundle folder
Most of the time This error come because of entity path issue and cache
run command first
doctrine:cache:clear-metadata
and then try
doctrine:schema:create
I had the same problem. Try to remove Blogger from your namespace:
namespace BlogBundle\Entity;
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 trying to create a many to one unidirectional relationship between two tables. dateTime and availibilityList are two entities, one list may have multiple date and time.
I added the annotations likewise in the entity dateTime but it is showing me this error:
{
code: 500
message: "[Semantical Error] line 0, col 84 near 'd WHERE d.offerAvailibilityId': Error: Class StreetBumb\ApiBundle\Entity\availibilityList has no association named dateTime"
}
I updated my schema from the terminal by using this:
sudo php app/console doctrine:schema:update --force
It says : your database is already in sync with the current entity metadata.
dateTime Entity:
<?php
namespace StreetBumb\ApiBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* dateTime
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="StreetBumb\ApiBundle\Entity\dateTimeRepository")
*/
class dateTime
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var \DateTime
*
* #ORM\Column(name="startDate", type="date")
*/
private $startDate;
/**
* #var \DateTime
*
* #ORM\Column(name="endDate", type="date")
*/
private $endDate;
/**
* #var \DateTime
*
* #ORM\Column(name="startTime", type="time")
*/
private $startTime;
/**
* #var \DateTime
*
* #ORM\Column(name="endTime", type="time")
*/
private $endTime;
/**
* #var string
*
* #ORM\Column(name="type", type="string", length=55)
*/
private $type;
/**
* #var DateTime $offerAvailibilityId
*
* #ORM\ManyToOne(targetEntity="AvailibilityList")
* #ORM\JoinColumn(name="offerAvailibilityId", referencedColumnName="id")
*/
private $offerAvailibilityId;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set startDate
*
* #param \DateTime $startDate
* #return dateTime
*/
public function setStartDate($startDate)
{
$this->startDate = $startDate;
return $this;
}
/**
* Get startDate
*
* #return \DateTime
*/
public function getStartDate()
{
return $this->startDate;
}
/**
* Set endDate
*
* #param \DateTime $endDate
* #return dateTime
*/
public function setEndDate($endDate)
{
$this->endDate = $endDate;
return $this;
}
/**
* Get endDate
*
* #return \DateTime
*/
public function getEndDate()
{
return $this->endDate;
}
/**
* Set startTime
*
* #param \DateTime $startTime
* #return dateTime
*/
public function setStartTime($startTime)
{
$this->startTime = $startTime;
return $this;
}
/**
* Get startTime
*
* #return \DateTime
*/
public function getStartTime()
{
return $this->startTime;
}
/**
* Set endTime
*
* #param \DateTime $endTime
* #return dateTime
*/
public function setEndTime($endTime)
{
$this->endTime = $endTime;
return $this;
}
/**
* Get endTime
*
* #return \DateTime
*/
public function getEndTime()
{
return $this->endTime;
}
/**
* Set type
*
* #param string $type
* #return dateTime
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* Get type
*
* #return string
*/
public function getType()
{
return $this->type;
}
/**
* Set offerAvailibilityId
*
* #param integer $offerAvailibilityId
* #return dateTime
*/
public function setOfferAvailibilityId($offerAvailibilityId)
{
$this->offerAvailibilityId = $offerAvailibilityId;
return $this;
}
/**
* Get offerAvailibilityId
*
* #return integer
*/
public function getOfferAvailibilityId()
{
return $this->offerAvailibilityId;
}
}
The function in repository i am calling in controller.
public function findOpeningDetailById($id)
{
$qb = $this->getEntityManager()->createQueryBuilder()
->select('list')
->from('StreetBumbApiBundle:availibilityList', 'list')
->innerJoin('list.dateTime', 'd')
->where('d.offerAvailibilityId = :id')->setParameter('id', $id) //This line is showing error
->getQuery()->getResult();
return $qb;
}
Is there any problem in my join query or the relation i made(Many to one) is incorrect?
Please guide..
Thank you
Let's say that I have an entity.
This entity has two field that should be changed only through one function, because of domain logic
How could I make the form framework to set the values using one method call.
What I've read about data transformers, lead me to the impression that it can not be used to this propose.
Next is form events, this are the available events
PRE_BIND
BIND
POST_BIND
PRE_SET_DATA
POST_SET_DATA
BIND_CLIENT_DATA
BIND_NORM_DATA
SET_DATA
but the documentation about this is very scarce.
This is a sample entity
<?php
namespace X3\TestBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Test
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="X3\TestBundle\Entity\TestRepository")
*/
class Test
{
/**
* #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)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="firstValue", type="string", length=255)
*/
private $firstValue;
/**
* #var string
*
* #ORM\Column(name="secondValue", type="string", length=255)
*/
private $secondValue;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return Test
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set firstValue
*
* #param string $firstValue
* #return Test
*/
protected function setFirstValue($firstValue)
{
$this->firstValue = $firstValue;
return $this;
}
/**
* Get firstValue
*
* #return string
*/
public function getFirstValue()
{
return $this->firstValue;
}
/**
* Set secondValue
*
* #param string $secondValue
* #return Test
*/
protected function setSecondValue($secondValue)
{
$this->secondValue = $secondValue;
return $this;
}
/**
* Get secondValue
*
* #return string
*/
public function getSecondValue()
{
return $this->secondValue;
}
//
// The objective here is that the form use this function
// to set the two values in one call
//
protected function setValues($firstValue, $secondValue)
{
$this->firstValue = $firstValue;
$this->secondValue = $secondValue;
return $this;
}
}
Do note that setFirstValue($firstValue) and setSecondValue($secondValue) are protected, the values should be set using the method setValues($firstValue, $secondValue)
Is there an event I can use, to retrieve the firstValue and secondValue and set it using setValues($firstValue, $secondValue) and avoid the form component to complain about Method "setFirstValue()" is not public in class...?
Some code or link to it, would be a bonus.
Extending the form framework would not be easy, even if using some of form events.
One possible solution would be simply create values property in the entity which isn't mapped to any database field, and its setter and getter. Then the setValues function can do whatever needed to set firstValue and secondValue.
You may want to use DataTransformer. It allows you to control the transformation of data in both directions. Anyway you need to add values field to form, that will transformed to two fields and saved as one field.
I would like to save my users search on my website. Thaht's why i have a Class User and i would like to create Search Class.
I have done that :
class Search
{
public function __construct()
{
$this->searched_date = new \Datetime();
}
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue
*/
private $id;
/**
* #ORM\Id
* #ORM\ManyToOne(targetEntity="test\UserBundle\Entity\User")
*/
private $user;
/**
* #ORM\Column(name="termsearch", type="string", length=255, nullable="true")
*/
private $termsearch;
/**
* #ORM\Column(name="goodtitle", type="string", length=255, nullable="true")
*/
private $goodtitle;
/**
* #ORM\Column(name="searched_date", type="datetime")
*/
private $searched_date;
/**
* Set termsearch
*
* #param text $termsearch
*/
public function setTermsearch($termsearch)
{
$this->termsearch = $termsearch;
}
/**
* Get termsearch
*
* #return text
*/
public function getTermsearch()
{
return $this->termsearch;
}
/**
* Set searched_date
*
* #param datetime $searchedDate
*/
public function setSearchedDate($searchedDate)
{
$this->searched_date = $searchedDate;
}
/**
* Get searched_date
*
* #return datetime
*/
public function getSearchedDate()
{
return $this->searched_date;
}
/**
* Set user
*
* #param test\UserBundle\Entity\User $user
*/
public function setUser(\test\UserBundle\Entity\User $user)
{
$this->user = $user;
}
/**
* Get user
*
* #return test\UserBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
/**
* Set goodtitle
*
* #param text $goodtitle
*/
public function setGoodtitle($goodtitle)
{
$this->goodtitle = $goodtitle;
}
/**
* Get goodtitle
*
* #return text
*/
public function getGoodtitle()
{
return $this->goodtitle;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
}
And i would like to insert like that :
$em = $this->getDoctrine()->getEntityManager();
$user = $em->getRepository('TestUserBundle:User')->find($currentuser->getID());
$search = new Search();
$search->setUser($user);
$search->setTermsearch($termsearch);
$search->setGoodtitle($goodtitle);
$em->persist($search);
$em->flush();
Unfortunately i have this error :
SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens (500 Internal Server Error)
And in the stack we can found that :
INSERT INTO s_search (user_id, termsearch, goodtitle, searched_date) VALUES (?, ?, ?, ?) ({"1":"c2c","2":"C2C Down The Road","3":{"date":"2012-10-31 00:18:47","timezone_type":3,"timezone":"Europe\/Paris"}})
I don't know how i can create this class Search...
Thank for for you help !
Remove #ORM\Id from the $user as #ManyToOne mapping reference does not require a type. See Doctrine's Annotation Reference for details. Doctrine takes care of the correct column type to hold the reference to another entity.
Make also sure that your User query really returns a valid $user. If it's possible that Search does not have $user, use #JoinColumn annotation to claim the column nullable. See another SO question Doctrine 2 can't use nullable=false in manyToOne relation?