Problem creating an instance of ObjectStorage - typo3

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);
}

Related

APIPlatform (Symfony/REST) - "Error: Bad Request : Invalid IRI\"1\""

there is the situation :
I would like to send information to my REST API (working on Symfony), but when i tried to send this line, i got some errors that i don't understand, like : This one .
I really tried to resolve it alone, but nothing helped me on forums.
I think about an attribute than can't probably be set to editiion, or the reverse : an attribute which is not in edition and has to be.
This is what i want : be able to modify "dateCommande" (attribute for Purchase Date) , "statut" (used for see the statement of the purchase) and "idUsager" (used for put an id for a user); and finally be able to create automatically an id ("$id").
Thanks beforehand to help me ! ^^
Code of my class "Categorie.php" :
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\RangeFilter;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\NotBlank;
/**
* Commande
*
* #ORM\Table(name="commande", indexes={#ORM\Index(name="IDX_6EEAA67DCF034CDB", columns={"id_usager_id"})})
* #ORM\Entity
* #ApiResource(normalizationContext={"groups"={"commande"},"enable_max_depth"=true},denormalizationContext={"groups"={"commandeecriture"},"enable_max_depth"=true})
*/
class Commande
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
* #Groups({"commande"})
* #NotNull(message="L'id ne peut être null")
*/
private $id;
/**
* #var \DateTime
*
* #ORM\Column(name="date_commande", type="date", nullable=false)
* #Groups({"commande","commandeecriture"})
* #NotBlank(message="Date non renseignée")
*/
private $dateCommande;
/**
* #var string
*
* #ORM\Column(name="statut", type="string", length=255, nullable=false)
* #Groups({"commande","commandeecriture"})
* #NotBlank(message="Statut non renseigné")
*/
private $statut;
/**
* #var User
*
* #ORM\ManyToOne(targetEntity="User")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="id_usager_id", referencedColumnName="id")
* })
* #Groups({"commande", "commandeecriture"})
* #NotNull(message="L'id ne peut être null")
*/
private $idUsager;
/**
* Constructor
*/
public function __construct()
{
$this->idArticle = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* #return int
*/
public function getId(): int
{
return $this->id;
}
/**
* #param int $id
*/
public function setId(int $id): void
{
$this->id = $id;
}
/**
* #return \DateTime
*/
public function getDateCommande(): \DateTime
{
return $this->dateCommande;
}
/**
* #param \DateTime $dateCommande
*/
public function setDateCommande(\DateTime $dateCommande): void
{
$this->dateCommande = $dateCommande;
}
/**
* #return string
*/
public function getStatut(): string
{
return $this->statut;
}
/**
* #param string $statut
*/
public function setStatut(string $statut): void
{
$this->statut = $statut;
}
/**
* #return User
*/
public function getIdUsager(): User
{
return $this->idUsager;
}
/**
* #param User $idUsager
*/
public function setIdUsager(User $idUsager): void
{
$this->idUsager = $idUsager;
}
}
api-platform uses IRI instead of entity ids, try to replace idUsager="1" by idUsager="/usager/1". I'm guessing usager is the name of the related entity but if it's not then you just need to replace the actual name in the snippet in general it must be /related-entity-name/id

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 implement multiple file upload in TYPO3 Front End Extension

My requirement is to implement a multiple fileupload field in TYPO3 Front-end Extension. Here is what I've used for a single file upload.
My Fields in Model
/**
* angebotuploaden
*
* #var \TYPO3\CMS\Extbase\Domain\Model\FileReference
*/
protected $angebotuploaden = NULL;
/**
* Returns the angebotuploaden
*
* #return \TYPO3\CMS\Extbase\Domain\Model\FileReference $angebotuploaden
*/
public function getAngebotuploaden() {
return $this->angebotuploaden;
}
/**
* Sets the angebotuploaden
*
* #param \TYPO3\CMS\Extbase\Domain\Model\FileReference $angebotuploaden
* #return void
*/
public function setAngebotuploaden(\TYPO3\CMS\Extbase\Domain\Model\FileReference $angebotuploaden) {
$this->angebotuploaden = $angebotuploaden;
}
Now I face issues in implementing multiple file-uploads for this field. Please help me to sort it out.
Use ObjectStorage to get an 1:n-Relation to the FileReference model. In your model that could look like this:
/**
* uploadFiles
*
* #var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference>
* #cascade remove
*/
protected $uploadFiles = NULL;
/**
* __construct
*/
public function __construct() {
//Do not remove the next line: It would break the functionality
$this->initStorageObjects();
}
/**
* Initializes all ObjectStorage properties
* Do not modify this method!
* It will be rewritten on each save in the extension builder
* You may modify the constructor of this class instead
*
* #return void
*/
protected function initStorageObjects() {
$this->uploadFiles = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
}
/**
* Adds a UploadFile
*
* #param \TYPO3\CMS\Extbase\Domain\Model\FileReference $uploadFile
* #return void
*/
public function addUploadFile(\TYPO3\CMS\Extbase\Domain\Model\FileReference $uploadFile) {
$this->uploadFiles->attach($uploadFile);
}
/**
* Removes a UploadFile
*
* #param \TYPO3\CMS\Extbase\Domain\Model\FileReference $uploadFileToRemove The UploadFile to be removed
* #return void
*/
public function removeUploadFile(\TYPO3\CMS\Extbase\Domain\Model\FileReference $uploadFileToRemove) {
$this->uploadFiles->detach($uploadFileToRemove);
}
/**
* Returns the uploadFiles
*
* #return \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference> $uploadFiles
*/
public function getUploadFiles() {
return $this->uploadFiles;
}
/**
* Sets the uploadFiles
*
* #param \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference> $uploadFiles
* #return void
*/
public function setUploadFiles(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $uploadFiles) {
$this->uploadFiles = $uploadFiles;
}
There're still more things to do, especially in TCA, but I don't know them in detail because I didn't use that yet. See Hemult Hummels Upload Example an this question for more detailed information.

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.

Create and save a value object in CommandController

im trying to create and save a value object in my ImportCommandController.php, but only the entity will be saved.
Let me show some code:
// Create Entity "Fewo"
$fewo = new Fewo();
$fewo->setTitle('MyFewo');
...
// Create Value Object "Period"
$period = new Period();
$period->setTitle('MyTestTitle');
...
$fewo->addPeriod($period);
$this->fewoRepository->add($fewo);
$this->persistenceManager->persistAll();
Now the Fewo is in the database, but the period-table is still empty. I can't find my mistake...
UPDATE:
This is the Period Model:
<?php
namespace TYPO3\Fewo\Domain\Model;
class Period extends \TYPO3\CMS\Extbase\DomainObject\AbstractValueObject {
/**
* Name der Saison
*
* #var \string
*/
protected $name;
/**
* Von
*
* #var \DateTime
*/
protected $begin;
/**
* Bis
*
* #var \DateTime
*/
protected $end;
/**
* rentalcharges
*
* #var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\Fewo\Domain\Model\Rentalcharge>
*/
protected $rentalcharges;
/**
* __construct
*
* #return Period
*/
public function __construct() {
//Do not remove the next line: It would break the functionality
$this->initStorageObjects();
}
/**
* Initializes all ObjectStorage properties.
*
* #return void
*/
protected function initStorageObjects() {
/**
* Do not modify this method!
* It will be rewritten on each save in the extension builder
* You may modify the constructor of this class instead
*/
$this->rentalcharges = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
}
/**
* Returns the name
*
* #return \string $name
*/
public function getName() {
return $this->name;
}
/**
* Sets the name
*
* #param \string $name
* #return void
*/
public function setName($name) {
$this->name = $name;
}
/**
* Returns the begin
*
* #return \DateTime $begin
*/
public function getBegin() {
return $this->begin;
}
/**
* Sets the begin
*
* #param \DateTime $begin
* #return void
*/
public function setBegin($begin) {
$this->begin = $begin;
}
/**
* Returns the end
*
* #return \DateTime $end
*/
public function getEnd() {
return $this->end;
}
/**
* Sets the end
*
* #param \DateTime $end
* #return void
*/
public function setEnd($end) {
$this->end = $end;
}
/**
* Adds a Rentalcharge
*
* #param \TYPO3\Fewo\Domain\Model\Rentalcharge $rentalcharge
* #return void
*/
public function addRentalcharge(\TYPO3\Fewo\Domain\Model\Rentalcharge $rentalcharge) {
$this->rentalcharges->attach($rentalcharge);
}
/**
* Removes a Rentalcharge
*
* #param \TYPO3\Fewo\Domain\Model\Rentalcharge $rentalchargeToRemove The Rentalcharge to be removed
* #return void
*/
public function removeRentalcharge(\TYPO3\Fewo\Domain\Model\Rentalcharge $rentalchargeToRemove) {
$this->rentalcharges->detach($rentalchargeToRemove);
}
/**
* Returns the rentalcharges
*
* #return \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\Fewo\Domain\Model\Rentalcharge> $rentalcharges
*/
public function getRentalcharges() {
return $this->rentalcharges;
}
/**
* Sets the rentalcharges
*
* #param \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\Fewo\Domain\Model\Rentalcharge> $rentalcharges
* #return void
*/
public function setRentalcharges(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $rentalcharges) {
$this->rentalcharges = $rentalcharges;
}
}
UPDATE2:
Tried:
class Period extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity {...}
and:
$period = $this->objectManager->get('TYPO3\Fewo\Domain\Model\Period');
with no effect :(
Is Period in FeWo of the right type?
It have to be something like this:
* #var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\Fewo\Domain\Model\Fewo>