Welcome Guest, Not a member yet? Register   Sign In
Unique customer
#1

I’m facing a small problem and hope CodeIgniter 4 has some build in way to handle this. I’m more then willing to write something myself, but rather use something internal if it’s available. 
I have a inventory management system. When a product gets sold, we link it to a specific customer. I want to make sure that a specific customer is always unique, so you can’t accidentally add it twice. This means I have to check if the combination of a couple of values is unique in the database (so, not the individual values, but the combination of those values). 
Is there a build in validation filter to check this? I know about is_unique, but that’s for a specific value.
Reply
#2

You would need to write your own validation filter for that.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

Thanks insiteFX!
Reply
#4

(This post was last modified: 08-27-2021, 05:33 AM by nfaiz.)

(08-27-2021, 01:21 AM)BFlokstra Wrote: I’m facing a small problem and hope CodeIgniter 4 has some build in way to handle this. I’m more then willing to write something myself, but rather use something internal if it’s available. 
I have a inventory management system. When a product gets sold, we link it to a specific customer. I want to make sure that a specific customer is always unique, so you can’t accidentally add it twice. This means I have to check if the combination of a couple of values is unique in the database (so, not the individual values, but the combination of those values). 
Is there a build in validation filter to check this? I know about is_unique, but that’s for a specific value.

Sugesstion;

From CI4's codehere

you can create your own custom validation for 2 or more fields like this

PHP Code:
    public function is_unique_2field(?string $strstring $field, array $data): bool
    
{
        // Grab any data for exclusion of a single row.
        [$field$ignoreField$ignoreValue$ignoreField2$ignoreValue2] = array_pad(explode(','$field), 5null);

        // Break the table and field apart
        sscanf($field'%[^.].%[^.]'$table$field);

        $db Database::connect($data['DBGroup'] ?? null);

        $row $db->table($table)
            ->select('1')
            ->where($field$str)
            ->limit(1);

        if (! empty($ignoreField) && ! empty($ignoreValue) && ! preg_match('/^\{(\w+)\}$/'$ignoreValue)) {
            $row $row->where("{$ignoreField} !="$ignoreValue);
        }

        if (! empty($ignoreField2) && ! empty($ignoreValue2) && ! preg_match('/^\{(\w+)\}$/'$ignoreValue2)) {
            $row $row->where("{$ignoreField2} !="$ignoreValue2);
        }

        return $row->get()->getRow() === null;
    
Reply




Theme © iAndrew 2016 - Forum software by © MyBB