How-to send sub data array to api-platform in a ManyToOne relation?

I’ve got a nice entity with 3 ManyToOne relations.

A final entity linked with the ID fields.

I want to send to my API each value for each parameter and save it in my database.

See my code below:

One of my linked entity (Mode and Customer are "quasi" the same):

/**
 * @ApiResource(
 *     normalizationContext={"groups"={"user:read"}},
 *     denormalizationContext={"groups"={"user:write"}},
 *     collectionOperations={
 *         "get",
 *         "post"={"security"="is_granted('ROLE_ADMIN')"}
 *     },
 *     itemOperations={
 *         "get",
 *         "put"={"security"="is_granted('ROLE_ADMIN')"},
 *     },
 *     attributes={"order"={"name": "ASC"}}
 * )
 * @ApiFilter(OrderFilter::class)
 * @ApiFilter(SearchFilter::class, properties={"id": "exact", "name": "ipartial"})
 * @UniqueEntity(fields={"name"})
 * @ORMEntity(repositoryClass="AppRepositoryParameterRepository")
 */
class Parameter
{
    /**
     * @ORMId
     * @ORMGeneratedValue
     * @ORMColumn(type="integer")
     * @ApiProperty(identifier=true)
     * @Groups({"user:read"})
     */
    private $id;

    /**
     * @ApiSubresource
     * @Groups({"user:read", "user:write"})
     * @ORMColumn(type="string", length=255)
     * @AssertNotBlank()
     * @AssertUnique()
     */
    private $name;

    /**
     * @Groups({"user:read", "user:write"})
     * @ORMColumn(type="text", length=65535, nullable=true)
     */
    private $help;

    /**
     * @ORMOneToMany(targetEntity=CustomersParameters::class, mappedBy="parameter")
     */
    private $param;

The entity who have the linked results:

     /**
     * @ApiResource(
     *     normalizationContext={"groups"={"user:read"}},
     *     denormalizationContext={"groups"={"user:write"}},
     *     collectionOperations={
     *          "get",
     *          "post"={"security"="is_granted('ROLE_ADMIN')"},
     *     },
     *     itemOperations={
     *          "get",
     *          "put"={"security"="is_granted('ROLE_ADMIN')"},
     *          "delete"={"security"="is_granted('ROLE_ADMIN')"},
     *     },
     * )
     * @ApiFilter(GroupFilter::class, arguments={"parameterName": "group_on", "overrideDefaultGroups": false, "whitelist": {"allowed_group"}})
     * @ApiFilter(OrderFilter::class, properties={"value","createdAt","updatedAt","customer.name","customer.id","parameter.name","parameter.id","mode.name","mode.id"})
     * @ApiFilter(SearchFilter::class, properties={"id": "exact", "value": "ipartial", "customer.name": "ipartial", "customer.id": "exact", "parameter.name": "ipartial", "parameter.id": "exact", "mode.name": "ipartial", "mode.id": "exact"})
     * @ORMEntity(repositoryClass="AppRepositoryCustomersParametersRepository")
     */
    class CustomersParameters
    {
        /**
         * @ORMId
         * @ORMGeneratedValue
         * @ORMColumn(type="integer")
         * @Groups({"CustomersParameters:read","user:read"})
         * @ApiProperty(identifier=true)
         */
        private $id;
    
        /**
         * @ORMColumn(type="string", length=255, nullable=false)
         * @Groups({"user:read","user:write"})
         * @AssertNotBlank()
         */
        private $value;
    
        /**
         * @ORMManyToOne(targetEntity=Customer::class, inversedBy="param")
         * @ORMJoinColumn(nullable=false)
         * @Groups({"user:read","user:write"})
         * @AssertNotBlank()
         */
        private $customer;
    
        /**
         * @ORMManyToOne(targetEntity=Parameter::class, inversedBy="param")
         * @ORMJoinColumn(nullable=false)
         * @Groups({"user:read","user:write"})
         * @AssertNotBlank()
         */
        private $parameter;
    
        /**
         * @ORMManyToOne(targetEntity=Mode::class, inversedBy="param")
         * @ORMJoinColumn(nullable=false)
         * @Groups({"user:read","user:write"})
         * @AssertNotBlank()
         */
        private $mode;
    
        /**
         * @ORMColumn(type="datetime", nullable=false)
         * @Groups({"user:read"})
         */
        private $createdAt;
    
        /**
         * @ORMColumn(type="datetime", nullable=true)
         * @Groups({"user:read"})
         */
        private $updatedAt;

I want to use in react-admin this component:

const CustomersParametersCreate =(props) => (
    <Create {...props} title={"Creation d'un lien client/paramètre"}>
        <SimpleForm toolbar={<Tools.CreateToolbar/>} warnWhenUnsavedChanges>
            <ReferenceInput
                label='Client'
                source="customer.name"
                reference="customers"
                validate={required()}
                filterToQuery={searchText => ({name: searchText, order: 'ASC', pagination: false})}
                fullWidth
            >
                <AutocompleteInput name={'customer'} optionText={"name"} allowNull={false}/>
            </ReferenceInput>
            <ReferenceInput
                label='Fonctionnement'
                source="mode.name"
                reference="modes"
                validate={required()}
                sort={{field: 'name', order: 'DESC'}}
                fullWidth
            >
                <AutocompleteInput optionText={"name"} allowNull={false}/>
            </ReferenceInput>

            <ArrayInput label={"Paramètrage"} source={'parameter'}>
                <SimpleFormIterator>
                    <ReferenceInput label='Paramètre' reference={'parameters'} source={'parameter.name'} filterToQuery={searchText => ({name: searchText, order: 'ASC', pagination: false})} fullWidth>
                        <SelectInput optionText={'name'} />
                    </ReferenceInput>
                    <TextInput source="value" label='Valeur' validate={required()} fullWidth/>
                </SimpleFormIterator>
            </ArrayInput>

        </SimpleForm>
    </Create>
);

And the POST result is:

{
"customer":
   {"name":"/api/customers/1"},
"mode":
   {"name":"/api/modes/1"},
"parameter":
   [{
   "parameter":
      {
      "name":"/api/parameters/1"
      },
      "value":"1"
   },
   {
   "parameter":
      {
      "name":"/api/parameters/2"
      },
      "value":"1"
   }]
}

What can I do to have the ‘value’ of the CustomersParameters entity linked with the parameter?

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *