How do I check if $values->field already exists in table?

aleemont
Member | 11
+
0
-

I'm trying to make a check on the value that I'm trying to insert into the table.

This is my code, but it does not insert data into table and it does not show error message:

protected function createComponentStudentForm(): form{

    $form = new Form;
    $form->addText("name", "Name")->setRequired();
    $form->addText("surname", "Surname")->setRequired();
    $form->addText("age", "Age")->setRequired();
    $form->addSubmit("submit", "Submit");
    $form->onSuccess[] = [$this, 'studentFormSucceeded'];

    return $form;
    }

  public function studentFormSucceeded(Form $form, \stdClass $values): void{
    $a = $values->age;
    $rows = $this->database->table('students')->select('nsme')->where("age = ?", $a);
    if($rows == NULL){
    $this->database->table("students")->insert([
      "name"=>$values->name,
      "surname"=>$values->surname,
      "age"=>$values->age,
    ]);
    }
    else{
      $form->addError("Age exists yet");
    }
    $this->redirect('Homepage:index');
    }
}

I'm pretty sure my code is wrong, how do I check if a value does exist into a table column?

Last edited by aleemont (2020-08-25 13:43)

David Matějka
Moderator | 6275
+
+2
-

$rows is never null, Selection class provides a fluent interface. to fetch a single row just call ->fetch() on it.

Ondřej Kubíček
Member | 463
+
0
-

firts, use onValidate for validating your form instead of onSuccess which is for success submitting form or you can validate on input directly – see https://doc.nette.org/…m-validation

second, Form is instance of Nette\Application\UI\Form ?

third try to debug your code, and add breakpoint and see what is in $rows

aleemont
Member | 11
+
0
-

David Matějka wrote:

$rows is never null, Selection class provides a fluent interface. to fetch a single row just call ->fetch() on it.

So how am I supposed to check if the value to insert exists or not in the table?

I call

$rows->fetchAll();

and then what?

Last edited by aleemont (2020-08-25 15:08)

aleemont
Member | 11
+
0
-

Ondřej Kubíček wrote:

firts, use onValidate for validating your form instead of onSuccess which is for success submitting form or you can validate on input directly – see https://doc.nette.org/…m-validation

will check that. Thank you.

second, Form is instance of Nette\Application\UI\Form ?

Yes, it is.

third try to debug your code, and add breakpoint and see what is in $rows

How do I see what is in $rows?

Last edited by aleemont (2020-08-25 14:44)

Pavel Kravčík
Member | 995
+
+1
-

aleemont wrote:
How do I see what is in $rows?

Just call dump or bdump – Tracy dumper

aleemont
Member | 11
+
0
-

Pavel Kravčík wrote:

aleemont wrote:
How do I see what is in $rows?

Just call dump or bdump – Tracy dumper

Yeah, I tried it, but it shows an incomprehensible giant list.

aleemont
Member | 11
+
0
-

I thought this could be a solution, but Tracy shows LogicException Table 'students' does not have a primary key.

 protected function createComponentStudentForm(): form{

    $form = new Form;
    $rows = $this->database->table('students')->fetchAll();
    dump($rows);
    $form->addText("name", "Name")->setRequired();
    $form->addText("surname", "Surname")->setRequired();
    $form->addText("age", "Age")->setRequired()->addRule($form::IS_NOT_IN, "Age exists yet", $rows);
    $form->addSubmit("submit", "Submit");
    $form->onValidation[] = [$this, 'studentFormSucceeded'];

    return $form;
    }

  public function studentFormSucceeded(Form $form, \stdClass $values): void{
    $this->database->table("students")->insert([
      "name"=>$values->name,
      "surname"=>$values->surname,
      "age"=>$values->age,
    ]);
    $this->redirect('Homepage:index');
    }
}
aleemont
Member | 11
+
0
-

SOLVED

  protected function createComponentStudentForm(): form{

    $form = new Form;
    $form->addText("name", "Name")->setRequired();
    $form->addText("surname", "Surname")->setRequired();
    $form->addText("age", "Age")->setRequired();
    $form->addSubmit("submit", "Submit");
    $form->onSuccess[] = [$this, 'checkAge'];

    return $form;
    }

  public function checkAge(Form $form, \stdClass $values): void{
    $rows = $this->database->table("students")->fetchAll();
    $n = sizeof($rows);
    $exist = false;

    for($i=0;$i<$n;$i++){
      if($rows[$i]->age == $values->age){
        echo "Age exists yet";
        $exist = true;
        break;
       }
      }
    if($exist === false){
      $this->insertTable($form,$values);
    }
  }

  public function insertTable(Form $form, \stdClass $values): void{
    $this->database->table("students")->insert([
      "name"=>$values->nome,
      "surname"=>$values->surname,
      "age"=>$values->age,
    ]);
    $this->redirect('Homepage:index');
  }
}
CZechBoY
Member | 3584
+
0
-

Thats quite ineffective. Add primary key (and/or unique one) and check if it throws exception on insert.

Roman Halaxa
Member | 57
+
0
-

CZechBoY wrote:

Thats quite ineffective. Add primary key (and/or unique one) and check if it throws exception on insert.

Is this efficient? I use it for checking if record has related entity like this:

public function getOkres() : ?Okres
{
    if ($this->okres instanceof Proxy) {
        try {
            $this->okres->__load();
        } catch (\Doctrine\ORM\EntityNotFoundException $e) {
            $this->okres = null;
        }
    }

    return $this->okres;
}

But ever since I wondered how to get rid of it.

CZechBoY
Member | 3584
+
0
-

Roman Halaxa wrote:

CZechBoY wrote:

Thats quite ineffective. Add primary key (and/or unique one) and check if it throws exception on insert.

Is this efficient? I use it for checking if record has related entity like this:

public function getOkres() : ?Okres
{
    if ($this->okres instanceof Proxy) {
        try {
            $this->okres->__load();
        } catch (\Doctrine\ORM\EntityNotFoundException $e) {
            $this->okres = null;
        }
    }

    return $this->okres;
}

But ever since I wondered how to get rid of it.

Depends how you use it :)