У меня есть две сущности Person и PersonMontlyPayHistory.Когда я пытаюсь добавить элемент в свою коллекцию, я получаю сообщение об ошибке.
Ожидаемый аргумент типа "App \ Entity \ History \ MonthlyPayHistory", "массив", указанный в пути к свойству "monthPayHistory".
У меня нет ошибки при редактировании коллекции или удалении элемента из коллекции
Код выдержки следующий:
Сущности
Персона
use App\Entity\History\MonthlyPayHistory;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Person user
*
* @ORM\Table(name="user_person", indexes={@ORM\Index(name="slug_index", columns={"slug"}), @ORM\Index(name="firstname_index", columns={"first_name"}), @ORM\Index(name="lastname_index", columns={"last_name"})})
* @ORM\Entity(repositoryClass="App\Repository\User\PersonRepository")
* @ORM\HasLifecycleCallbacks()
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="type", type="string")
* @ORM\DiscriminatorMap({
* "person": "App\Entity\User\Person",
* "staff"="App\Entity\User\Staff",
* "worker"="App\Entity\User\Worker",
* "consultant"="App\Entity\User\Consultant",
* "subcontractor"="App\Entity\User\Subcontractor"
* })
*
* @AssertDav\NotSelfParent(parents={"manager"})
*
* @Serializer\ExclusionPolicy("all")
*/
abstract class Person extends User
{
/**
* @ORM\OneToMany(targetEntity="App\Entity\History\ContractHistory", mappedBy="person", cascade={"persist", "remove"})
* @ORM\OrderBy({"startDate"="ASC"})
*
* @Assert\Valid()
*/
protected $contractHistory;
/**
* Constructor
*/
public function __construct()
{
parent::__construct();
$this->monthlyPayHistory = new ArrayCollection();
}
}
MonthlyPayHistory
use App\Entity\User\Person;
/**
* MonthlyPayHistory
*
* @ORM\Table(name="history_monthly_pay")
* @ORM\Entity(repositoryClass="App\Repository\History\MonthlyPayRepository")
*/
class MonthlyPayHistory extends EstablishmentHistory
{
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User\Person", inversedBy="monthlyPayHistory")
* @ORM\JoinColumn(name="person_id", nullable=false)
*/
protected $person;
}
FormType
use App\Form\Type\BaseType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use App\Service\MatrixHelper;
use App\Entity\User\Person;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
/**
* Person monthly pay history form type
*/
class MonthlyPayHistoryPersonType extends BaseType
{
/**
* @param MatrixHelper $matrixHelper The matrix helper.
*/
public function __construct(MatrixHelper $matrixHelper)
{
parent::__construct($matrixHelper);
}
/**
* {@inheritDoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
$this->add('monthlyPayHistory', $builder);
}
/**
* Monthly pay collection field
*
* @param FormBuilderInterface &$builder The form builder.
* @param array $options The form options.
* @return FormBuilderInterface The builder object.
*/
public function addMonthlyPayHistoryField(FormBuilderInterface &$builder, array $options = array())
{
$builder->add('monthlyPayHistory', CollectionType::class, array_merge([
'entry_type' => 'App\Form\Type\History\MonthlyPayHistoryModalType',
'by_reference' => false,
'allow_add' => true,
'allow_delete' => true,
], $options));
}
/**
* {@inheritDoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults([
'data_class' => Person::class,
'validation_groups' => ['Default', 'MonthlyPayHistoryPerson'],
]);
/*$resolver ->setRequired(array(
'matrix_entity',
'matrix_action',
));*/
}
/**
* {@inheritDoc}
*/
public function getName()
{
return 'person_monthly_pay_history';
}
}
Контроллер
use App\Controller\BaseController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use JMS\SecurityExtraBundle\Annotation\PreAuthorize;
use JMS\SecurityExtraBundle\Annotation\Secure;
use JMS\SecurityExtraBundle\Annotation\SecureParam;
use App\Entity\User\Person;
use App\Form\Type\User\Person\ContractHistoryPersonModalType;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Person controller.
*
* @Route("/")
*/
class PersonController extends BaseController
{
/**
* Manage monthly pay history
*
* @param Request $request The request
* @param Person $person A person entity.
*
* @return JsonResponse
*
* @Route(
* {
* "en": "/{_scope}/person/{slug}/monthly-pay-history",
* "fr": "/{_scope}/person/{slug}/monthly-pay-history"
* },
* name="Person_MonthlyPayHistory")
* @ParamConverter("person", class="App\Entity\User\Person")
*/
public function monthlyPayHistoryAction(Request $request, Person $person)
{
//services
$formErrorService = $this->get('dav_app.service.form_error');
$form = $this->createForm('App\Form\Type\User\Person\MonthlyPayHistoryPersonType', $person);
$originalPerson = clone $person;
$originalElements = clone $person->getMonthlyPayHistory();
$errorMessage = '';
if ($request->isMethod('POST')) {
$userEvent = new \App\Event\UserEvent( $originalPerson, $person);
//error_log("request save data".print_r($request,true));
//submit data
$form->handleRequest($request);
if ( $form->isValid() ) {
//services
$em = $this->getDoctrine()->getManager();
$dispatcher = $this->get( 'event_dispatcher' );
$em->persist( $person );
$em->flush();
$dispatcher->dispatch(DavAppEvents::PERSON_MONTHLY_PAY_HISTORY_COMPLETED, $userEvent);
$responseStatus = true;
} else {
$responseStatus = false;
//get the errors as a string
$errorMessage = $formErrorService->getRecursiveReadableErrors($form);
}
} else {
$responseStatus = true;
}
$formView = $this->renderView(
'user/person/monthlyPayHistory.html.twig',
[
'form' => $form->createView(),
'person' => $person,
'startDate' => $person->getCurrentContract()->getStartDate()->format("d-m-Y")
]
);
return new JsonResponse(array(
'data_type' => 'user',
'data_count' => 1,
'form' => $formView,
'request_type' => 'person_monthly_pay_change',
'response_status' => $responseStatus,
'error_message' => $errorMessage
));
}
}
Услуги: введите описание изображения здесь
Параметры почты введитеописание изображения здесь
исключение введите описание изображения здесь