XF 2.2 Best practice for basicEntitySave with unique_fields

Robert9

Well-known member
Code:
    protected function saveProcess(\Lala\Entity\Country $country)
    {
        $form = $this->formAction();
        $input = $this->filter([
            'country_id' => 'uint',
            'country_name' => 'str',
            'country_saison' => 'str',
            'display_order' => 'uint',
        ]);
        $form->basicEntitySave($country, $input);
        return $form;
    }

Without any unique fields this code is the usual one and perfect.

But now i have changed country_id (!= inc_key!) and country_name to unique fields.
When i save the form, i get an Opps-overlay plus the text "true"

- so i have

1. to find out how to pass a text to the message and

2. What is the best practice for this;

just fetch or better just count rows with unique_fields = input?

Code:
    public function countExistingCountriesWithIdOrName($countryId, $countryName)
    {
        return $this->finder('Lala:Country')
            ->where( ... counry_id = $countryId or country__name = $countryName
    }

or anything else?
 
Code:
    protected function countrysaveProcess(\Lala\Entity\Country $country)
    {
        $form = $this->formAction();

        $input = $this->filter([
            'server_country_id' => 'uint',
            'server_country_name' => 'str',
            'country_saison' => 'str',
            'display_order' => 'uint',
        ]);

        $countryRepo = $this->getCountryRepo();
        if ($countryRepo->findCountryWithIdOrName($input['server_country_id'], $input['server_country_name'])
        ->fetchOne())
        {
            $form->logError(\XF::phrase('server_country_id_or_server_country_name_exists'));
        }
        else
        {
            $form->basicEntitySave($country, $input);
        }
        return $form;
    }
 
Last edited:
Or maybe just try and catch?
looks easier, needs no query?

I cant fix it; when trying to $form->basicEntitySave($country, $input); with same data to a unique field i got "true" as message back.
 
Last edited:
You can define fields as unique in the entity structure definition along with a phrase that will be returned on a duplicate exception.

Something like:
PHP:
'my_field' => ['type' => self::STR, 'unique' => 'my_error_phrase_name']
 
Yes, shure. I am finished with the tables, also with all classes and functions.
Now i would like to learn how to make things better.

Example:
country_id auto_inc
server_country_id unique
server_country_name unique
more fields

the data comes from a public database or from a manual input.
To avoid that countries are fetched/inserted twice times,
i have unique_fields for the foreign (server_)country_id and name.

When i fetch new countries (also with old ones) or add one by hand,
i have to check which ones are inserted and which ones are maybe updated.

Possible ways: use finder and look for existing id or name with fetch, count or just fetchOne.
Maybe with a try? (Have not solved it)
Maybe with just nothing, beause nothing happens now. I get "Ops ... and message "true";
so maybe i have to find out from where this "true" comes and change it to something like: "You have inserted existing data"


firefox_kyM23H9dAL.png
 
Top Bottom