0

I ran into a problem where I don't know how to check the uniqueness of some fields in the database, I don't have a field where only 1 element is unique, of course, except for the id, but I can't compare values by id because this is auto_increment and not manual input.

Question: how to compare if there are fields label, table_id

                    $this->db->table('context')->insert([
                        'type_flow' => $node['name'],
                        'title' => $node['data']['title'],
                        'label' => $item['label'],
                        'type' =>  $item['type'],
                        'value' => $item['value'],
                        'table_id' => $node['id'],
                    ]);

mysql table

I need to implement this in codeigniter and if there is a comparison method, can you tell me of course I think I could manually try to extract fields from the database and compare but I think the code would not be entirely correct

If you need more data, please write in the comments.

my try:

$db_conn = $this->model->where('type_flow', $item['field'])->where('table_id', $node['id'])->where('type_flow', 'conditions')->first() ? true : false;
                    if (!$db_conn) {
                        $this->db->table('context')->insert([
                            'type_flow' => $node['name'],
                            'title' => $node['data']['title'],
                            'label' => $item['field'],
                            'type' =>  $item['type'],
                            'value' => $item['value'],
                            'table_id' => $node['id'],
                        ]);
                    }
2
  • Can you give some examples of what you are trying to find? For example do you want to find any rows where label=="TITLE" and table_id==1 (so rows 1 and 11 in the screen shot, and maybe others not shown)? Do you want to just retrieve them, or do you want to prevent them from being written in the first place? For the latter I believe you can create a constraint based on multiple columns. Commented Jul 15 at 7:14
  • thank you very much, I have already solved the problem in another way Commented Jul 15 at 7:28

1 Answer 1

0
$conditions = $this->db->table('context')->where('type_flow', 'condition')->where('label', $item['field'])->where('table_id', $node['id']);
                        $conditions = $conditions->get();
                        if (!$conditions->getRow() != null) {
                            $this->db->table('context')->insert([
                                'type_flow' => $node['name'],
                                'title' => $node['data']['title'],
                                'label' => $item['field'],
                                'type' =>  $item['type'],
                                'value' => $item['value'],
                                'table_id' => $node['id'],
                            ]);
                        }

it turns out that we check the database for the presence of a field and extract it, if it is not null then we add data to it, and if it is null then we go through the following fields

we find the field using the where function, which determines the search by the identifier specified in it:

$conditions = $this->db->table('context')->where('type_flow', 'condition')->where('label', $item['field'])->where('table_id', $node['id']);

we call the get method to return the data:

$conditions = $conditions->get();

we get fields:

if (!$conditions->getRow() != null) {

we get fields for the condition of adding to the database:

                        if (!$conditions->getRow() != null) {
                            $this->db->table('context')->insert([
                                'type_flow' => $node['name'],
                                'title' => $node['data']['title'],
                                'label' => $item['field'],
                                'type' =>  $item['type'],
                                'value' => $item['value'],
                                'table_id' => $node['id'],
                            ]);
                        }
Sign up to request clarification or add additional context in comments.

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.