So i send the data to the controller using ajax and i want to insert it in the comment table; everything works fine if i delete these two lines about inserting current datetime for the comment and setting my entity nullable
but how can i insert current datetime ;
Controller Code
if($request->get('texte')==NULL)
{
throw new AccessDeniedException('This user does not have access
to this section.');
}
$user = $this->getUser();
if (!is_object($user) || !$user instanceof UserInterface)
{
throw new AccessDeniedException('This user does not have access
to this section.');
}
$comment = new Commentaire();
$em = $this->getDoctrine()->getManager();
$veterinaire=$em->getRepository("MyAppUserBundle:User")->findOneById($request->get('cible'));
$comment->setIdCible($veterinaire);
$comment->setIdClient($user);
$comment->setTexte($request->get('texte'));
$literalTime = \DateTime::createFromFormat("d/m/Y H:i",date_default_timezone_get());
$comment->setDate($literalTime);
$em->persist($comment);
$em->flush();
return new Response("");
}
My Entity :
/**
* #var \DateTime
*
* #ORM\Column(name="date", type="datetime", nullable=false)
*/
private $date;
I even tried to set my AppKernel __construct
public function __construct($environment, $debug)
{
date_default_timezone_set( 'Africa/Tunis' );
parent::__construct($environment, $debug);
}
You just need to pass a DateTime object to setDate:
$comment->setDate(new \DateTime());
or if you want to specify timezone:
$comment->setDate(new \DateTime('now', new \DateTimeZone('Africa/Tunis')));
Of course you can extract creation of DateTime object from the setDate and have additional variable to pass.
Related
I'm trying to save my ID in my relation ManyToOne, but an error returned:
This is how i'm trying to save data:
$user = $this->getUser()->getId();
$catalogcomment = new CatalogComment();
$form = $this->createForm(CatalogCommentType::class, $catalogcomment);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$catalogcomment->setUserId($user);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($catalogcomment);
$entityManager->flush();
return $this->redirectToRoute('catalog_index');
}
And this is my Entity CatalogComment related with the relation user_id
public function getUserId(): ?User
{
return $this->user_id;
}
public function setUserId(?User $user_id): self
{
$this->user_id = $user_id;
return $this;
}
The error received is:
Argument 1 passed to App\Entity\CatalogComment::setUserId() must be an instance of App\Entity\User or null, int given
What i'm doing wrong?
Thanks for your time.
I think you have to adjust your mapped relationship in the Entity CatalogComment not to have a property $userId but instead a property $user which should be of type User
class CatalogComment
{
// ...
/**
* #ManyToOne(targetEntity="User")
* #JoinColumn(name="user_id", referencedColumnName="id")
*/
private $user;
}
You have to create getter and setter for $user too, and then you can set the user in an CatalogComment Object as follows
$user = $this->getUser();
$catalogComment = new CatalogComment();
$catalogComment->setUser($user);
$em = $this->getDoctrine()->getManager();
$em->persist($catalogComment);
$em->flush();
Hope it helps :)
I have a User entity that contains address. I will save address as a json in my database. After form validation, I have to manually serialize address before persisting data. Is there anyway to avoid doing it like that ? Is it possible to call serialize event when doctrine is persisting data ?
class User{
/**
* #ORM\Column(name="username", type="string", length=30)
**/
private $username;
/**
* #ORM\Column(name="address", type="json")
**/
private $address;
}
class Address{
private $postalcode;
private $street;
}
// Inside my controller
class UserController extends Controller{
/**
* #Rest\View(StatusCode = Response::HTTP_CREATED)
*
* #Rest\Post(
* path = "/user",
* name = "user_create"
* )
*/
public function createAction(){
$user = new User();
$form = $this->createForm(UserType::class, $user);
$form->submit($request->request->all());
if ($form->isValid())
{
$em = $this->getDoctrine()->getManager();
$user->setAddress($this->get('jms_serializer')->serialize($user->getAddress(), 'json'));
$em->persist($user);
$em->flush();
return $this->view($user, Response::HTTP_CREATED);
}
return $form;
}
}
Doctrine failed to make it json because address property was private (PHP json_encode returning empty sctructure). Making it public resolve the problem.
When defining type as json, doctrine will use php's json encoding functions: https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/types.html#json-array
Thanks
I am trying to embed a collection of forms, but I am having trouble persisting the newly created objects.
A Customer has many Emails. In my controller,
// CustomersController.php
$customer = new Customer();
$customer->setCreatedBy(0);
$blankEmail = new Email();
$customer->addEmail($blankEmail);
$form = $this->createForm(new CustomerType(), $customer);
I did remember to set the cascade option in my Customer class:
// Customer.php
...
/**
* #ORM\OneToMany(targetEntity="Email", mappedBy="customer", cascade={"persist"})
*/
protected $emails;
My Email class also has the required information:
// Email.php
...
/**
* #ORM\ManyToOne(targetEntity="Customer", inversedBy="emails", cascade={"persist"})
* #ORM\JoinColumn(name="customers_id", referencedColumnName="id")
*/
protected $customer;
For some reason, this doesn't work:
if ($request->isMethod('POST')) {
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($customer);
$em->flush();
It adds the customer alright, but when it tries to add the Email, it says no customerId has been set. So, I tried this, and it works:
if ($request->isMethod('POST')) {
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
foreach ($customer->getEmails() as $email)
$email->setCustomer($customer);
$em->persist($email);
}
$em->persist($customer);
$em->flush();
But I'd really like to be able to get it in one fell swoop, just by persisting the $customer object (as I know it can).
Try to change the default "addEmail" method in your Customer class.
This method should look like:
public function addEmail($email)
{
$email->setCustomer($this);
$this->emails[] = $email;
return $this;
}
I've searched a lot about this, and seriously asking is my last resource, doctrine is kicking me hard.
I have an entity named "Contract" and another "Request", a Contract may have several Requests, when adding a new Request I search for an existent contract of that client and associate it if already exists or create it if not.
In RequestRepository.php:
public function findOrCreate($phone)
{
$em = $this->getEntityManager();
$contract = $this->findOneBy(array('phone' => $phone));
if($contract === null)
{
$contract = new Contract();
$contract->setPhone($phone)
->setDesDate(new \DateTime());
# save only if new
$em->persist($contract);
}
return $contract;
}
The thing is, when the contract is new it works ok, but when is "reused" from db I can't modify its attributes. I checked the OneToMany and ManyToOne already.
In Contract.php:
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\OneToMany(targetEntity="Request", mappedBy="contract")
*/
private $id;
In Request.php:
/**
* #var string
*
* #ORM\JoinColumn(nullable=false)
* #ORM\ManyToOne(targetEntity="Cid\FrontBundle\Entity\Contract", inversedBy="id", cascade={"persist"})
*/
protected $contract;
I also have a method which modifies an attribute within Contract.php:
public function addTime($months)
{
$days = $months * 30;
$this->des_date->add(new \DateInterval("P".$days."D"));
return $this;
}
I create the request and "findOrCreate" a contract, but if the later is not "fresh" the addTime does not save to db.
What am I doing wrong?
Edit: The controller is a common CRUD with minor modifications.
Don't worry about "request" name clash, the actual code is in spanish, Request = Solicitud
public function createAction(Request $req)
{
$entity = new Request();
$form = $this->createForm(new RequestType(), $entity);
$form->bind($req);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$entity->setUser($this->getUser());
$data = $request->request->get('cid_frontbundle_requesttype');
$phone = $data['phone_number'];
$reqRep = $em->getRepository('FrontBundle:Request');
$entity = $reqRep->newRequest($entity, $phone);
return $this->redirect($this->generateUrl('request_show', array('id' => $entity->getId())));
}
return $this->render('FrontBundle:Request:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}
The newRequest:
public function newRequest($request, $phone)
{
$em = $this->getEntityManager();
$contractRep = $em->getRepository('FrontBundle:Contract');
$contract = $contractRep->findOrCreate($phone);
$contract->addTime(123); # this is the problem, I use var_dump and this method works, but doesn't persists
$em->persist($request);
$em->flush();
return $request;
}
Eureka!! The issue was that doctrine seems to check the objects by reference, and all I did with the contract was adding a DateInterval to a DateTime property, so the object was the same for doctrine's matter and there was no saving. This is the code that made it.
public function addTime($months)
{
$days = $months * 30; # I know DateInterval has months but this is company policy ;)
$other = new \DateTime($this->des_date->format('Y-m-d')); # creating a brand new DateTime did the trick
$other->add(new \DateInterval("P".$days."D"));
$this->des_date = $other;
return $this;
}
Thanks for everything #cheesemacfly.
I'm trying to save a date (user's birthday).
$values['name'] = $request->get('name');
$values['fbid'] = $request->get('fbid');
$values['birthdate'] = date("Y-m-d", $request->get('birthdate'));
$em = $this->getDoctrine()->getEntityManager();
$user = new User();
$user->setName($values['name']);
$user->setFbId($values['fbid']);
$user->setBirthdate($values['birthdate']);
$em->persist($user);
$em->flush();
This is however not working. I'm not seeing any errors. What could be wrong? When I delete the setter for the birthday, the user gets inserted into the database perfectly.
Edit:
/**
* #ORM\Column(type="date", nullable=true)
*/
protected $birthdate;
/**
* Set date
*
* #param date $birthdate
*/
public function setBirthdate($birthdate)
{
$this->birthdate = $birthdate;
}
Try setting
$values['birthdate'] = date("Y-m-d", $request->get('birthdate'));
To
$dateTime = new \DateTime();
$values['birthdate'] = $dateTime->setTimestamp($request->get('birthdate'));
Or look into how you can create a DateTime to pass to doctrine