Put request entity linked to some others entity symfony - rest

i'm stucked on something: i have to entity Profile and Job
in the Job entity i have :
/**
* #var string
* #Gedmo\Translatable
* #ORM\Column(name="label", type="string", length=255)
* #Serializer\Groups({"profile_details","profile_put"})
* #Serializer\Type("string")
* #Serializer\Expose()
*/
private $label;
in the Profile entity i have :
* #var Job
*
* #Assert\NotBlank(message="app.profile.job.not_blank")
* #ORM\ManyToOne(targetEntity="App\Entity\Job")
* #ORM\JoinColumn(name="job_id", referencedColumnName="id")
* #Serializer\Groups({"profile_details","user_details","corporate_details","profile_put"})
* #Serializer\Type("App\Entity\Job")
* #Serializer\Expose()
*/
private $job;
/**
* #return Job
*/
public function getJob(): Job
{
return $this->job;
}
/**
* #param Job $job
*
* #return Profile
*/
public function setJob(Job $job): Profile
{
$this->job = $job;
return $this;
}
EDIT:
in the controller :
public function putAction(Request $request, Profile $profile): Profile
{
$profile
->setJob($request->get('job'))
$errors = $this->validator->validate($profile);
if (count($errors) > 0) {
throw new UnprocessableEntityHttpException((string) $errors);
}
$this->entityManager->persist($profile);
$this->entityManager->flush();
return $profile;
}
I'm trying to perform a put on the Profile entity to change some normal string fields. I don't know how to set the put request body on postman, i tried to put just the label but i got : "Argument 1 passed to App\\Entity\\Profile::setJob() must be an instance of App\\Entity\\Job, array given, called in /srv/api/src/Controller/ProfileController.php on line 114"

Related

Problem creating an instance of ObjectStorage

In my domain model there is a property month which is an ObjectStorage for bill elements. Here is how the domain model looks:
/**
* establishment
*
* #var ObjectStorage<Bill>
* #TYPO3\CMS\Extbase\Annotation\ORM\Cascade("remove")
*/
protected ObjectStorage $month;
public function __construct()
{
$this->setMonth(new ObjectStorage());
}
Here is the setter:
/**
* month setter
*
* #param ObjectStorage<Bill> $month
* #return void
*/
public function setMonth(ObjectStorage $month) : void
{
$this->month = $month;
}
Sadly phpstan shows the following error:
Parameter #1 $establishment of method VIC\Ext\Domain\Model\StorageRoom::setMonth() expects iterable&TYPO3\CMS\Extbase\Persistence\ObjectStorage,
TYPO3\CMS\Extbase\Persistence\ObjectStorage given.
Any Idea what is wrong?
How to build the construct for phpstan:
/**
* establishment
*
* #var ObjectStorage<Bill>
* #TYPO3\CMS\Extbase\Annotation\ORM\Cascade("remove")
*/
protected $month;
public function __construct()
{
/** #var ObjectStorage<Bill> $objectStorageBill */
$objectStorageBill = new ObjectStorage();
$this->setMonth($objectStorageBill);
}

Get the id, not the object in a ManyToOne relationship to send it from an API Rest

I've got an entity I send in a datatable (webix) thanks to an API rest. In this entity I have several ManyToOne relationeships which I don't know how to display or edit.
In my datatable, when I try to display the field which is in a relathinship I've got this instead of the id
[object Object]
When I try to edit this field by adding the id I've got this error message
Type error: Argument 1 passed to ErpBundle\Entity\Trial::setGSponsor() must be an instance of ErpBundle\Entity\Sponsor, string given
I understand what I get in my datatable is an object but I don't know how to get the Id of the name instead (and being able to chose it from the datatable).
Do I have to edit the getter? The repository?
I've tried to edit my getter like that but of course it's not working:
/**
* Get gSponsor
*
* #return \ErpBundle\Entity\Sponsor
*/
public function getGSponsor()
{
return $this->gSponsor->getId();
}
Below a part of my code:
In the controller, I'm just rendering the template for the view. Webix just take the url as an argument to get the data (url: "rest->{{ path('erp_trialapi_cgetload') }}") The datatable is displayed automatically from what I send in my ApiController
Part of my entity
class Trial
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
*
* #ORM\ManyToOne(targetEntity="ErpBundle\Entity\Sponsor")
*/
private $gSponsor;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set gSponsor
*
* #param \ErpBundle\Entity\Sponsor $gSponsor
*
* #return Trial
*/
public function setGSponsor(\ErpBundle\Entity\Sponsor $gSponsor = null)
{
$this->gSponsor->get = $gSponsor;
return $this;
}
/**
* Get gSponsor
*
* #return \ErpBundle\Entity\Sponsor
*/
public function getGSponsor()
{
return $this->gSponsor;
}
My API
class TrialApiController extends FOSRestController
{
/**
* #Rest\Get("/api_t/get")
*/
public function cgetLoadAction()
{
$em = $this->getDoctrine()->getManager();
$repo = $em->getRepository('ErpBundle:Trial');
$trials = $repo->findAll();
return $trials;
}
I'd appreciate any kind of help.
Thank you very much!

Symfony 2 Doctrine ODM returning errors on existing fields

I have a simple symfony 2 setup with Doctrine ORM and a db with some underscore seperated field names (for instance "error_page"). Querying this never gives a result (getTitle does give a result, getErrorPage is always empty) and symfony gives me an error:
Method "error_page" for object "My\CmsBundle\Document\Website" does not exist in MyCmsBundle:Default:dashboard.html.twig at line 5
I can't figure out why... My Document looks like this:
<?php
// src/My/CmsBundle/Document/Website.php
namespace My\CmsBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/**
* #MongoDB\Document(
* collection="websites"
* )
*/
class Website
{
/**
* #MongoDB\Id
*/
protected $id;
/**
* #MongoDB\String
*/
protected $slug;
/**
* #MongoDB\Field(type="string", name="error_page")
*/
protected $error_page = "";
/**
* #MongoDB\String
*/
protected $title;
/**
* #MongoDB\String(name="seo_title")
*/
protected $seo_title;
/**
* #MongoDB\String
*/
protected $seo_description;
/**
* #MongoDB\Collection
*/
protected $url = array();
/**
* Get id
*
* #return id $id
*/
public function getId()
{
return $this->id;
}
/**
* Set slug
*
* #param string $slug
* #return self
*/
public function setSlug($slug)
{
$this->slug = $slug;
return $this;
}
/**
* Get slug
*
* #return string $slug
*/
public function getSlug()
{
return $this->slug;
}
/**
* Set title
*
* #param string $title
* #return self
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string $title
*/
public function getTitle()
{
return $this->title;
}
/**
* Set errorPage
*
* #param string $errorPage
* #return self
*/
public function setErrorPage($errorPage)
{
$this->error_page = $errorPage;
return $this;
}
/**
* Get errorPage
*
* #return string $errorPage
*/
public function getErrorPage()
{
return $this->error_page;
}
/**
* Set url
*
* #param collection $url
* #return self
*/
public function setUrl($url)
{
$this->url = $url;
return $this;
}
/**
* Get url
*
* #return collection $url
*/
public function getUrl()
{
return $this->url;
}
/**
* Set seoTitle
*
* #param string $seoTitle
* #return self
*/
public function setSeoTitle($seoTitle)
{
$this->seo_title = $seoTitle;
return $this;
}
/**
* Get seoTitle
*
* #return string $seoTitle
*/
public function getSeoTitle()
{
return $this->seo_title;
}
/**
* Set seoDescription
*
* #param string $seoDescription
* #return self
*/
public function setSeoDescription($seoDescription)
{
$this->seo_description = $seoDescription;
return $this;
}
/**
* Get seoDescription
*
* #return string $seoDescription
*/
public function getSeoDescription()
{
return $this->seo_description;
}
}
Document creation via this document works fine by the way. The field name is also set to error_page as expected... I'm at a loss here :S
Actually, Doctrine+Symfony2 assume camel case variable naming. Twig using the getter method names should be obvious, how should it access protected/private variables? It needs a name for something public : the getter. You're probably wondering why "get" is ignored; it is a simplification for designers as they normally shouldnt know about what "getters" are and the difference between methods and variables.
so in your twig file ,change :
{{document.error_page}}
to
{{document.errorPage}}
this would helpful.

How to store MongoDB\ReferenceOne with Sonata Admin?

I have a problem Sonata Admin and storing a reference in a MongoDB document. I set up Sonata Admin correct (without reference it works well).
But if i try to store a user-reference for the cinema-document, i get the following error.
Error
Symfony\Component\Validator\ConstraintViolation
Object(Symfony\Component\Form\Form).children[user_id] = 5511710289d39769378b4567
Caused by:
Symfony\Component\Form\Exception\TransformationFailedException
Unable to reverse value for property path "user_id": The choice "5511710289d39769378b4567" does not exist or is not unique
Caused by:
Symfony\Component\Form\Exception\TransformationFailedException
The choice "5511710289d39769378b4567" does not exist or is not unique
Cinema.php
namespace Cinemato\AppBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Cinemato\UserBundle\Document\User as User;
/**
* #MongoDB\Document
*/
class Cinema
{
/**
*
* #var integer
* #MongoDB\Id
*/
private $id;
/**
*
* #var string
* #MongoDB\String
*/
private $title;
/**
* #MongoDB\ReferenceOne(targetDocument="Cinemato\UserBundle\Document\User")
*/
private $user_id;
/**
* Get id
*
* #return id $id
*/
public function getId()
{
return $this->id;
}
/**
* Set userId
*
* #param Cinemato\AppBundle\Document\User $userId
* #return self
*/
public function setUserId(\Cinemato\AppBundle\Document\User $userId)
{
$this->user_id = $userId;
return $this;
}
/**
* Get userId
*
* #return Cinemato\AppBundle\Document\User $userId
*/
public function getUserId()
{
return $this->user_id;
}
}
To solve the problem the CinemaAdmin.php must be edited - sonata_type_model_list must be added in configureFormFields.
/**
* #param FormMapper $formMapper
*/
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('title')
->add('user_id', 'sonata_type_model_list')
;
}

add role in zfcuser form registration

i am using zfcuser, bjyauthorize et roleuserbridge.
i want to add a field in the form registration. I followed this tutorial step :
http://resoftsol.com/adding-custom-fields-to-zfcuser-register-form/
in the module front i have added :
- the directory entity with files user et userinterface:
namespace Front\Entity;
interface UserInterface
{
/**
* Get id.
*
* #return int
*/
public function getId();
/**
* Set id.
*
* #param int $id
* #return UserInterface
*/
public function setId($id);
/**
* Get username.
*
* #return string
*/
public function getUsername();
/**
* Set username.
*
* #param string $username
* #return UserInterface
*/
public function setUsername($username);
/**
* Get email.
*
* #return string
*/
public function getEmail();
/**
* Set email.
*
* #param string $email
* #return UserInterface
*/
public function setEmail($email);
/**
* Get displayName.
*
* #return string
*/
public function getDisplayName();
/**
* Set displayName.
*
* #param string $displayName
* #return UserInterface
*/
public function setDisplayName($displayName);
/**
* Get password.
*
* #return string password
*/
public function getPassword();
/**
* Set password.
*
* #param string $password
* #return UserInterface
*/
public function setPassword($password);
/**
* Get state.
*
* #return int
*/
public function getState();
/**
* Set state.
*
* #param int $state
* #return UserInterface
*/
public function setState($state);
/**
* Get role.
*
* #return string
*/
public function getRole();
/**
* Set role.
*
* #param string $role
* #return UserInterface
*/
public function setRole($role);
}
++++++++++++++++++++
namespace Font\Entity;
class User implements UserInterface
{
/**
* #var int
*/
protected $id;
/**
* #var string
*/
protected $username;
/**
* #var string
*/
protected $email;
/**
* #var string
*/
protected $displayName;
/**
* #var string
*/
protected $password;
/**
* #var int
*/
protected $state;
/**
* #var string
*/
protected $role;
/**
* Get id.
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set id.
*
* #param int $id
* #return UserInterface
*/
public function setId($id)
{
$this->id = (int) $id;
return $this;
}
/**
* Get username.
*
* #return string
*/
public function getUsername()
{
return $this->username;
}
/**
* Set username.
*
* #param string $username
* #return UserInterface
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Get email.
*
* #return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set email.
*
* #param string $email
* #return UserInterface
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get displayName.
*
* #return string
*/
public function getDisplayName()
{
return $this->displayName;
}
/**
* Set displayName.
*
* #param string $displayName
* #return UserInterface
*/
public function setDisplayName($displayName)
{
$this->displayName = $displayName;
return $this;
}
/**
* Get password.
*
* #return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Set password.
*
* #param string $password
* #return UserInterface
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get state.
*
* #return int
*/
public function getState()
{
return $this->state;
}
/**
* Set state.
*
* #param int $state
* #return UserInterface
*/
public function setState($state)
{
$this->state = $state;
return $this;
}
/**
* Get role.
*
* #return string
*/
public function getRole()
{
return $this->role;
}
/**
* Set role.
*
* #param string $role
* #return UserInterface
*/
public function setRole($role)
{
$this->role = $role;
return $this;
}
}
++++++++++++++++++++++++++
also i have add the mapp directory.
i had the following error :
Catchable fatal error: Argument 1 passed to ZfcUser\Validator\AbstractRecord::setMapper()
must be an instance of ZfcUser\Mapper\UserInterface, instance of Front\Mapper\User given,
called in C:\wamppp\www\projet\vendor\zendframework\zendframework\library\Zend\Validator
\AbstractValidator.php on line 139 and defined in C:\wamppp\www\projet\vendor\zf-commons
\zfc-user\src\ZfcUser\Validator\AbstractRecord.php on line 65
I just had the problem myself and was able to solve it... finally.
Copy the ZfcUser\Validator folder into your module.
Adapt the namespace and change the expected Object type of the method AbstractRecord::setMapper to your user entity / interface, whatever you have there now.
Also, in the code you provided the namespaces arent identical. Yout got "Front" and "Font" there.
Edit: forgot the important part xD
After you have done that you need the following code in the config file (my module is called User):
'zfcuser_register_form' => function ($sm) {
$options = $sm->get('zfcuser_module_options');
$form = new ZfcUser\Form\Register(null, $options);
//$form->setCaptchaElement($sm->get('zfcuser_captcha_element'));
$form->setInputFilter(new ZfcUser\Form\RegisterFilter(
new User\Validator\NoRecordExists(array(
'mapper' => $sm->get('zfcuser_user_mapper'),
'key' => 'email'
)),
new User\Validator\NoRecordExists(array(
'mapper' => $sm->get('zfcuser_user_mapper'),
'key' => 'username'
)),
$options
));
return $form;
},
I hope this helps.
You should not create your own interface(and if you do, your new interface should extend ZfcUser\Entity\UserInterface), instead just make your user entity extend the ZfcUser\Entity\UserInterface.